最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Webpack 4 not excluding node_modules - Stack Overflow

programmeradmin2浏览0评论

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

        ]
    }

}

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

        ]
    }

}

Share Improve this question edited Sep 20, 2018 at 19:06 Freestyle09 asked Sep 20, 2018 at 16:52 Freestyle09Freestyle09 5,53810 gold badges58 silver badges92 bronze badges 9
  • review your 'target' config re: stackoverflow./questions/44351310/… – Robert Rowntree Commented Sep 20, 2018 at 17:00
  • Still nothing... i edited to web, only when i remove line nodeExternals it is working but not removing node_modules – Freestyle09 Commented Sep 20, 2018 at 17:09
  • retry module.rules.exclude using exclude: path.resolve(__dirname, "node_modules"), – Robert Rowntree Commented Sep 20, 2018 at 17:47
  • Is the node_modules folder in the same folder as your Webpack config? – Adam Commented Sep 20, 2018 at 18:17
  • Yes, is in the same directory – Freestyle09 Commented Sep 20, 2018 at 18:37
 |  Show 4 more ments

1 Answer 1

Reset to default 4

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 :)

发布评论

评论列表(0)

  1. 暂无评论