Alternative for __dirname in node when using the --experimental-modules flag

前端 未结 9 1891
旧时难觅i
旧时难觅i 2020-12-01 06:03

I use the flag --experimental-modules when running my node application in order to use ES6 modules.

However when I use this flag the metavariable

9条回答
  •  情歌与酒
    2020-12-01 06:50

    As of Node.js 10.12 there's an alternative that doesn't require creating multiple files and handles special characters in filenames across platforms:

    import { dirname } from 'path';
    import { fileURLToPath } from 'url';
    
    const __dirname = dirname(fileURLToPath(import.meta.url));
    

提交回复
热议问题