Can system.js replace require.js
Asked Answered
B

2

12

I'm using requirejs in a large project. This project will soon be upgraded to angular2.

Angular2 uses system.js, so I'm thinking of switching to system.js too. Should I be able to remove the reference to the requirejs library and include system.js instead and expect it to work, or is there something I don't understand here?

I tried by just removing the require.js file and adding the system.js file instead, but I get error messages saying define is not defined. Can you help? Will I need require.js in addition to system.js?

Beaulieu answered 25/10, 2015 at 17:47 Comment(2)
Does Angular 2.0 still use SystemJS or is it also compatible with Webpack?Strikebreaker
It is compatible with bothBeaulieu
M
8

I just switched to system.js too. You need to replace your require.js with system.js and add simple script tag. So it should look like this:

<script src="~/lib/system.js/dist/system.js" type="text/javascript"></script>
<script>  
    // set our baseURL reference path
    System.config({
        baseURL: '/app'
    });
    System.import('startup.js');
</script>
Machellemachete answered 26/10, 2015 at 9:34 Comment(6)
And you got it working without changing anything in the existing files that uses requirejs from before?Beaulieu
Yeah, which version of system.js do you use?Machellemachete
I got it working. The problem was that the define function and the require function was not available on the global scope. I had to do like this: window.define = System.amdDefine; window.require = window.requirejs = System.amdRequire;Beaulieu
what do you do if you get system.js:4 Uncaught (in promise) Error: (SystemJS) ReferenceError: require is not defined(…)Serafinaserafine
Thanks for the window.require = window.requirejs = System.amdRequire; recommendation. But how to use my old requirejs.config({...})?Housewife
@Housewife this might be usefull: github.com/systemjs/systemjs/blob/master/docs/… and github.com/systemjs/systemjs/blob/master/docs/…Goglet
S
3

in addition to above steps set

System.defaultJSExtensions = true;

by default systemjs is not adding extension .js

Saraband answered 23/8, 2016 at 13:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.