如何迁移到 ESM
How to move to ESM
我正在为我的 NodeJs(v14.15.4) 应用程序使用 Egg framework
我想使用最新版本的 p-debounce
库,the package is now pure ESM,而不是 const pDebounce = require('p-debounce')
我必须使用 import pDebounce from 'p-debounce'
,它不适用于 EggJs
如果我使用导入
(node:10636) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
test.js:5
import pDebounce from 'p-debounce'
^^^^^^
SyntaxError: Cannot use import statement outside a module
如果我使用 require
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.
如果我在 package.json
中添加 "type": "module"
const path = require('path');
^
ReferenceError: require is not defined
我的应用程序中有很多 require
,目前不想全部更改为导入
- package.json 中的“类型”:“模块”是什么?
- 如何修复错误?
您似乎忽略了 the changelog you linked 中的一个特定点:
If you cannot move to ESM yet, don't upgrade to this version.
正如您所指出的,您的申请已经取得了显着进展,并且您有许多 require
目前不想更改。暂时不需要升级到最新版本p-debounce
,根据您的需要。
当您决定迁移到 ESM 格式时,为了一次升级单个文件,you can use the .mjs
extension to treat the code as an ECMAScript module.
我正在为我的 NodeJs(v14.15.4) 应用程序使用 Egg framework
我想使用最新版本的 p-debounce
库,the package is now pure ESM,而不是 const pDebounce = require('p-debounce')
我必须使用 import pDebounce from 'p-debounce'
,它不适用于 EggJs
如果我使用导入
(node:10636) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
test.js:5
import pDebounce from 'p-debounce'
^^^^^^
SyntaxError: Cannot use import statement outside a module
如果我使用 require
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.
如果我在 package.json
中添加 "type": "module"const path = require('path');
^
ReferenceError: require is not defined
我的应用程序中有很多 require
,目前不想全部更改为导入
- package.json 中的“类型”:“模块”是什么?
- 如何修复错误?
您似乎忽略了 the changelog you linked 中的一个特定点:
If you cannot move to ESM yet, don't upgrade to this version.
正如您所指出的,您的申请已经取得了显着进展,并且您有许多 require
目前不想更改。暂时不需要升级到最新版本p-debounce
,根据您的需要。
当您决定迁移到 ESM 格式时,为了一次升级单个文件,you can use the .mjs
extension to treat the code as an ECMAScript module.