我正在尝试创建一个独立的node.js项目。我遵循的步骤是-
npm init.
node-fetch.const fetch = require("node-fetch");语句导入提取模块的新模块。得到以下错误-
const fetch = require("node-fetch");
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/jatin/Desktop/test-app/node_modules/node-fetch/src/index.js from /Users/jatin/Desktop/test-app/index.js not supported. Instead change the require of /Users/jatin/Desktop/test-app/node_modules/node-fetch/src/index.js in /Users/jatin/Desktop/test-app/index.js to a dynamic import() which is available in all CommonJS modules. at Object.<anonymous> (/Users/jatin/Desktop/test-app/index.js:2:15) { code: 'ERR_REQUIRE_ESM' }
我的机器上的节点版本是- v16.9.0。
发布于 2021-09-11 12:45:14
您应该使用ES模块导入而不是require。
import * as fetch from 'node-fetch';您也可以使用动态导入,因为错误消息声明。
const fetch = import('node-fetch');https://stackoverflow.com/questions/69142503
复制相似问题