Skip to content

[BUG?] npm link causes Webpack to look for babel-plugin in linked package node_modules. #1866

Closed

Description

@trusktr

I've linked a package that is outside of the project by first running npm link in the outside package, then npm link name-of-package inside the project.

I am now getting this error:

Module build failed: ReferenceError: Unknown plugin "transform-es2015-arrow-functions" specified in "base" at 0, attempted to resolve relative to "/path/to/location/far/away/from/project/name-of-package"

Of course there's no babel plugin there.

Activity

  1. trusktr commented on Jan 8, 2016

    @trusktr
    Author

    Is there an intention for package authors to depend on certain babel plugins? Maybe that's why this error happens, since the package is outside my project the babel plugin wasn't found there? If that's the case, this would be a feature like how browserify lets developers specify transforms, and wouldn't be a bug, but would need to be documented for library authors to take advantage of.

    Nonetheless, it should still work if my project has the needed plugins installed.

  2. gileze33 commented on Jan 18, 2016

    @gileze33

    Just ran into the same issue, did you manage to find a work around? npm link is obviously super important for our workflow

  3. trusktr commented on Jan 18, 2016

    @trusktr
    Author

    @gileze33 Unfortunately I haven't found a workaround. What I'm doing now is just git pushing my package to GitHub, then re-installing the new one each time I push, for development. It's a slower workflow, but at least it works for now. package.json just has the dependency listed something like

    {
      "dependencies": {
        "package-name": "username/package-name#master"
      }
    }
    

    to get it from GitHub. I can then just change it to a version number once I publish a version on NPM.

  4. basham commented on Jan 21, 2016

    @basham

    I'm avoiding the issue by sticking with [email protected], the latest version I've found that works with npm link. I've been able to upgrade other babel loaders, plugins, and presets without issue in the meantime. I also have the latest webpack and webpack-dev-server. Just going to wait for a solution.

  5. trusktr commented on Jan 21, 2016

    @trusktr
    Author

    Ah, good to know! I'll try giving that a shot too.

    /#!/JoePea
    On Jan 21, 2016 12:15 PM, "Chris Basham" [email protected] wrote:

    I'm avoiding the issue by sticking with [email protected], the latest
    version that works. I've been able to upgrade other babel loaders, plugins,
    and presets without issue in the meantime. Just going to wait for a
    solution.

    —
    Reply to this email directly or view it on GitHub
    #1866 (comment).

  6. trusktr commented on Jan 23, 2016

    @trusktr
    Author

    @basham Indeed, [email protected] works! I'm using that for now, as being able to npm link makes dev life a lot easier.

  7. trusktr commented on Jan 23, 2016

    @trusktr
    Author

    Oh, wait, so is this an issue with Babel, not Webpack?

  8. basham commented on Jan 25, 2016

    @basham

    I highly suspect it is due to this 6.2.0 change:

    Infer filename from the base directory when resolving plugins and presets.

    The issue seems to be that babel plugins now want to be relative to their module, not relative to the main project. Webpack isn't aware of Babel plugins to resolve it like it does with the resolve and resolveLoader configurations. Webpack could potentially solve this, but I'm not convinced it should be Webpack's responsibility to accommodate how babel does its resolving.

  9. kintel commented on Feb 21, 2016

    @kintel

    This sounds a bit like webpack isn't correctly excluding node_modules. Perhaps there's a symlink resolution issue causing it to run the babel loader on npm linked modules, even though the links are located in node_modules.
    Browserify had a very similar bug a while ago, and I experience this bug to be of similar nature.

    Update: It looks like this is indeed the case, but that's not the real cause of this issue. I can force webpack to exclude npm linked modules by adding my module name to the list of excludes:

          loaders: [ {
            exclude: [/node_modules/, /my-module/],
            loader: "transform?brfs"
          } ]
    

    Of course, this only helps if you don't want to run the transform. If you do, the originally reported issue is still present. See below for more info.

  10. kintel commented on Feb 22, 2016

    @kintel

    I investigated a bit more and I can reproduce this problem without any usage of babel.
    A simple npm linked module and using the brfs transform loader triggers the same issue:

        module: {
          loaders: [ { loader: "transform?brfs" } ]
        }
    

    I don't currently have any good workaround for this, except ensuring that you only npm link fully transformed modules. Also, browserify seems to handle all this pretty well (although that's not a good workaround seen from the perspective of this project :/ )

  11. slightlytyler commented on Feb 25, 2016

    @slightlytyler

    Also experiencing this issue. @basham your suggestion works but doesn't truly solve the problem, just circumvents the issue of webpack loading linked modules from their source directory for the case of transpiling with babel. In my case the babel transform error is gone but my linter (loaded through webpack) is still printing errors for the linked module.

  12. zzarcon commented on Mar 30, 2016

    @zzarcon

    I'm also having the same issue using babel-plugin-transform-async-to-generator, I just downgraded from [email protected] to [email protected] and it works...

    Update: Found a workaround:

    query: {
      presets: ['es2015', 'stage-0'],
      plugins: ['babel-plugin-transform-async-to-generator'].map(require.resolve)
    }

    More info babel/babel-loader#166 (comment)

    Any update on this?

  13. trusktr commented on Apr 7, 2016

    @trusktr
    Author

    Ah, the require.resolve trick makes it absolute, circumventing Webpack from looking for the plugin in the wrong place during build? Good workaround!

  14. kintel commented on Apr 15, 2016

    @kintel

    I believe plugins also suffer from this bug.
    I'm using the ProvidePlugin to bind the three.js library to THREE:

        new webpack.ProvidePlugin({
          THREE: 'three'
        }),
    

    This triggers the Module not found: Error: Cannot resolve module 'three' in ... error in my npm linked modules.

    I did not manage to get the require.resolve trick to work for my babel setup. My workaround is to manually exclude regexps matching my linked modules.
    The trick does work as a workaround for the provide plugin though.

  15. 16 remaining items

  16. wmzy commented on Dec 26, 2017

    @wmzy

    @joaoreynolds
    Let the followSymlinks to be true work for me : node_modules/watchpack/lib/DirectoryWatcher.js#L52

    But why not webpack turn on the followSymlinks option when symlinks is false?

  17. txinfo commented on Jan 16, 2018

    @txinfo

    I found the same as @wmzy after my own lengthy investigation. I have opened an issue on watchpack's issue tracker to make that option configurable: webpack/watchpack#61

  18. nreoch25 commented on Jan 25, 2018

    @nreoch25

    Something I did which isn't the most ideal is just install the babel plugin as a devDependency in the module you are linking into your project. The babel-plugin-dynamic-import-node was the culprit for me so I just installed that. Errors cleared up and I can continue working on my package. Pretty much just a time saver so I can stop looking into this issue and move forward.

  19. mbardelmeijer commented on Feb 2, 2018

    @mbardelmeijer

    Any updates on this issue? 😄

  20. immortal-tofu commented on Mar 16, 2018

    @immortal-tofu

    Just opened a PR on watchpack:
    webpack/watchpack#72

  21. immortal-tofu commented on Mar 23, 2018

    @immortal-tofu

    Add an issue with possible solution feel free to comment or thumbs up this future change.
    #6845

  22. JamesRamm commented on Apr 27, 2018

    @JamesRamm

    My workaround (for webpack 3) which is compatible with webpack-dev-server:

    First set the resolve.modules property:

    resolve: { modules: [path.resolve(__dirname, 'node_modules'] }

    It is important that you supply the abs path to your node_modules

    The, for each babel preset and plugin, explicitly use require.resolve.

    query: {
        plugins: [ require.resolve('babel-plugin-transform-object-rest-spread')],
       presets: [ require.resolve('babel-preset-react') ..etc.. ]
    }

    Note this means you need to explicitly move any babel setup you have in a babelrc file into your webpack config (in my case it was moving the plugins defs)

    This compiles and when I make a change in a yarn link'd module, dev-server is picking up the changes

  23. DaveWelling commented on May 16, 2018

    @DaveWelling

    I need symlinks for Lerna so I cannot do the symlinks:false trick. @JamesRamm solution worked for me.

  24. webpack-bot commented on Jan 15, 2021

    @webpack-bot
    Contributor

    This issue had no activity for at least three months.

    It's subject to automatic issue closing if there is no activity in the next 15 days.

  25. added
    status: stale
    on Jan 15, 2021
  26. alexander-akait commented on Jan 18, 2021

    @alexander-akait
    Member

    I think we can close it, nothing to fix on webpack side, require.resolve is good solution, also can be relevant tot his https://classic.yarnpkg.com/blog/2018/02/15/nohoist/

  27. nico-ledesma commented on May 16, 2022

    @nico-ledesma

    It's May 2022 and this issue was still present for me: babel-loader failed when processing symlinked packages added via npm link.

    Hope this helps!

    TL;DR
    Add this to webpack.config.js when watching:

    resolve.alias = {
        'core-js' : path.resolve( __dirname, './node_modules/core-js' ),
        'regenerator-runtime' : path.resolve( __dirname, './node_modules/regenerator-runtime' )
    } 

    Long explanation

    • I was using npm link in my main project to add symlinked dependencies to other modules.
    • These modules lived together in Lerna monorrepo, initialized by running lerna bootstrap.
    • babel-loader failed because it was looking for core-js and regenerator-runtime in each symlinked modules node_modules dir.
    • I was using webpack@5, @babel/core@7 @babel/preset-env@7 like this:
    [ "@babel/preset-env", {
        "useBuiltIns": "usage",
        "corejs" : 3
    } ]

    Solutions that were not working:

    • Using absolute paths for presets, i.e. require.resolve( @babel/preset-env).
    • Using resolve.symlinks : false in Webpack:
      • This fixes babel-loader problem, but Webpack won't recompile if the symlinked modules change.
      • Using watchOptions.followSymlinks : true doesn't fix this Webpack watch problem either.

    If you can't avoid using babel-loader when watching, defining those aliases does the trick.

  28. alexander-akait commented on May 16, 2022

    @alexander-akait
    Member

    @nico-ledesma Can you open a new issue and describe it, we will investigate it, thanks

  29. tofrankie commented on Jun 21, 2023

    @tofrankie

    👀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: stale

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions