Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在commonjs项目中使用es模块包?_Node.js - Fatal编程技术网

Node.js 在commonjs项目中使用es模块包?

Node.js 在commonjs项目中使用es模块包?,node.js,Node.js,我正在尝试将es模块包与commonjs项目一起使用。我发现了错误 Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/user/Documents/git/test/node_modules/logger/lib/index.js require() of ES modules is not supported. require() of /Users/user/Documents/git/test/node_m

我正在尝试将es模块包与commonjs项目一起使用。我发现了错误

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/user/Documents/git/test/node_modules/logger/lib/index.js
require() of ES modules is not supported.
require() of /Users/user/Documents/git/test/node_modules/logger/lib/index.js from /Users/user/Documents/git/test/testLogger.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/user/Documents/git/test/node_modules/logger/package.json.

这个包裹实际上是我自己的。除了替换所有导入之外,我还能做些什么使它与commonjs一起工作?

您需要安装babel register和babel preset env

//添加到index.js或start.js 需要“babel-register”{ 预设:['env'] };
请参阅此处的详细信息

您的节点版本是什么?@Sehdev v13.10.1如果出现错误,建议尝试从package.json文件中删除1type:module`。@Sehdev npm包需要类型module,本地项目需要类型commonjs。我也不能删除