[javascript] Webpack not excluding node_modules

I'm using webpack for a Node framework that I'm building (though I should probably use gulp, admittedly). When I include the EJS module, webpack includes it in the compiled source, even though I explicitly tell it to exclude the node_modules dir.

module.exports = {
    context: __dirname,
    target: 'node',
    // ...
    output: {
        libraryTarget: 'commonjs'
        // ...
    },
    module: {
        loaders: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel-loader?{ "stage": 0, "optional": ["runtime"] }'
            }
        ]
    }
};

As you can see, I have a test for JS files, and I tell it to exclude node_modules; why is it ignoring my exclude?

This question is related to javascript webpack

The answer is


Try use absolute path:

exclude:path.resolve(__dirname, "node_modules")

If you ran into this issue when using TypeScript, you may need to add skipLibCheck: true in your tsconfig.json file.


try this below solution:

exclude:path.resolve(__dirname, "node_modules")

This worked for me:

exclude: [/bower_components/, /node_modules/]

module.loaders

A array of automatically applied loaders.

Each item can have these properties:

test: A condition that must be met

exclude: A condition that must not be met

include: A condition that must be met

loader: A string of "!" separated loaders

loaders: A array of loaders as string

A condition can be a RegExp, an absolute path start, or an array of one of these combined with "and".

See http://webpack.github.io/docs/configuration.html#module-loaders