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

Import as ES Module fails #18

Open
DrMurx opened this issue Dec 22, 2021 · 3 comments
Open

Import as ES Module fails #18

DrMurx opened this issue Dec 22, 2021 · 3 comments

Comments

@DrMurx
Copy link

DrMurx commented Dec 22, 2021

Right now with version 4.2.0, importing multi-progress-bars into a project using ES modules fails like this:

file:///....../node_modules/multi-progress-bars/dist/multi-progress-bars.mjs:1
import { green } from 'chalk';
         ^^^^^
SyntaxError: Named export 'green' not found. The requested module 'chalk' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'chalk';
const { green } = pkg;

It's caused by how the TypeScript transpiler optimize import statements if it compiles to an ES module, and how NodeJS treats CJS imports in said ES modules. The error message suggests a potential solution to the problem, but I'm not sure if the TypeScript transpiler could be configured to achieve these statements.

Related (yet not similar) issues are discussed here and here.

As a side note, the issue doesn't appear if dist/multi-progress-bars.cjs is imported instead. However, this workaround can't be used with the current multi-progress-bars release, as it defines an exports section in its package.json which masks direct access to the files in the dist/ folder. The workaround would require the exports to be modified.

@kamiyo
Copy link
Owner

kamiyo commented Jan 9, 2022

Since I really don't know how all this importing stuff works (or I don't grok it at a low level), I can only go by following writeups I find online on how to dual publish commonjs and ESM. However, it appears I forgot a root ".": for my exports section... which maybe that will fix it? I also did the import pkg from 'chalk'; const { green } = pkg; bit suggested by node. You can test it out with prerelease version 4.2.2-0. Let me know if it happens to work.

@DrMurx
Copy link
Author

DrMurx commented Jan 10, 2022

Hey @kamiyo, thank you, this solved the issue. I confirm that 4.2.2-0 works properly in mjs environments.

@kamiyo
Copy link
Owner

kamiyo commented Jan 10, 2022

Awesome. I'll make an actual release tonight that isn't a prerelease.

@kamiyo kamiyo mentioned this issue Jan 12, 2022
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