__dirname is not defined in Node.js 10 experimental?

落花浮王杯 提交于 2021-02-16 06:25:39

问题


I am using Node.js 10.0.0 & my index.mjs looks like:

import path from "path";

console.log(__dirname);

In my terminal, I run

node --experimental-modules index.mjs

And I get the following error:

(node:3750) ExperimentalWarning: The ESM module loader is experimental.
ReferenceError: __dirname is not defined
at file:///MyFolderPath/node-10/index.mjs:3:21
at ModuleJob.run (internal/modules/esm/module_job.js:106:14)

回答1:


ESM is not node-specific, and node-specific "globals" (such as __dirname and module) will not work. import.meta is expected to provide a suitable replacement.

Source: GitHub issue.



来源:https://stackoverflow.com/questions/50268077/dirname-is-not-defined-in-node-js-10-experimental

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!