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

babel doesn't transform ES6 under node_modules #41

Closed
zebulonj opened this issue Jul 21, 2015 · 26 comments
Closed

babel doesn't transform ES6 under node_modules #41

zebulonj opened this issue Jul 21, 2015 · 26 comments

Comments

@zebulonj
Copy link

This boilerplate uses a nested node_modules folder to make internal references cleaner (I like it). However, when the code under the folder is ported to ES6, it is no longer compiled by babel-node because it is assumed to be a module.

Options:

  • compile ES6 code before execution, and execute via node rather than babel-node
  • babel require hook
  • babel runtime?
@zebulonj zebulonj changed the title babel-node doesn't transform ES6 under node_modules babel doesn't transform ES6 under node_modules Jul 21, 2015
@zebulonj
Copy link
Author

I'm not convinced that babel runtime will address this.

@therealklanni
Copy link

Modules should be doing their own 6to5 transpiling and providing the compiled "dist" version as the "main" in package.json. It's not this project's (or any other project's) responsibility to compile dependencies to ES5.

/2¢

@nkbt
Copy link
Contributor

nkbt commented Jul 21, 2015

@therealklanni, node_modules is used internally for convenience (not for external modules). And node_modules gets ignored. This is a valid point.

@therealklanni
Copy link

Oh, I misread that as the main node_modules folder where npm dependencies are installed.

@nkbt
Copy link
Contributor

nkbt commented Jul 21, 2015

I personally do not think using node_modules for convenience is a good idea.

@zebulonj
Copy link
Author

@nkbt Fair critique. It's unconventional. I've been experimenting with it, and in my own project I've moved the structure to a src directory and am using babel src --out-dir app.

@nkbt
Copy link
Contributor

nkbt commented Jul 21, 2015

Compiling code before running it for backend feels awkward, just need to whitelist internal node_modules for babel. Need to add it to .babelrc

@zebulonj
Copy link
Author

@nkbt Is that all there is to it? I agree about the awkwardness.

@nkbt
Copy link
Contributor

nkbt commented Jul 21, 2015

@zebulonj I am not 100% sure, of course, but looks like that should be it.

@zebulonj
Copy link
Author

@nkbt It appears that the -w option refers to transformers, not files to compile. This is born out by a test I just attempted. See https://babeljs.io/docs/usage/cli/

@ericelliott
Copy link
Contributor

Compiling code before running it for backend feels awkward, just need to whitelist internal node_modules for babel. Need to add it to .babelrc

If that's possible, it's the right solution.

Otherwise, I have no problem compiling the internal node_modules folder as demonstrated in this article.

Of course, we leave the vendor node_modules alone.

@ericelliott
Copy link
Contributor

You may want to ask the babel team how to handle this before resorting to the pre-compile route.

@adrian-seijo
Copy link

Based on past experiences in a project with node modules where we run mocha through babel-node (and after expend a fair amount of time trying to figure out why it didnt transpile... :D) you just need to change the ignore property in the require call, by default is a regexp for /node_modules/, if you change it it will transpile fine: https://babeljs.io/docs/usage/require/

@zebulonj
Copy link
Author

Thanks @kileras. This works:

babel-node --ignore ^\.\/node_modules app

What do you all think? Smell funny?

@zebulonj
Copy link
Author

Now that we've identified a command-line option that works, it can alternatively be added to the package.json file or to a .babelrc.

@nkbt
Copy link
Contributor

nkbt commented Jul 22, 2015

better to .babelrc

zebulonj added a commit to zebulonj/universal-react-boilerplate that referenced this issue Jul 22, 2015
@zebulonj zebulonj mentioned this issue Jul 22, 2015
@zebulonj
Copy link
Author

Ugh. In the .babelrc, ignores appear to add to the default behavior (ignoring node_modules) rather than replacing it.

@nkbt
Copy link
Contributor

nkbt commented Jul 22, 2015

Then add to npm scripts if it does not work. And you can file a bug to babel as well...

@gavriguy
Copy link

Another solution for the same problem (requiring modules from the root) is to use a a custom babel plugin - https://github.com/gavriguy/babel-plugin-project-relative-require as discussed at http://stackoverflow.com/questions/31068698/importing-node-modules-from-root-directory-using-es6-and-babel-node/31069137#31069137

Basically you can use the ~ sign to require modules from the root e.g. import {a} from '~my-module'; will require my-module that is located at the root folder from anywhere in your code. This also solves the issue of using es6

@ericelliott
Copy link
Contributor

@gavriguy I don't like the idea of littering the codebase with weird/uncommon babel preprocessors.

@ericelliott
Copy link
Contributor

Then add to npm scripts if it does not work. And you can file a bug to babel as well...

👍

@ericelliott
Copy link
Contributor

What's the status of this issue?

@vasco3
Copy link
Contributor

vasco3 commented Sep 24, 2015

I got this working following this solution http://stackoverflow.com/questions/31822593/import-a-module-from-node-modules-with-babel-but-failed/31822668#31822668

babel-node --ignore '/node_modules/(?!my_module1|my_module2)' script.js

Since babel-node ignores node_modules by default, this regex will unignore just the module you want to transpile... Although this is not a pattern that the babeljs community wants to encourage, since all npm packages should be transpiled on pre-publish.

@ericelliott
Copy link
Contributor

If anybody wants to take a crack at this, I'd appreciate it. It will probably be a couple weeks before I can get back to it.

@ericelliott
Copy link
Contributor

Scrapped. See new docs.

@Nedudi
Copy link

Nedudi commented Aug 16, 2017

you can use https://www.npmjs.com/package/babel-node-modules

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

Successfully merging a pull request may close this issue.

8 participants