mstruebing
mstruebing

Reputation: 1792

Webpack isn't excluding node_modules

I have the following directory structure:

- webpack.config.js - src - index.js -dist - index.js

A webpack config:

module.exports = {
    module: {
      loaders: [{
        test: /\.js$/,
        include: __dirname + 'src',
        exclude: __dirname + 'node_modules',
        loader: "babel-loader"
      }],
    },
    resolve: {
      extensions: ['.js']
    },
    output: {
      path: __dirname + '/dist',
      filename: 'index.js'
    }
};

But if I import request in my src/index.js (const request = require('request');) I'm getting errors from node_modules.

How can I avoid this? I've thought I've excluded node_modules.

Just for completion my package.json:

{
  "name": "leo-trans",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "watch": "webpack --watch",
    "start": "node dist/index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "preferGlobal": true,
  "bin": {
    "leo-trans": "./dist/index.js"
  },
  "devDependencies": {
    "babel-core": "^6.3.26",
    "babel-loader": "^6.2.1"
  },
  "dependencies": {
    "cheerio": "^0.19.0",
    "request": "^2.67.0"
  }
}

Upvotes: 2

Views: 3747

Answers (1)

alexpods
alexpods

Reputation: 48505

You've not excluded node_modules, because __dirname doesn't contain trailing slash. Try to use path.resolve:

const path = require('path');

module.exports = {
  module: {
    loaders: [{
      test: /\.js$/,
      include: path.resolve(__dirname, 'src'),
      exclude: path.resolve(__dirname, 'node_modules'),
      loader: "babel-loader"
    }],
  },
  // ...
}

Upvotes: 2

Related Questions