systemjs: Can't load Node core modules

I’m probably missing something basic, but I can’t seem to load Node core modules such as fs, path, http, etc. Here’s what I’m doing.

// index.js
var System = require('systemjs');
System.import('./app')
    .then(function (m) {
        console.log(m);
    }, function (e) {
        console.log(e);
    });
// app.js
var fs = require('fs');

This produces the following output:

{ [Error: ENOENT, open '/Users/orand/Projects/SystemJSTest/fs.js']
  errno: 34,
  code: 'ENOENT',
  path: '/Users/orand/Projects/SystemJSTest/fs.js' }

How can I tell SystemJS to not treat Node core modules as if they are local files?

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 25 (8 by maintainers)

Most upvoted comments

Perhaps the “nodeRequire” loader could also be activated by a System.config setting.

Yeah, I’m not sure if this is really a good solution either. It lets me make progress for now, but feels like a hackish temporary solution, which is why I’d rather have a way to load these with require() instead of nodeRequire(), perhaps through meta, config, or a loader fallback when running on NodeJS.

I’m still very new to the ES6 world and haven’t read any specs and have only taken a cursory glance at the code base, so it’s quite likely this isn’t the “compatible” way to do things. For instance, I’m guessing it completely bypasses the loader tracing functionality.

Yes, I’m using SystemJS in a NodeJS project to allow ES6 module support. However, my goal is a bit broader than that. I’d like to implement shared business logic in ES6 modules that can be run both in the browser (including PhoneGap) and on the server. Some of the dependencies (data storage, logging, etc.) may have different implementations in the browser vs. on the server, but the core business logic would ideally live in a single set of ES6 modules.

Yes! The nodeRequire technique works great! I simply added the following to the very last else block of polyfill-wrapper-end.js (or dist/system.js if you don’t want to rebuild):

    global.nodeRequire = function (modulePath) {
      return require(modulePath);
    };

Yes it would still be nice to be able to add something to System.config to tell it which modules should be loaded with nodeRequire so I don’t have to use the nodeRequire syntax in my code, but I’m happy to be making progress with this technique for now.

Thank you so much for pointing me in the right direction. This opens up a whole new world of possibilities for me.