Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error [ERR_REQUIRE_ESM] #1129

Closed
Hawley-hy opened this issue Jun 20, 2022 · 14 comments
Closed

Error [ERR_REQUIRE_ESM] #1129

Hawley-hy opened this issue Jun 20, 2022 · 14 comments

Comments

@Hawley-hy
Copy link

Hawley-hy commented Jun 20, 2022

Error detail as bellow:

Error [ERR_REQUIRE_ESM]: require() of ES Module node_modules/inquirer/lib/inquirer.js from csv-to-insert/index.ts not supported.
Instead change the require of inquirer.js in csv-to-insert/index.ts to a dynamic import() which is available in all CommonJS modules.
}

code

const inquirer = require("inquirer");
inquirer
  .prompt([]).then()

Env:
Node version v16.11.1

tsconfig:

{
    "compilerOptions": {
        "target": "ES2017",
        "module": "commonjs",
        "allowSyntheticDefaultImports": true,
        "allowUnreachableCode": true,
        "allowUnusedLabels": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "forceConsistentCasingInFileNames": true,
        "noFallthroughCasesInSwitch": true,
        "noUnusedLocals": false,
        "moduleResolution": "node",
        "outDir": "dist",
        "removeComments": true,
        "strict": true,
        "strictFunctionTypes": false,
        "suppressImplicitAnyIndexErrors": true,
        "sourceMap": true,
        "strictPropertyInitialization": false,
        "baseUrl": ".",
        "paths": {
        },
        "plugins": [
            {
                "name": "tslint-language-service"
            }
        ]
    },
    "exclude": [
        "node_modules"
    ]
}

script in package.json

ts-node --project tsconfig.json index.ts
@AlejandroSotoCastro
Copy link

Same problem

@SBoudrias
Copy link
Owner

Inquirer v9 is an esm node module. So TypeScript code must be compiled to ESM too for Node to work properly.

See release announcement #1126 and feel free to discuss over there your solution or ask for help.

This is not a bug with Inquirer.

A few resources that might help you find the right configuration:

Also, bundling your dependencies in a single package with babel/webpack/etc should also interop properly.

@softmarshmallow
Copy link

This basically means we can't use Inquirer with ts-node while development.

@SBoudrias
Copy link
Owner

SBoudrias commented Aug 1, 2022

@softmarshmallow that isn't true. I've been using ts-node while developing inquirer itself. You need to add this to your ts-config.json file: https://github.com/SBoudrias/Inquirer.js/blob/master/tsconfig.json#L4-L6

Also, as long as you bundle all the code together, you can compile in any way you wants. The problem with modules for npm is if your module is compiled to commonjs, but relies on esm dependencies.

@bali182
Copy link

bali182 commented Sep 22, 2022

@SBoudrias what is the recommended way to use Inquirer for commonjs project? Just use v8?

@SBoudrias
Copy link
Owner

@bali182 yeah, that's your best bet for now!

@gabrielgrover
Copy link

I still get Error [ERR_REQUIRE_ESM]: require() of ES Module when trying to run with ts-node. Even with the tsconfig.json property @SBoudrias mentioned

@kolarski
Copy link

kolarski commented Nov 6, 2022

@SBoudrias Unless you use Typescript Paths (https://www.typescriptlang.org/tsconfig#paths) like import from @core/myModule
then ts-node not work and it will not resolve the paths in esm mode. Basicly will not be able to import anything.
There is a 1-year-old issue that is still not resolved in the ts-node repo: TypeStrong/ts-node#1585

So stuck working with the outdated version.

@thebug404
Copy link

thebug404 commented Apr 6, 2023

Explanation of the problem.

The error "[ERR_REQUIRE_ESM]: require() of ES Module not supported. Instead change the require of index.js to a dynamic import() which is available in all CommonJS modules" occurs because a package you are importing has been converted to an ESM-only package, which means that the package cannot be imported with require() anymore.

First solution

  1. Remove "type": "module" from package.json if it's added
  2. In tsconfig.json under the compilerOptions Set module property to CommonJS module: "CommonJS" and moduleResolution: "Node"

Second solution

This solution was the one I used.

  1. Add "type": "module" to package.json
  2. Install ts-node npm i -g ts-node
  3. Go to tsconfig.json and add the following:
{
     "compilerOptions": {
         "module": "ESNext",
         "moduleResolution": "Node",
         /* ... your props ... */
     },
     "ts-node": {
         "esm": true
     }
}
  1. Run ts-node {filename}.ts or if it is installed locally run npx ts-node {filename}.ts
  2. Incorporating this change requires all imports to carry .js (only the files belonging to your project)
//Bad
import { random } from './utils'

//Good
import { random } from './utils.js'

With this the error has to be solved. If you found another way to solve the problem, you can reply to this comment to help others. 🫶

@VLtim43
Copy link

VLtim43 commented Apr 6, 2023

Second solution

The second option partially works for me. While the import of inquirer.js now works, literally all of the other regular imports in the rest of the code have stopped working.

@thebug404
Copy link

Second solution

The second option partially works for me. While the import of inquirer.js now works, literally all of the other regular imports in the rest of the code have stopped working.

You are absolutely right, due to the change that was added with the second option, it is necessary that you add the .js extension to your files when importing them.

For example:

// Bad
import { random } from './utils'

// Good
import { random } from './utils.js'

I hope this works for you.

@wizardpisces
Copy link

fallback to version 8 fix the problem

@woshiqiji
Copy link

fallback to version 8 fix the problem
npm install inquirer@^8.0.0 -S Yes, dropping to version 8 would fix that

@bitcoinbrisbane
Copy link

fallback to version 8 fix the problem
npm install inquirer@^8.0.0 -S Yes, dropping to version 8 would fix that

Confirmed. Fixed my issue too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests