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

chalk 5.x requires ESM support #5

Closed
drernie opened this issue Sep 16, 2023 · 3 comments · Fixed by #6
Closed

chalk 5.x requires ESM support #5

drernie opened this issue Sep 16, 2023 · 3 comments · Fixed by #6

Comments

@drernie
Copy link
Contributor

drernie commented Sep 16, 2023

Reproducible test case

@drernie
Copy link
Contributor Author

drernie commented Sep 16, 2023

const chalk = require("chalk");
              ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/ernest/Developer/ts-cli/node_modules/chalk/source/index.js from /Users/ernest/Developer/ts-cli/dist/src/utils.js not supported.
Instead change the require of index.js in /Users/ernest/Developer/ts-cli/dist/src/utils.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/Users/ernest/Developer/ts-cli/dist/src/utils.js:14:15)
    at Object.<anonymous> (/Users/ernest/Developer/ts-cli/dist/src/index.js:4:17) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v20.5.1

@drernie
Copy link
Contributor Author

drernie commented Sep 16, 2023

Trying suggestions from:
https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

How can I make my TypeScript project output ESM?

Read the official ESM guide.

Quick steps:

  1. Make sure you are using TypeScript 4.7 or later.
  2. Add "type": "module" to your package.json.
  3. Replace "main": "index.js" with "exports": "./index.js" in your package.json.
  4. Update the "engines" field in package.json to Node.js 16: "node": ">=16".
  5. Add "module": "node16", "moduleResolution": "node16" to your tsconfig.json. (Example)
  6. Use only full relative file paths for imports: import x from '.'; → import x from './index.js';.
  7. Remove namespace usage and use export instead.
  8. Use the node: protocol for Node.js built-in imports.
  9. You must use a .js extension in relative imports even though you're importing .ts files.
  10. If you use ts-node, follow ESM support: soliciting feedback TypeStrong/ts-node#1007. Example config.

@drernie drernie changed the title chalk 5.x fails with typescript chalk 5.x requires ESM support Sep 16, 2023
@drernie
Copy link
Contributor Author

drernie commented Sep 16, 2023

Renaming for ESM support: chalk/chalk#613

https://www.typescriptlang.org/docs/handbook/esm-node.html

however, support for ESM in Node.js is now implemented in Node.js, and the dust has begun to settle.

@drernie drernie linked a pull request Sep 16, 2023 that will close this issue
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

Successfully merging a pull request may close this issue.

1 participant