Repository navigation
require not working in es6 modules in NodeJS #427
Description
Activity
The module loader on its own does not support CommonJS modules. You'd need to use a loader like SystemJS for that.
Thanks for the reply. Do you know what I might have been doing previously to have this working in the past? I've got code in production with 0.15.0 now where the the require calls are behaving. I had assumed that it was previously working because any require calls were being processed by node rather than the loader, and that something in the loader or transpiling is now interfering with that, but it sounds like this might not be the case. I had also tried with Systemjs, but got the same require not defined error (I assume you mean I need to do explicit imports with systemjs replacing the existing require calls, which I haven't tried yet - I thought there were issues with the default setup and grabbing stuff directly from node_modules that required custom loading at the moment?).
Regards,
Richard.I found a solution for my issue, but I'm still not very clear on the underlying cause.
Just to be clear, I haven't been trying to load CommonJS modules with the es6-module-loader/SystemJS, but rather with the native node require from within transpiled es6 modules (which I have been trying to laod with es6-module-loader/SystemJS). It seems the native node 'require' function is not defined inside the transpiled code. I'm not really sure why, as it had been in previous versions (again not clear if this is the result of upgrading Babel or es6-module-loader).
In any case, if I add this:
System.myrequire = require;
At the top level after first loading the es6-module-loader (with Nodes require).Then changing all my var foo = require('foo'); in my es6 modules to:
var foo = System.myrequire('foo'), everything works again.So are there any suggestions on what might be happening to remove the native 'require' from the scope of the transpiled module? (sorry It is starting to look more like I may have posted this in the wrong project, and this is more likely a transpiling issue).
Writing
requireinside of ES modules is not encouraged. You can create a global layer to the loader that retains the standard NodeJS require as this project is really designed to be extended with custom loader implementations and not typically used on its own for more than experimentation.Yes, I figured it might not be :-) Is the loader deliberately scrubbing require from the module context to discourage its use, or is there something else going on that I'm missing? Does a loader exist at the moment that can load CommonJS modules from within NodeJS such that it handles loading out of node_modules with minimal configuration? I'm guessing your suggestion implies that such a custom loader doesn't exist yet, but Systemjs has such a broad range of useful features, I sometimes have trouble assessing whether it can or can't do something :-) Anyway, thanks for your patience, I'm new to both CommonJS and Es6 modules so my questions are probably looking quite obtuse :-)
This project will not alter the global
requirebut SystemJS will do that to ensure module formats like AMD and globals are interpreted correctly.
I'm not sure if this is a module loader issue, or a transpiling issue, but since upgrading to the latest version (from 0.15.0) I have not been able to get require to work from my dynamically transpiled modules when running in NodeJS. I've also upgraded Babel (5.0.0 to 5.8.21) at the same tiime, so apologies if I'm raising this in the wrong place.
Previously I was able to get code like this to work:
This is a module loaded with an es6 import such as:
import { log } from "server-modules/logger/logger.js";
I needed to make a few changes such as installing bable-core and appending .js to my module paths, but the setup should be otherwise the same as my old working code. I setup the es6 module loader like this:
When running the app I get errors like this:
I can see why require would not be defined in a browser environment, but I'm unclear why it is not available in a NodeJS environment. Removing the require from logger.js moves the error along to another file using require. All of this worked ok in 0.15.0.
Any idea what I'm doing wrong here?
Regards,
Richard.