-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
"main" entry point should be CommonJS module format #126
Comments
Hello, In previous versions, rollup was configured to produce UMD builds with both ESM and CJS support, but I changed it in v4 to only produce ESM builds. See v4 release notes for details
That's not what I understand from Node.js docs :
Current package.json on this repo tries to follow the option 2.3 in this article: https://2ality.com/2019/10/hybrid-npm-packages.html#option-3%3A-bare-import-esm%2C-deep-import-commonjs-with-backward-compatibility ; except I don't provide a CommonJS build, because I think it's for the best for this library users to bring their own transpiler. I also explain my reasons in v4 release notes So because But here there must be an issue with your transpiler configuration. The error message is pretty clear:
That means the transpiler replace the import instruction by require, but without touching to the library code which is an ESM build, hence the error. I don't know your whole build step configuration, but I can show you how to build a CJS version of the lib if it helps ? |
Thanks for your help. Yes, I'm using Babel for transpilation, and I'm not sure why it is trying to import Anyway, I've worked around the problem by building my own CJS version of ObjectModel. Thanks again for your help. |
No problem. I was expecting this move to ESM only to cause issues for some users with quite complex build steps or older versions of Node. Most lib authors are currently dealing with the problem by providing "hybrid" packages, but that just moves the complexity to our side and slows down the necessary transition to be able to just ship ESM everywhere. Hopefully, next Node LTS will ease this process for everyone. |
Thanks for this fantastic library Sylvain.
I am writing my application in standard ES modules. My development server runs on the source code, transpilling dynamically at runtime.
A component of my application imports
ObjectModel
as follows.This causes the server to fail with the following error.
I tried swapping out
babel-node
foresm
to handle the runtime transpilation, but the result is the same.On closer inspection, I think the problem lies in the ObjectModel package. The entry points to the package are defined in
package.json
as follows:Node.js expects the "main" entry point to adopt the CommonJS module format for imports, for legacy reasons, while the "module" entry point is for standard ES imports. However, the
dist
directory is built to theesm
format:I think the
output.format
setting needs to be changed tocjs
. 🤔The text was updated successfully, but these errors were encountered: