Home > Blockchain >  weird Nodejs: I never use `require()`, but got an error
weird Nodejs: I never use `require()`, but got an error

Time:12-19

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()

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!

CodePudding user response:

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. If not, then it will generate code that uses require().

You can always look at your compiled (plain Javascript) code and see what it is generating.

CodePudding user response:

Yes, I didn't use any require(). But I wrongly got

module:"commonjs"

in tsconfig.json!

so ts changed import into require...

  • Related