How can I exclude node_modules from a webpack build?

See original GitHub issue

I have a specific question below, but I think the more general question is, how can I control what webpack adds to my build automatically (especially without depending on external modules)?

I have a simple 10-line js example I’m trying to use with webpack. When I run locally with webpack-dev-server, I see that bundle.js is >300KB. I assume this is because node_modules is being included, correct?

The file size of dist/bundle.js is ~2KB, so this is apparently being done at runtime.

I’ve added babel-loader and the necessary rules as specified on the package’s repo README to my webpack config. I don’t receive any errors when running locally, so I assume that the configuration is now correct.

However, my bundle.js is still showing 300KB+ in dev tools. How can I get rid of this stuff? This is simply a short example with plain JS inserting a single DOM element, so I (obviously) don’t need all the extra stuff.

None of the existing SO posts or blog articles I’ve been able to locate have been helpful (they all use some variation of exclude, and I’ve tried them all, and have seen config errors in cases where they weren’t specified correctly, so that’s why I believe it’s specified correctly now).

(Posting here because webpack questions are no longer answered on SO, which leaves no other resources AFAIK.)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

34reactions
sokracommented, Sep 12, 2017

It looks like you just deleted/ignored our lovely crafted issue template. It was there for good reasons. Please help us solving your issue by answering the questions asked in this template. I’m closing this. Please open a new issue with filled issue template. Also make sure your issue is not a question. Questions should be posted on Stack Overflow.

20reactions
hutch120commented, Feb 21, 2018

The very popular module webpack-node-externals appears to address this issue: https://www.npmjs.com/package/webpack-node-externals

Read more comments on GitHub >

github_iconTop Results From Across the Web

babel-loader-exclude-node-modules-except - npm
Creating a regular expression for excluding node_modules from babel transpiling except for individual modules. Latest version: 1.2.1, ...
Read more >
JavaScript : Webpack not excluding node_modules - YouTube
JavaScript : Webpack not excluding node_modules [ Gift : Animated Search Engine : https://bit.ly/AnimSearch ] JavaScript : Webpack not ...
Read more >
When using webpack in my React project, should I exclude ...
Hi, I'm trying to use Webpack to bundle my app. When I do npm run webpack, I get like 100 errors regarding packages...
Read more >
Webpack not excluding node_modules - SyntaxFix
The Best Answer is · Add target: 'node' to your webpack.config.js . This will exclude native node modules (path, fs, etc.) from being...
Read more >
babel-loader-exclude-node-modules-except - Yarn
Creating a regular expression for excluding node_modules from babel transpiling except for individual modules. webpack, babel-loader, exclude, node_modules ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found