Webpack 4不排除node_modules

时间:2018-09-20 16:52:09

标签: javascript webpack

我的webpack并不排除node_modules文件夹,我点击了此链接

Webpack not excluding node_modules

我想从包中排除node_modules文件夹,并尝试使用这种较旧的写方法:排除: path.resolve(__ dirname,'node_modules'),但仍然导入了node_modules

但是在排除之后,控制台出现错误:

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

仍然导入了node_modules文件夹:

./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} [

已编辑

这很奇怪,我只使用了webpack,仍然为我增加了30个职位!看下面的图片:

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/')
            }

        ]
    }

}

enter image description here

1 个答案:

答案 0 :(得分:2)

我目前正在读一本书,其配置设置如下:

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

我唯一使用path.resolve的地方是在输出键上。

希望这会有所帮助:)