Javascript 如何转移到ESM
我正在使用NodeJs(v14.15.4)应用程序 我想使用最新版本的Javascript 如何转移到ESM,javascript,node.js,esm,Javascript,Node.js,Esm,我正在使用NodeJs(v14.15.4)应用程序 我想使用最新版本的p-debounce库,而不是const-pDebounce=require('p-debounce')我必须使用import-pDebounce from'p-debounce'在EggJs上不起作用 如果我使用导入 (节点:10636)警告:要加载ES模块,请在package.json中设置“type”:“module”或使用.mjs扩展名。 (使用`node--trace warnings…`显示警告的创建位置) tes
p-debounce
库,而不是const-pDebounce=require('p-debounce')
我必须使用import-pDebounce from'p-debounce'
在EggJs上不起作用
如果我使用导入
(节点:10636)警告:要加载ES模块,请在package.json中设置“type”:“module”或使用.mjs扩展名。
(使用`node--trace warnings…`显示警告的创建位置)
test.js:5
从“p-debounce”导入PDebound
^^^^^^
SyntaxError:无法在模块外部使用导入语句
如果我需要
如果我在package.json中添加“type”:“module”
我的应用程序中有许多要求
,目前不想将所有内容更改为导入
您似乎忽略了以下方面的一个特定点: 如果您还不能移动到ESM,请不要升级到此版本 正如您所指出的,您的应用程序已经取得了显著的进步,您现在不想更改许多
需要的。此时无需根据您的需要升级到最新版本的p-debounce
当您决定迁移到ESM格式时,为了一次升级一个文件
internal/modules/cjs/loader.js:1080
throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
^
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: .\node_modules\p-debounce\index.js
require() of ES modules is not supported.
require() of .\node_modules\p-debounce\index.js from .\test.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 .\node_modules\p-debounce\package.json.
const path = require('path');
^
ReferenceError: require is not defined