javascriptecmascript-6systemjses6-module-loadertraceur

ES6 + SystemJs - why do I have to use the .js extension to import es6 module?


Why do I have to use the .js extension to import es6 module with systemjs? For instance:

import { multiplier } from "adder.js"; // ok
import { double, square } from 'modules'; // error

var timesTwo = multiplier(2);

console.log(timesTwo(4));

The error message:

GET http://local-host/projects/es6/src/modules 404 (Not Found)Z @ system.js:4(anonymous function) @ system.js:4(anonymous function) @ system.js:4(anonymous function) @ system.js:5(anonymous function) @ system.js:5(anonymous function) @ system.js:5(anonymous function) @ system.js:5(anonymous function) @ system.js:5(anonymous function) @ system.js:5(anonymous function) @ system.js:4 system.js:4 Uncaught (in promise) Error: Error: XHR error (404 Not Found) loading http://local-host/projects/es6/src/modules(…)

This is my HTML with system.js:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>ES2015 Module Example</title>
</head>
<body>
    <script src="lib/system.js"></script>
    <script>
        System.config({
            "baseURL": "src",
            // 'plugin-babel' or 'traceur' or 'typescript'
            transpiler: 'plugin-babel',
            // or traceurOptions or typescriptOptions
            babelOptions: {

            },
            map: {
                'traceur': './lib/traceur.min.js',
                'plugin-babel': './lib/plugin-babel/plugin-babel.js',
                'systemjs-babel-build': './lib/plugin-babel/systemjs-babel-browser.js'
            }
          }
        });
        System.import("app.js");
    </script>
</body>
</html>

Any ideas what have I missed?


Solution

  • Because system js doesn't append .js to the module name. Some module loaders / bundlers (Node, webpack, etc) are doing this, but there is no standard that specifies that this has to be done.

    There is a configuration option for this, but it's going to be deprecated.