我在我的NodeJs(v14.15.4)应用程序中使用蛋框架
我想使用p-debounce库的最新版本,这个包现在是纯ESM的。,而不是const pDebounce = require('p-debounce'),我必须使用不能在EggJ上工作的import pDebounce from 'p-debounce'
如果我使用导入
(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如果我使用要求
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":“模块”
const path = require('path');
^
ReferenceError: require is not defined我的应用程序中有许多require,目前不想全部更改为导入。
发布于 2021-02-17 08:06:52
您似乎忽略了你联系上的变化中的某个特定点。
如果你还不能移动到ESM,不要升级到这个版本。
正如您已经指出的,您的应用程序已经有了很大的进步,并且您有许多require目前不想更改。现在没有必要根据您的需要升级到最新版本的p-debounce。
当您决定迁移到ESM格式时,为了一次升级单个文件,扩展,将代码视为ECMAScript模块。
发布于 2022-11-04 02:00:51
ES模块是未来的发展方向。
在ES模块中有一种需要common.js模块的方法。使用createRequire
axios -curlirize是ES模块,axios同时支持.
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const axios = require('axios');
import curlirize from 'axios-curlirize';
curlirize(axios);
const { data } = await axios.get('https://jsonplaceholder.typicode.com/todos/1');
console.log(data)使用ES模块,您可以使用顶级等待,而不需要功能.。
https://stackoverflow.com/questions/66237573
复制相似问题