簡體   English   中英

不支持 ES 模塊的 require()

[英]require() of ES modules not supported

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: D:\...\node_modules\normalize-url\index.js
[0] require() of ES modules is not supported.
[0] require() of D:\...\node_modules\normalize-url\index.js from D:\...\routes\api\users.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.
[0] Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from D:\Personnel\Linu
x downloads n docs\Docs\Project 2\node_modules\normalize-url\package.json.

我試圖降級 node-fetchand type:module,但沒有任何效果。

您安裝了一個新版本的normalize-url ,其package.json包含{type:module}這是從:

...package.json 包含“類型”:“模塊”

因為模塊是使用 'import' 而不是 'require' 加載的,所以 Node 會返回錯誤。


怎么解決

您可能需要 CJS 腳本。

以標准方式刪除並重新安裝 package( npm remove normalize-url && npm i normalize-url )。 如果npm i normalize-url@latest已安裝,則它是 ES2015 並且僅適用於導入語句。

將節點版本降級到 LTS,它開始工作了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM