Search code examples
javascriptsystemjs

systemjs - "system.import" fails if file contains "use strict"


I would like to use systemjs as a module loader in a new project. Everything works fine until I add 'use strict'; to the top of the file which should be loaded.

script.js

System.import('loadme.js').then(function(m) {
    console.log('loaded');
    console.log(app);
})

loadme.js

'use strict'; //if I remove this line the import works fine
var app={
  version:'0.0.0',
  name:'just a test'
};

I have a plunkr here https://plnkr.co/edit/bhSTkcZw9XaKszXuIYZQ


Solution

  • It's expecting a module to be passed back with the data, and not a global variable (see documentation on strict mode globals).

    Here's something you could do, if you just want it to work: https://plnkr.co/edit/pVKqfGkcCagyLixtmziB?p=preview

    'use strict';
    
    var app = {
      version: '0.0.0',
      name: 'just a test'
    };
    
    module.exports = app;
    
    /*
      You can also do
    
      module.exports = {
        app: app,
        foo: foo,
        bar: bar
        .
        .
        .
      }
    
      and then in your script.js have module.app, module.foo
    
    */