Babel-loader: Can I use node_modules out of project directory?

Created on 17 Oct 2016  路  5Comments  路  Source: babel/babel-loader

directory like this:

  • project

    • src

    • webpack.config.js

  • node_modules

When I use babel-loader like this;

project/webpack.config.js:

  module: {
        loaders: [
            {
                test: /\.(js|jsx)$/,
                exclude: '/node_modules/',
                loader: 'babel',
                query: {
                    presets: ['es2015'],
                    cacheDirectory: true 
                }
            }
        ]
    },

It give me this error:

ERROR in   Error: Child compilation failed:                                                                                                                                                                                                                   
  Module build failed: Error: Couldn't find preset "es2015" relative to directory "/usr/local/lib/node_modules/webpack/buildin"

Most helpful comment

Most common issue with babel-loader but sadly any of the PRs that try to fix this have been shut down. This is how you work around it.

{
                test: /\.js$/,
                loader: 'babel-loader',
                query: {
                    presets: [
                        require.resolve('babel-preset-es2015')
                    ]
                }
            }

All 5 comments

I think the solution is to define where you can find the preset.
You can do this by setting the resolve loader path. I use the it like below, you may need to alter the path.

webpack: { resolveLoader: { root: path.resolve(__dirname, 'node_modules') } }

resolve: { 
        root: [
            path.resolve('../node_modules')
        ] 
    }

@martinbroos Thanks for reply.
but still not work. same error.

Most common issue with babel-loader but sadly any of the PRs that try to fix this have been shut down. This is how you work around it.

{
                test: /\.js$/,
                loader: 'babel-loader',
                query: {
                    presets: [
                        require.resolve('babel-preset-es2015')
                    ]
                }
            }

@briandipalma it seems that "require.resolve" cannot work around it either in webpack 2.2.1, so Did someone found solution for webpack 2?

I'm triaging old issues on babel-loader. Since this has been inactive for ages, I'm going to close it. Feel free to re-open if there's still something to discuss, but I'm assuming at this point it's been too long to address in a useful way.

Was this page helpful?
0 / 5 - 0 ratings