-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
Babel core not importable with Node.js native ESM support #11247
Comments
Hey @kriskowal! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly. If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite." |
This is a known issue per how CJS files are only able to generate |
@bmeck, I read your statement as “This is not a Node.js bug. Using Babel as it is written today depends on crutches of ESM that will vanish soon. Babel will need to change to accommodate Node.js.” Is this correct? Can you propose a rule that Babel would need to follow to straddle CJS and ESM, like “Avoid default exports.”? |
@kriskowal I wouldn't phrase it as such since I don't actually think this is a bug but an integration problem by the nature of the transpilation; note that babel file in question is CJS and CJS does not export named bindings in Node. I wouldn't state that importing CJS is broken on Node's side, nor would I state that babel creating CJS from ESM input is broken. They just don't mix / no solutions have been able to be found. There are lots of issues exactly about this topic, from things on Node trackers, at TC39, and many more. In general mixing the transpiled ecosystem and spec ESM simply doesn't work as it stands. So, once you cross the transpilation barrier things must adhere to their compilation target expectations (in the case of |
We might decide to change this in Babel 8, but in the meantime use |
@nicolo-ribaudo Thank you! That is a fine work-around. Feel free to close if you do not require this task to track. |
I'll defer the decision to @JLHwung, since he recently started investigating Node.js's new |
I agree on what Bradley has said before: exports.transformSync = require("./transform").transformSync The ESM named import The We are likely to ship dual cjs/esm builds of babel on Babel 9, which is expected in next year. So in the future it should be valid to do import * as babelCore from "@babel/core" // the esm entry is imported At this moment the Node.js ES Module API is still experimental and Babel 8 is on the horizon, it is risky to ship esm builds for Babel 8. Before that please use import babelCore from "@babel/core" since I am gonna close that because the current situation is expected. |
Bug Report
Current Behavior
Babel version 7.8.7
import * as babelCore from '@babel/core'
from an ESM in Node.js’s experimental support for ESMs produces an object with atransformSync
accessor but the accessor returnsundefined
. This occurs when the containing package.json has"type": "module"
set or when the file has the.mjs
extension. The same module works fine when executed withnode -r esm
:babelCore.transformSync
is a function. The same also works fine when required with CommonJS conventions.Unexpected behavior for Node.js ESM support when file has
.mjs
extension:Expected behavior: log a defined function object.
// [Function: spec]
Unexpected behavior for Node.js ESM support when file has a
.js
extension and module type in package.json:As expected, the .mjs extension or module type in package.json are required to use native import.
As expected,
node -r esm
does access a defined function.As expected,
node
works fine when importing as CommonJS.It is possible that this behavior is due to aberrant behavior of Node.js’s ESM implementation. It is likely that this is a symptom of an underlying problem.
The text was updated successfully, but these errors were encountered: