webpack doesn't bundle node modules

5.1k views Asked by At

I want to require a module from node_modules and I want to bundle it (for test purposes), but Webpack behaves as if it is added to externals.

// no externals or any plugin used
let config = {
    mode: 'none',
    target: 'node',
    entry: {
      output: `/example.js`,
    },
    resolve: {
      extensions: ['.js'],
    },
    output: {
      path: './dist',
    },
  };
// exampl.js
require('path')
// dist/output.js
require('path');

Expected behavior

the node module path to be bundled

actual behavior

Webpack keep require('path');

1

There are 1 answers

0
Lin Du On BEST ANSWER

This is by design. When you set target: 'node' in webpack config, webpack will not bundle the built-ins module of Node.js. path is a built-in module of Node.js, it doesn't come from the node_modules directory.

using node webpack will compile for usage in a Node.js-like environment (uses Node.js require to load chunks and not touch any built in modules like fs or path).

See targets