简体   繁体   中英

Webpack 4 not excluding node_modules

My webpack doesn't exclude node_modules folder, i followed this link

Webpack not excluding node_modules

I want to exclude node_modules folder from bundle, and have tried this older way to write: exclude: path.resolve(__dirname, 'node_modules') but still node_modules is imported

But after this exclude, I have error in console:

Uncaught ReferenceError: require is not defined
    at Object.<anonymous> (out.fbd50140277b839fca03.js:1)
    at E (out.fbd50140277b839fca03.js:1)
    at t (out.fbd50140277b839fca03.js:1)
    at Object.<anonymous> (out.fbd50140277b839fca03.js:1)
    at Object.<anonymous> (out.fbd50140277b839fca03.js:1)
    at E (out.fbd50140277b839fca03.js:1)
    at t (out.fbd50140277b839fca03.js:1)
    at Object.<anonymous> (out.fbd50140277b839fca03.js:1)
    at E (out.fbd50140277b839fca03.js:1)
    at out.fbd50140277b839fca03.js:1

And still node_modules folder is imported:

./node_modules/css-loader!./node_modules/sass-loader/lib/loader.js!./css/style.scss 1.36 KiB {0} [built]
    [1] ./node_modules/css-loader/lib/url/escape.js 419 bytes {0} [built]
    [2] ./node_modules/css-loader/lib/css-base.js 2.33 KiB {0} [built]
    [3] ./images/furry.png 82 bytes {0} [built]
    [4] ./images/coin.png 82 bytes {0} [

EDITED

This is weird, I have simply webpack and still it adds me over 30 positions! Look picture below:

Webpack.config.js

const path = require('path');

module.exports = {
    entry: './js/app.js',
    output: {
        path: path.resolve(__dirname, 'js'),
        filename: 'out.js',
    },
    devServer: {
        contentBase: path.resolve(__dirname, 'js'),
        port: 3000,
    },
    module: {
        rules: [
            {
                test: path.resolve(__dirname, 'app.js'),
                loader: 'babel-loader',
                options: {
                    presets: ['env'],
                },
                exclude: path.resolve(__dirname, 'node_modules/')
            }

        ]
    }

}

在此处输入图片说明

I'm currently reading a book and my config is set like this:

 module: {
    rules: [
        {
            test: /\.js$/,
            exclude: /node_modules/,
            use: {
                loader: 'babel-loader'
            }
        }
    ]
},

The only place where I use the path.resolve is on the output key.

Hope this helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM