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

ES module for toxic #9

Closed
gustawdaniel opened this issue Mar 7, 2023 · 3 comments
Closed

ES module for toxic #9

gustawdaniel opened this issue Mar 7, 2023 · 3 comments

Comments

@gustawdaniel
Copy link

gustawdaniel commented Mar 7, 2023

I have error

file:///home/daniel/pro/on-chain/custom-events/node_modules/colors-cli/toxic.js:1
const co = require('./lib/color');
           ^

ReferenceError: require is not defined in ES module scope, you can use import instead
    at file:///home/daniel/pro/on-chain/custom-events/node_modules/colors-cli/toxic.js:1:12
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

I am using typescript, tsx and importing by

import 'colors-cli/toxic'

I am attaching fragment of tsx docs https://www.npmjs.com/package/tsx

tsx is zero-config because it has smart detections built in. As a runtime, it detects what's imported to make many options in tsconfig.json redundant—which was designed for compiling matching files regardless of whether they're imported.

It seamlessly adapts between CommonJS and ESM package types by detecting how modules are loaded (require() or import) to determine how to compile them. It even adds support for require()ing ESM modules from CommonJS so you don't have to worry about your dependencies as the ecosystem migrates to ESM.


I migrated my project from tsx to rollup with @rollup/plugin-typescript.

@gustawdaniel
Copy link
Author

gustawdaniel commented Mar 7, 2023

in watch mode rollup works but after build running it with node i see

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/daniel/pro/on-chain/custom-events/node_modules/colors-cli/toxic' imported from /home/daniel/pro/on-chain/custom-events/dist/app.js
Did you mean to import colors-cli/toxic.js?

I fixed it by

import 'colors-cli/toxic.js'

Finally i decide to left this issue opened because esm modules are more and more popular and potentially other people will see this error and solution, that I hope will be temporary.

@gustawdaniel gustawdaniel reopened this Mar 7, 2023
jaywcjlove added a commit that referenced this issue Mar 7, 2023
jaywcjlove added a commit that referenced this issue Mar 7, 2023
jaywcjlove added a commit that referenced this issue Mar 7, 2023
@jaywcjlove
Copy link
Owner

@gustawdaniel Maybe solve your problem

Upgrade 1.0.30 to test?

@gustawdaniel
Copy link
Author

It works! Thank you so much.

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

2 participants