babel-loader: Doesn't compile a npm linked module
Not sure this is a babel loader issue or a webpack issue.
I am developing two modules and one of them has a dependency on the other. So I have npm linked it. I have the following config on webpack:
loaders: [{
test: /\.js$/,
loaders: ['babel'],
exclude: /node_modules\/(?!other-module)/,
include: __dirname
}]
This works fine if I install dependency as a local dependency but if I use npm link, babel doesn’t process it.
Am I missing something? Or is there a workaround for this?
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 117
- Comments: 72 (2 by maintainers)
Links to this issue
Commits related to this issue
- fix(internals): Fix babel relative preset Fix webpack issue with relative babel preset paths: https://github.com/babel/babel-loader/issues/149 — committed to pavlin-policar/react-boilerplate by pavlin-policar 8 years ago
- fix(internals): Fix babel relative preset Fix webpack issue with relative babel preset paths: https://github.com/babel/babel-loader/issues/149 temp — committed to pavlin-policar/react-boilerplate by pavlin-policar 8 years ago
- fix(internals): Fix babel relative preset Fix webpack issue with relative babel preset paths: https://github.com/babel/babel-loader/issues/149 — committed to pavlin-policar/react-boilerplate by pavlin-policar 8 years ago
- fix(internals): Fix babel relative preset Fix webpack issue with relative babel preset paths: https://github.com/babel/babel-loader/issues/149 — committed to pavlin-policar/react-boilerplate by pavlin-policar 8 years ago
- feat(docs): Extracting packages into their own modules (#979) * fix(internals): Fix babel relative preset Fix webpack issue with relative babel preset paths: https://github.com/babel/babel-loader... — committed to react-boilerplate/react-boilerplate by pavlin-policar 8 years ago
- feat(docs): Extracting packages into their own modules (#979) * fix(internals): Fix babel relative preset Fix webpack issue with relative babel preset paths: https://github.com/babel/babel-loader... — committed to samit4me/react-boilerplate by pavlin-policar 8 years ago
- feat(docs): Extracting packages into their own modules (#979) * fix(internals): Fix babel relative preset Fix webpack issue with relative babel preset paths: https://github.com/babel/babel-loader... — committed to samit4me/react-boilerplate by pavlin-policar 8 years ago
- Merge with base (#2) * feat(docs): Extracting packages into their own modules (#979) * fix(internals): Fix babel relative preset Fix webpack issue with relative babel preset paths: https://git... — committed to AnhHT/react-boilerplate by AnhHT 7 years ago
- Merge with base (#3) * feat(docs): Extracting packages into their own modules (#979) * fix(internals): Fix babel relative preset Fix webpack issue with relative babel preset paths: https://git... — committed to AnhHT/react-boilerplate by AnhHT 7 years ago
- feat(docs): Extracting packages into their own modules (#979) * fix(internals): Fix babel relative preset Fix webpack issue with relative babel preset paths: https://github.com/babel/babel-loader... — committed to marvellous122/react-redux-base by marvellous122 8 years ago
- feat(docs): Extracting packages into their own modules (#979) * fix(internals): Fix babel relative preset Fix webpack issue with relative babel preset paths: https://github.com/babel/babel-loader... — committed to shaun554/react-boilerplate by shaun554 8 years ago
- Fixed issue related to https://github.com/babel/babel-loader/issues/149 — committed to NorthwaveSecurity/forked-frida-inject by tijme 4 years ago
- Had to do this in order to make it work: https://github.com/babel/babel-loader/issues/149#issuecomment-159909713 — committed to jrand0m/fashcrowdapp by and3rson 8 years ago
- feat(docs): Extracting packages into their own modules (#979) * fix(internals): Fix babel relative preset Fix webpack issue with relative babel preset paths: https://github.com/babel/babel-loader... — committed to mauricewells/react-boilerplate by deleted user 8 years ago
- feat(docs): Extracting packages into their own modules (#979) * fix(internals): Fix babel relative preset Fix webpack issue with relative babel preset paths: https://github.com/babel/babel-loader... — committed to kazuma512/react-boilerplate-typescript by kazuma512 8 years ago
- feat(docs): Extracting packages into their own modules (#979) * fix(internals): Fix babel relative preset Fix webpack issue with relative babel preset paths: https://github.com/babel/babel-loader... — committed to AIDevMonster/react-boilerplate by AIDevMonster 8 years ago
- feat(docs): Extracting packages into their own modules (#979) * fix(internals): Fix babel relative preset Fix webpack issue with relative babel preset paths: https://github.com/babel/babel-loader... — committed to whiteghostDev/react-boilerplate by whiteghostDev 8 years ago
- feat(docs): Extracting packages into their own modules (#979) * fix(internals): Fix babel relative preset Fix webpack issue with relative babel preset paths: https://github.com/babel/babel-loader... — committed to muscliary/react-boilerplate by pavlin-policar 8 years ago
- feat(docs): Extracting packages into their own modules (#979) * fix(internals): Fix babel relative preset Fix webpack issue with relative babel preset paths: https://github.com/babel/babel-loader... — committed to wesleywang4766/lodash by wesleywang4766 8 years ago
@mingxian For Webpack 2, adding this to seemed to resolve (derp) the issue :
@henit Try putting the presets in your package.json instead with a babel property:
I have the same issue. I have a package at
/path/to/project/
and a dependency that worked well when located in/path/to/project/node_modules/dependency/
. However, when I npm link the dependency to a git clone located at the same place as the project (/path/to/dependency/
, linked via/usr/lib/node_modules
the way npm links), then the loaders complain that there is no presets installed in the package directory for dependency when I run webpack from the project directory thatnpm link
s to it.Webpack loader config in
/path/to/project
:Output from running webpack:
How can I make babel-loader use the presets installed in the project the same way it did when the dependency where located inside the project directory instead of looking for it in the linked project directory?
this worked for me, Notice that I don’t have a ‘include’ key.
It works to me just setting
Ran into a similar issue as @hitchcott and wound up resolving it with the following:
before (not working)
after (working)
It should be noted, I also have the following:
This is happening to me as as well. @barroudjo’s suggested workaround seems to work in my case:
@davidortizfrau’s
exclude
solution was the only solution here that worked for me: https://github.com/babel/babel-loader/issues/149#issuecomment-226637036I was experiencing the same JSX problem as @hitchcott: https://github.com/babel/babel-loader/issues/149#issuecomment-221946176
I still can’t get this to work. I’ve tried everything in this thread. Seems to be specific with
npm link
d react components. ES6 appears to be working, but it’s choking as soon as it gets to jsx including in a linked module.Using Webpack 4 I did not use the symlink: false in resolve
but simply had to use
@sebinsua I have tested this a bit now and am pretty sure why it works (at least for me)!
If you have your library at
/path/to/linked-package
, and your project at/path/to/project
. npm linking will link/path/to/project/node_modules/linked-package
->/path/to/linked-package
Let’s say you have the following exclude:
With
symlinks: true
,modulePath
will be/path/to/linked-package
. This will not match any of the regexp above as node_modules is not part of the path.With
symlinks: false
,modulePath
will be/path/to/project/node_modules/linked-package
, which does match the regexp.Hope this helps 😃
I’m a big fan of @seantimm’s solution in #179:
Easier than all those
require.resolve
s and allows you to decouple your settings into the .babelrc again. Just thought I’d add it as another solution until @Swaagie’s PR is merged in Babel…Seeing the same issue, building failed with an
npm link
module, after removing the link and manually copying over the locally developed module it worked fine. There is something odd with symlinks going on. Webpack config is as follow. Working with node@4.2 and npm@3.5.What should I put in
babel.config.js
?Your config is not right. You need to include babel presets in your loader config as well as your babelrc or babelConfig in your package.json. The presets for babel loader can be added with query params or query object. Review the babel 6 docs on presets for your babel config. The preset packages must also be included your project. Currently things are working for me with babel-loader@6.01 with babel presets and babel-core@6.1.4
@theninja This worked for me, but I’m now wondering why it works. Do you know why it’s working?
What was strange to me, was that even though I had originally made the
loader.include
point to the resolved location of a symlink, that didn’t work. Then when I got it to just ignore thesymlinks
using this configuration it started to work…I’m wondering if this bit of configuration could negatively impact anything, because if it does I’ll want to be able to switch it on/off.
I had the same issue as @hitchcott with an app created with create-react-app and followed @tswaters suggestion, now it works.
First issue
I replace in the paths:
by
Second issue
Nearly the same kind but not exactly
I add the following code in the query of babel loader:
Third issue
Now I set:
Final issue
It works! … except I commented this section:
and when I suppress the comments, I get the following errors:
To fix that, I add in
shared-hybrid/package.json
:And it works! Hope someone will find it usefull 😉
I have the same setup as @Nirazul. Using
require.resolve('babel-preset-es2015')
instead of'es2015'
resolves this issue for me.Additionally I set
resolve.modulesDirectories: [path.resolve('./node_modules')]
so modules in other directories can find modules installed in the main project.Yea I agree with @Swaagie. The babel-loader fails to find it’s preset modules when the module isn’t under the project root containing the node_modules (even when
resolveLoader.root
is set). Similar to #166@janzenz inside
LoaderOptionsPlugin
. I’ve “fixed” the problem by installing all babel-related plugins innode_modules
of linked module.Added tests to the PR few days ago (finally), I’ll see if I can give someone a nudge.
Thanks @mqklin, this works, but unfortunately when you use multiple loaders you can’t use a query object. Here is the workaround in this case:
I’m pretty sure I’ve got those modules installed and it’s not about the presets, babel-loader simply fails with symlinks
The issue here is most likely that
.babelrc
files in Babel 6 only apply to files nested in folders within the.babelrc
’s folder. This means the only option in Babel 6 is to put the configuration directly in yourwebpack.config.js
.As of Babel 7, this is restricted even further, and
.babelrc
files will also not apply to child packages, but we’ve also added support for project-widebabel.config.js
files, so I’d recommend users who wish to compilenode_modules
and linked packages use those.Just want to give my 2 cents as well, at first the symlinks: false didn’t work for me.
After some more digging i saw that in the resolve section i had the following setup:
turns out that there is a big difference between absolute path loading in modules in the resolve section vs relative loading see this article: https://webpack.js.org/configuration/resolve/#resolve-modules
So with that setup it failed my build since it couldn’t find the actual files in my symlinked directory.
After changing it to:
it worked perfectly for me, even my webpack dev server picks up the changes and refreshes what it needs to do.
Maybe it helps for someone who runs into this.
The reason why i’m adding my app to resolve modules is so i can reference pretty much anything from root level, so in my bundles i don’t have to do:
…/…/someModule/index.js
instead i can just simply:
someModule/index.js
in case someone is wondering why i use that setting
I had the same issue using an ejected react app. However, it seems to be working when I include the
realpath
instead of the absolute path to the target dependency.On webpack 3 with yarn workspaces,
javascript resolve: { symlinks: false }
made things click. nice
@joaoreynolds We’re not using resolve.modules in these two projects, for whatever that is worth. Nor do we have Webpack or Babel setup or packaged in the symlinked deps. Outer configs look roughly like:
But, I doubt that makes a difference. Anyway, my thought was just that --watch can work with symbolic links in node_modules and symlink === false, so I don’t think the root cause for you is as straightforward as it might seem.
Update, the only way I could get watching (of any kind) with
npm-link
to work was havesymlinks: true
and I had to install all babel presets and plugins in the linked dependency module - which seems weird because the linked dependency doesn’t even have it’s own webpack setup.Maybe I’m the only one in thinking that if
symlinks: true
(default webpack behavior), then my project should look for plugins and presets in the current directory’s node_modules. (just the same way it loadsbabel-loader
which is NOT required to be installed in the linked-module){symlinks: false}
fixes the error, however it disrupts webpack’s watch function (and therefore webpack-dev-server will not recompile when changing files in the “linked” dependency).Anybody found another solution?
@henit
I just found a method in the Babel docs. If you need a preset or plugin locally/bundled in your project like I did you can provide the plugins and presets a relative or absolute path to the node module package.
Notice the preset name instead of
es2015
it is a relative path./node_modules/babel-preset-es2015
.TIP: You also might need to resolve to the absolute path in which case you can use
Path.join(__dirname, './relative/path/package');
Adding an include to
fs.realpathSync(__dirname + '/node_modules/my-repo/lib')
might also be another solution thanks to https://github.com/webpack/webpack/issues/1643#issuecomment-220484356.Chiming in on this thread as I’ve got a variant scenario (I think). I’ve developing a CLI tool that functions like a black box site generator. My intent is that the tool be installed globally and executed from the command line where it locates a containing package.json relative to cwd() that it parses to retrieve some options. Subsequently, the contents of the target project (i.e. the dir structure rooted at the directory where package.json was found) is processed into a site, and the generated resources are written back out to the target project directory structure.
Internal to the tool, I synthesize webpack configuration objects and delegate to webpack which in turn delegates to babel-loader, babel … and fails when resolving the es2015 and react presets as described here (for slightly different reasons than typical but same root cause I think).
Currently, webpack and babel loader are correctly resolved in the node_modules dir of my tool where they’re declared as regular dependencies in the tool’s package.json. This isn’t a global install of anything more than a random Node.js derived CLI tool that happens to depend on webpack, babel… as an implementation detail.
So although I agree with the rationale of wanting to install babel on a per-project basis, this sharp edge with presets makes it difficult to embed in automation scripts.
For now I’ve modifying the dev-dependencies of the target project package.json and going to force an npm install prior to the first invocation of webpack (occurs as one of many steps in the site generation process encapsulated by the tool). But I think this extra complexity is not really necessary.
This thread: https://discuss.babeljs.io/t/error-parsing-jsx-with-global-installation-babel-preset-react/59/14 on Babel discussion is fairly recent and seems related.
Anyone know how to get this sort of wholly encapsulated embedding to work elegantly? Thanks.