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-node not working with "type": "module" in package.json #11108
Comments
Hey @subhero24! 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 currently expected. |
Worth add a note to https://babeljs.io/docs/en/babel-register that babel-register does not support compiling native Node.js es modules on the fly. |
I would like to help out in this doc fix. |
Should we close this issue since it has been solved in babel/website#2177 ? |
The problem is if you set And babel config in esm format works fine with |
You can use |
Thanks for response! I can't use |
Alright so is there no way to use a dynamic config while also having |
So what is the suggestion here? My understanding is that we use |
This is a nasty issue once you run into it - this essentially is keeping me from being able to use newer javascript features and native ES modules alongside one another - it breaks babel-node which is used in development |
Yeah this issue is sad. I'm "okay" with transpiling stuff, but the reason we are transpiling is so that we can use newer Javascript while browsers will still understand it. But we are not only using newer Javascript because it's cool, we also use it because it's "future-proof". There is a hope that one day you can remove all this transpiling and just give the browser the source code and everything works fine. This is basically what you are aiming for...right? With this issue, my source code will never be compatible with any browser in the future, it will only be compatible with babel. @JLHwung, why is this issue still closed? |
When the @babel/register hook isn't available, @babel/node should probably just transpile the whole app into a temp directory, and then run the node executable on the file so it at least "works"
|
Keep in mind that
This won't work for dynamic I think we can throw purposely on |
Yeah, but that's what I'm saying. Of course it's way slower now, in the present, but it's about being able to use the same exact code in the future without transpiling. It's about writing as much standard code as possible, to reduce future changes to it the moment you can actually use this code as it is, when browsers support these standards as well. Idk, that's at least how I understood it. I know that using Node.js itself while developing a webapp for example, is already not the "standard" ES way of doing it. But at least, at the moment, if I would write only supported ES code, without external modules, but also use dev tools like eslint and babel locally (which rely on node.js) just to make my dev experience better, I could in fact put this directory on a server with an index.html and it would just work. The moment I'd want to introduce a dynamic babel config, the whole thing breaks. Either I have to use commonjs module style for my whole project, transpile it and deploy it like that, or I have to live without babel and eslint. Of course, all that seems risky and at the current state, you still would want to transpile your code to support every reasonable browser out there, but why should the source code change for that? The dist code can change all it wants, no problem, but at the moment I'm forced to make small changes to actually working standard code. I'm curently totally fine with that and I fully understand that this isn't top priority, and that we have to wait for dependencies like Node.js to support this kind of coding, where you write standard ES code, everywhere from your source code, to your config files. What I'm trying to say here is that this issue isn't "solved", it shouldn't be closed, because like @nicolo-ribaudo said
but it isn't expected forever. Node.js itself apparently needs to work on that stuff as you said, and the suggestion
is a perfect one, but still, this issue shouldn't be closed, just because it is blocked. That's effectively all I'm saying. This issue was closed way too fast and I'm just wondering if there will be (or already is) a follow up issue or if this issue here will be reopened, otherwise I don't know how one would keep track of it. I know this seems pedantic, but I just feel like there is no current "tracker" of the progress of @babel/register and Node.js, respectively supporting compiling native ES modules on the fly. This issue could be that tracker, so my suggestion would be to reopen it. |
@nnmrts I think you misunderstood this issue. You can successfully use ES modules with Babel on Node.js and I'm doing it in different projects. The only thing that you cannot do is using the |
Btw, if you really want to rely on an unstable Node.js API to compile modules on the fly, you can use https://github.com/targos/multiloader which supports Babel |
So you are saying I'm able to write my babel.config.js, my eslint config, my rollup config and my actual source code all in ES module syntax? Including support in my IDE (vscode)? Because last time I checked, that wasn't the case, something always broke down and I had to use the cjs syntax and file extension for my configs, and even then, my toolchain somewhere complained that I "must use import to load ES Module" even though that's exactly what I'm doing. I'll revisit my repository in which I had these issues and see if it's working now somehow. |
I'll create a demo repo to share! The only problem is that you cannot use |
https://github.com/nicolo-ribaudo/babel-native-esm-demo
I don't know if those tools support native ESM configs, but Babel does. |
@nicolo-ribaudo Thank you for now, I'll look into it. |
Maybe I'm really just dumb and used an old version of babel or something, idk. It was roughly a week ago where my project stopped working. |
I just realized that it doesn't work with webpack, I'll review babel/babel-loader#825 so that it can work. |
I have the following script that I'm running with
npx babel-node script.js
It executes fine with my babel config
When I add "type": "module" to my package.json file, the
npx babel-node script.js
command fails to run and I get the errorSyntaxError: Unexpected token '?'
.So it seems specifying my package as module, prevents babel-node from loading the babel config?
The text was updated successfully, but these errors were encountered: