6

I would like to use the system.js module manager with TypeScript. My plan is to run typescript in node

The problem is that, if i mark my Main.js (generated from Main.ts) file as the entry point, it will crash with this error:

ReferenceError: System is not defined

And it's normal, because my generated code start like this: System.register(["./Apple"], function(exports_1) {

And for sure, there is no System class in node by default. So how to make typescript or node to load the system.js module?

I can create an extra js file to load it with require, but i'm looking for a native solution inside typescript

Thanks!

3 Answers 3

6

You should tell the compiler to export your modules into commonjs and not systemjs. Node is using commonjs as its native module system.

Sign up to request clarification or add additional context in comments.

5 Comments

But i think system.js support node according to system.js documentation: NodeJS To load modules in NodeJS, install SystemJS with: npm install systemjs We can then load modules equivalently to in the browser: var System = require('systemjs'); System.transpiler = 'traceur'; // loads './app.js' from the current directory System.import('./app').then(function(m) { console.log(m); }); If using TypeScript, set global.ts = require('typescript') before importing to ensure it is loaded correctly.
This is a way to load node modules into the browser, just like browserify... this will not help you load modules in node. NodeJS is already capable of loading modules, they are using commonjs. and TypeScript can transpile the modules into commonjs.
I do not agree with you. You can use system.js in node enviroment too, and i was successfully managed it. You just have to load it: require("systemjs") and after you can use all the features of systemjs. My problem is that for this i need to add an extra init.js file to my project, but this way for some reason i cannot debug my typescript sources (when i use the normal commonjs system it works)
Why do you insist on using systemjs if commonjs works for you?
Just to add to the answer, here is how it should be done if you're using tsconfig.json: inside compilerOptions section, replace "module": "system" with "module": "commonjs". This way, when running tsc, it'll compile it to require instead of System.register. Also, If you use TypeScript for the client side as well, you may get an error: ReferenceError: require is not defined, in this case you should remove the “format: ‘register’ “ from your System.Config.
2

I might not understand correctly, but have you tried to follow the node.js section on system.js GitHub Readme (https://github.com/systemjs/systemjs)?

$ npm install systemjs

then:

var System = require('systemjs');

// loads './app.js' from the current directory
System.import('./app').then(function(m) {
  console.log(m);
});

Hope that helps.

Comments

2

Are you using tsconfig file? You have add the module:'system' I was facing a similar problem, this fixed it. Also ensure that the system.src.js is at top of the scripts.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.