[英]weird Nodejs: I never use `require()`, but got an error
請看問題
我非常確定 package 和我都沒有使用require()
,但仍然有一個錯誤告訴我不要使用require()
多么奇怪啊!
錯誤代碼:
import stripAnsi from 'strip-ansi';
錯誤:
Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users\13931\Desktop\ucon\node_modules\strip-ansi\index.js from C:\Users \13931\Desktop\ucon\src\utty.ts not supported.
Instead change the require of index.js in C:\Users\13931\Desktop \ucon\src\utty.ts to a dynamic import() which is available in all commonjs modules.
最讓人困惑的是:import 語句除了導入strip-ansi
外,到處都有用!
確保為支持import
的 Javascript 的適當目標版本設置了 TypeScript 配置,以便 TypeScript 編譯器生成使用import
的代碼。 如果沒有,那么它將生成使用require()
的代碼。
您始終可以查看已編譯的(純 Javascript)代碼並查看它生成的內容。
是的,我沒有使用任何require()
。 但我錯了
module:"commonjs"
在 tsconfig.json 中!
所以 ts 將import
改為require
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.