jspm does not seem to modify require() in NWJS

24 views
Skip to first unread message

Rahul Gupta

unread,
Mar 13, 2016, 3:10:51 PM3/13/16
to jspm
I am not sure of this is a bug or a feature, so I thought I will check first!

I had accidently not changed a require() to System._nodeRequire() in a invoking a node library outside JSPM in my NWJS app. Let us call this library 'foo'. I expected that an error since there is no information about this library 'foo' in jspm config or jspm dependencies in package.json and SystemJS is supposed to hijack all module loading. However, the library 'foo' was still loaded from node_modules folder when I used require('foo') instead of System.nodeRequire('foo').

Using jspm 16.31, nwjs 0.13-rc2, node 5.5, windows 10.  

Guy Bedford

unread,
Mar 14, 2016, 2:32:00 AM3/14/16
to Rahul Gupta, jspm
It entirely depends on what module format the file is being detected as.

If the file is being treated as a CommonJS or AMD file, then the `require` function will be provided accordingly.

If the file is being treated as a global or ES6 module file (or System.register), then the global `require` function is left unchanged.

Typically in NodeJS environments, `require` is locally-scoped and not a global. I haven't checked Node Webkit recently, but I assume it defines the `window.require` to be the CommonJS require then?

--
You received this message because you are subscribed to the Google Groups "jspm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jspm-io+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rahul Gupta

unread,
Mar 14, 2016, 7:08:00 AM3/14/16
to jspm, rahulg...@gmail.com
Thanks for the reply. I am sorry but this only confuses me further.

So let me get this straight:

If I use ES6 syntax to import a lib with an name only eg `import bar from 'foo'`, that is resolved by JSPM and any require inside that library 'foo' will be further resolved by JSPM.

But if my code contains a require eg `require('baz')` (or I use ES6 syntax to import something locally and that contains a require eg `import blah from './foo'` and inside `./foo` we have `module.exports = require('baz')`  ), the module 'baz' will always be resolved as commonJS?

Am I describing the behaviour correctly?  
     

Suppose I have an ES6 module in which I call a CommonJS from node_modules and another where JSPM includes a npm modules.  

Guy Bedford

unread,
Mar 14, 2016, 10:01:42 AM3/14/16
to Rahul Gupta, jspm
Yes exactly, SystemJS will take over the entire loading process. Typically in NodeJS 'require' is only defined in modules as a locally scoped binding and not a global. So this works fine. Perhaps confirm if 'global.require' is set in Electron, as that would fully explain this?

We can possibly consider options for this conflict in Electron then further.

Rahul Gupta

unread,
Mar 14, 2016, 12:29:52 PM3/14/16
to jspm, rahulg...@gmail.com
Well, I am on nwjs (0.13-rc2) not electron (though I presume it would be similar) and global.require is set here.

And indeed if I use global.require inside JSPM package, then package inside is looked for in node_modules. I tested this by changing the require inside a package file in the jspm_packages folder to global.require. While the loading worked, SystemJS was unhappy about dependencies inside the library which I am guessing happens because now everything inside works the CommonJS way and not thru SystemJS?

I would really appreciate if there is a clarity on dealing with browser / node dichotomy scenarios in a consistent way. Thanks.

Guy Bedford

unread,
Mar 15, 2016, 12:10:01 PM3/15/16
to Rahul Gupta, jspm
Yes this is certainly a bug then, and was an unnoticed regression recently. I've created https://github.com/systemjs/systemjs/issues/1161 to track further.
Reply all
Reply to author
Forward
0 new messages