Systemjs: `cannot find module "vm"` when using SystemJS > 0.21.0 with webpack

Created on 4 May 2018  路  4Comments  路  Source: systemjs/systemjs

When using webpack to package systemjs versions > 0.21.0, I get the following warning during webpack bundling:

    WARNING in ./node_modules/systemjs/dist/system.js
    4:36270-36272 Critical dependency: require function is used in a way in which dependencies cannot be statically extracted
     @ ./node_modules/systemjs/dist/system.js
     @ ./index.js

attempting to call SystemJS.import() in the packaged module results in:

Error: Cannot find module "vm".
  Instantiating http://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.11/p5.js
  Loading http://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.11/p5.js

I can reproduce the issue with systemjs 0.21.1, 0.21.2, and 0.21.3. Version 0.21.0 seems to work just fine.

I've put together a minimal reproduction of the issue in this repo: https://github.com/rdeits/systemjs-issue-repro . You can try editing the systemjs version in package.json to verify that the issue goes away with 0.21.0.

Is there anything else I can do to help debug this?

Most helpful comment

For posterity's sake, I ran into this issue as well and was able to fix it by monkey patching SystemJS's _nodeRequire to be undefined.

if (SystemJS._nodeRequire) {
  // Fixes https://github.com/systemjs/systemjs/issues/1817
  // SystemJS basically will try to detect if it should use NodeJS's built-in
  // require to load things if it can, but because Webpack (sometimes, depending
  // on the settings/target I think?) defines `require`, SystemJS can get
  // confused. When it gets confused, it tries to use Webpack's require (which
  // then complains since the things we're trying to load dynamically weren't
  // loaded by Webpack). This is our hack to un-confuse SystemJS.
  console.warn("Monkey-patchings SystemJS._nodeRequire to undefined.");
  SystemJS._nodeRequire = undefined;
}

All 4 comments

Perhaps try using the SystemJS production loader instead here and see if that works better?

Thanks! The production loader works fine with 0.21.3, although I'm not sure we want to give up all the module support that the dev loader offers.

By the way, this is the line where webpack loses track of the required dependencies: https://github.com/systemjs/systemjs/blob/6fdd0a95dc5a6a33920e571a1d1890430853da70/src/systemjs-loader.js#L79

For posterity's sake, I ran into this issue as well and was able to fix it by monkey patching SystemJS's _nodeRequire to be undefined.

if (SystemJS._nodeRequire) {
  // Fixes https://github.com/systemjs/systemjs/issues/1817
  // SystemJS basically will try to detect if it should use NodeJS's built-in
  // require to load things if it can, but because Webpack (sometimes, depending
  // on the settings/target I think?) defines `require`, SystemJS can get
  // confused. When it gets confused, it tries to use Webpack's require (which
  // then complains since the things we're trying to load dynamically weren't
  // loaded by Webpack). This is our hack to un-confuse SystemJS.
  console.warn("Monkey-patchings SystemJS._nodeRequire to undefined.");
  SystemJS._nodeRequire = undefined;
}
Was this page helpful?
0 / 5 - 0 ratings