简体   繁体   English

奇怪的 Nodejs:我从不使用 `require()`,但出现错误

[英]weird Nodejs: I never use `require()`, but got an error

Please see Issue请看问题

I'm very sure that neither the package and I used no require() , but still got an error that tells me don't use require()我非常确定 package 和我都没有使用require() ,但仍然有一个错误告诉我不要使用require()

How weird it is!多么奇怪啊!

code with error:错误代码:

import stripAnsi from 'strip-ansi';

error:错误:

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.

And the most confused thing is that: import statement is helpful everywhere except importing strip-ansi !最让人困惑的是:import 语句除了导入strip-ansi外,到处都有用!

Make sure that your TypeScript config is set for an appropriate target version of Javascript that supports import so that the TypeScript compiler will generate code that uses import .确保为支持import的 Javascript 的适当目标版本设置了 TypeScript 配置,以便 TypeScript 编译器生成使用import的代码。 If not, then it will generate code that uses require() .如果没有,那么它将生成使用require()的代码。

You can always look at your compiled (plain Javascript) code and see what it is generating.您始终可以查看已编译的(纯 Javascript)代码并查看它生成的内容。

Yes, I didn't use any require() .是的,我没有使用任何require() But I wrongly got但我错了

module:"commonjs"

in tsconfig.json!在 tsconfig.json 中!

so ts changed import into require ...所以 ts 将import改为require ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM