question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error [ERR_REQUIRE_ESM]: require() of ES

See original GitHub issue

Error [ERR_REQUIRE_ESM]: require() of ES Module /var/runtime/app/node_modules/serialize-error/index.js from /var/runtime/app/src/webhooks/sendgrid.ts not supported. Instead change the require of index.js in [redacted] to a dynamic import() which is available in all CommonJS modules.

import { serializeError } from 'serialize-error';

tsconfig:

{
  "compilerOptions": {
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "module": "commonjs",
    "outDir": "dist",
    "skipLibCheck": true,
    "strict": true,
    "target": "ES2020",
    "lib": ["esnext"]
  },
  "exclude": [
    "infrastructure/**/*.ts",
    "test/**/*.ts"
  ]
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:5
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
luizfelipelaviolacommented, Jun 24, 2022

Works downgrading to 8.1.0

0reactions
papbcommented, Nov 21, 2022

Hi everyone

"module": "commonjs",

This part of tsconfig.json is probably the cause of the error. It has to be node16, not commonjs.

You also need to set "moduleResolution": "node16" as well.

This is explained in the link given by Sindre above. Here’s a quote:

Can I import ESM packages in my TypeScript project?

Yes, but you need to convert your project to output ESM. See below.

How can I make my TypeScript project output ESM?

Read the official ESM guide.

Quick steps:

  • Make sure you are using TypeScript 4.7 or later.
  • Add "type": "module" to your package.json.
  • Replace "main": "index.js" with "exports": "./index.js" in your package.json.
  • Update the "engines" field in package.json to Node.js 14: "node": ">=14.16". (Excluding Node.js 12 as it’s no longer supported)
  • Add "module": "node16", "moduleResolution": "node16" to your tsconfig.json. (Example)
  • Use only full relative file paths for imports: import x from '.';import x from './index.js';.
  • Remove namespace usage and use export instead.
  • Optional but recommended, use the node: protocol for imports.
  • You must use a .js extension in relative imports even though you’re importing .ts files.

If you use ts-node, follow this guide. Example config.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error [ERR_REQUIRE_ESM]: require() of ES Module not ...
The node-fetch latest version doesn't use the require() syntax to import the package. You need to go to your package.json and type
Read more >
Error [ERR_REQUIRE_ESM]: require() of ES Module not ...
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...
Read more >
Must use import to load ES Module / require() of ES ... - GitHub
I have suddenly started getting the following error: $ npm test > geodesy@2.2.0 test /home/travis/build/chrisveness/geodesy > mocha --exit ...
Read more >
ERR_REQUIRE_ESM - DEV Community ‍ ‍
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: ~/projects/semantic-release-toolkit/node_modules/zz/cjs.js require() of ES ...
Read more >
Error ERR REQUIRE ESM | Must use import to load ES Module
Error ERR REQUIRE ESM | Must use import to load ES Module. 15K views 1 year ago JS - JavaScript · Sagar S....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found