Does rollup bundle node_modules into bundle.js?

Viewed 13722

I am testdriving rollupjs to package a node app into a bundle.js and am confused.

Does rollup support bundling a full node app (including node_modules), or just the js files that are part of your project?

I have a standard node project (1 index.js, thousands of files in node_modules) and would like just one bundle.js. I tried:

rollup.config.js:

import commonjs from 'rollup-plugin-commonjs';
import nodeResolve from 'rollup-plugin-node-resolve';

export default {
entry: 'index.js',
dest: 'bundle.js',
format: 'iife',
plugins: [

    commonjs({
        // non-CommonJS modules will be ignored, but you can also
        // specifically include/exclude files
        include: 'node_modules/**',  // Default: undefined

        // if true then uses of `global` won't be dealt with by this plugin
        ignoreGlobal: false,  // Default: false

        // if false then skip sourceMap generation for CommonJS modules
        sourceMap: false,  // Default: true
    }),

    nodeResolve({
    jsnext: true,
    main: false
    })
]
};

Whatever I try rollup turns this index.js:

module.exports = require('dat-node') // 88 MB node_modules

with this command:

rollup index.js --format iife --output dist/bundle.js -c

to this bundle.js without adding anything from node_modules:

(function () {
'use strict';

module.exports = require('dat-node');

}());

And I have tried:

  • swapping plugin sequence
  • all different command line options
  • different formats
  • different config file settings

Now I am thinking, maybe I understand rollup incorrectly and it does not support what I want. Help much appreciated!

2 Answers

I was facing the same problem and searched a lot but the answers were mostly of the old syntax. After some searching, this is what worked for me. I am not 100% sure that this is the best way to do it.

It would be very helpful if someone more knowledgeable on rollup would verify it

So what i found does the trick is adding the modulesOnly option to the nodeResolve plugin something like this :

import nodeResolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";

export default {
  input: "src/index.js",
  output: [
    {
      format: "cjs",
      file: "dist/index.cjs.js",
    },
    {
      format: "esm",
      file: "dist/index.esm.js",
    },
  ],
  plugins: [
    commonjs(),
    nodeResolve({ modulesOnly: true }),
  ],
};

Related