__dirname是否在Node.js 10实验版中未定义? [英] __dirname is not defined in Node.js 10 experimental?

查看:42
本文介绍了__dirname是否在Node.js 10实验版中未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Node.js 10.0.0&我的 index.mjs 看起来像:

 从"path"导入路径;console.log(__ dirname); 

在我的终端中,运行

  node --experimental-modules index.mjs 

然后出现以下错误:

 (节点:3750)实验警告:ESM模块加载程序是实验性的.ReferenceError:__dirname未定义在file:///MyFolderPath/node-10/index.mjs:3:21在ModuleJob.run(内部/模块/esm/module_job.js:106:14) 

解决方案

ESM不是特定于节点的,特定于节点的全局变量"(例如 __ dirname 和模块)将不起作用. import.meta 有望提供合适的替代品./p>

来源:GitHub 问题.

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)

解决方案

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.

这篇关于__dirname是否在Node.js 10实验版中未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆