Rollup.js unresolved dependencies
Asked Answered
G

2

25

I am trying to incorporate rollup.js into a project. Currently I am getting the warnings provided below in the console (unresolved dependencies) and I am not sure why or how to fix it:

'fs' is imported by node_modules\filereader\FileReader.js, but could not be resolved – treating it as an external dependency

'fs' is imported by  commonjs-external:fs, but could not be resolved – treating it as an external dependency

preferring built-in module 'punycode' over local alternative at 'C:\Users\Ryan\OneDrive\Projects\Custom Coding\Zapier\Ryan Test\node_modules\punycode\punycode.js', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning

preferring built-in module 'punycode' over local alternative at 'C:\Users\Ryan\OneDrive\Projects\Custom Coding\Zapier\Ryan Test\node_modules\punycode\punycode.js', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning

Here is the test.js script requiring FileReader and https:

var FileReader = require('filereader');
var https = require('https');

Finally the rollup.config.js file which executes creating the bundle:

var rollup = require('rollup');

var commonjs = require('rollup-plugin-commonjs');
var nodeResolve = require('rollup-plugin-node-resolve');
var globals = require('rollup-plugin-node-globals');
var builtins = require('rollup-plugin-node-builtins');

// build bundle
rollup
  .rollup({
    entry: 'test.js',
    plugins: [
      nodeResolve(),
      commonjs(),
      globals(),
      builtins()
    ]
  })
  .then(bundle => bundle.write({
    dest: 'rollupBundle/bundle.js',
    format: 'cjs'
  }))
  .catch(err => console.log(err.stack));
Gamboge answered 5/1, 2017 at 22:9 Comment(0)
C
24

The CLI will generate more informative warnings — if you update your config file to use the standard form, then you can use rollup -c instead and it will often give you a URL to help diagnose issues.

Here's a config file with the necessary changes to squelch those warnings:

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

export default {
  entry: 'test.js',
  dest: 'rollupBundle/bundle.js',
  format: 'cjs',
  external: [ 'fs' ], // tells Rollup 'I know what I'm doing here'
  plugins: [
    nodeResolve({ preferBuiltins: false }), // or `true`
    commonjs(),
    globals(),
    builtins()
  ]
};

UPDATE: The "official" Rollup plugins are now under the @rollup namespace on npm, if you install the two versions mentioned above you will get an "npm WARN deprecated" message, so instead install the newer versions instead:

npm install @rollup/plugin-commonjs --save-dev
npm install @rollup/plugin-node-resolve --save-dev

then use them like this:

import commonjs from '@rollup/plugin-commonjs';
import { nodeResolve } from '@rollup/plugin-node-resolve';
Classicism answered 6/1, 2017 at 13:1 Comment(6)
any ideas why I would be getting an error saying that https is not defined based on the above? This is not a rollup error, but an error coming from the environment I am trying to deploy from.Gamboge
hard to say without a repro to investigate, sorryClassicism
I added a small update, as the "official" Rollup plugins are now under the @rollup namespace, but the answer is still valid as of today (august 2020)Crampon
The order of plugins is critical. Any change produces amazing errorsTeheran
I'm still getting 500 internal server errors on my child_process importGemot
From my understanding, this just adds polyfills for node libraries. Any suggestions for actually keeping the imports to the real standard libraries in the case of electron, where node builtins are available via require/import?Radiation
U
0

I had a similar issue. What worked for me was as follows.

According to Rollup docs you can add the /node_modules/ to your externals. This way, any module imported will be treated as external. To do this.

  1. First install @rollup/plugin-node-resolve via
npm install @rollup/plugin-node-resolve --save-dev
  1. Then update your rollup config with the following
import { nodeResolve } from "@rollup/plugin-node-resolve";
...

export default {

  external: [/node_modules/], // <-- Add `node_modules` as regex
  plugins: [
    nodeResolve(),  // <-- Resolver to handle node modules
  ],
};

Undercover answered 25/4, 2024 at 8:1 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.