3

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!

KermanX
  • 63
  • 1
  • 5
  • Without seeing your code, I'm not sure how we can be expected to help you. – Brad Dec 19 '21 at 03:32
  • Does this answer your question? [Error \[ERR\_REQUIRE\_ESM\]: How to use es6 modules in node 12?](https://stackoverflow.com/questions/57169793/error-err-require-esm-how-to-use-es6-modules-in-node-12) – msanford Dec 19 '21 at 04:20
  • @msanford Thanks a lot, but it is not my question. – KermanX Dec 19 '21 at 04:27
  • Are you possibly coding in TypeScript and have the compile target set for ES5 or something that generates `require()` instead of using `import`? I'd suggest you look at your generated JS code and see what it's doing. – jfriend00 Dec 19 '21 at 07:05
  • @jfriend00 You are right! OH MY GOD! I copied tsconfig.json from somewhere I've forgotten, and it has module:"commonjs"!!!!!!!Thank you so much!!!!!! – KermanX Dec 19 '21 at 07:43
  • But how to fix it up? – KermanX Dec 19 '21 at 08:01

1 Answers1

4

Make sure that your TypeScript config is set for an appropriate target version of Javascript and appropriate target module type that supports and will use 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.

jfriend00
  • 683,504
  • 96
  • 985
  • 979