Search code examples
javascriptnode.jsecmascript-6aureliacommonjs

EcmaScript 6 module requiring, how does it work?


So I know how to require and export modules in ES6. But for frameworks like Aurelia, the docs say that you require aurelia like so:

import {LogManager} from 'aurelia-framework';

Do I have to place a JS file named aurelia-framework in the folder where the JS file I'm executing it from resides, or does the import function work similiar to the require function in NodeJS/CommonJS?


Solution

  • According to this article ES6 modules spec only deals with loading modules that are present in the file path. Downloading these files (via NPM or by other means) is outside of scope of ECMAScript 6 modules spec. Nothing is said in the spec about supporting npm package includes (traversing the directory structure down to the /, one directory at a time, looking for a package.json file and then searching within the node_modules directory where package.json file is found). So while the import syntax is similar to commonJS style, the whole magic of looking for modules in the node_modules directory is not included.

    So for your example to work, aurelia-framework must be a javascript file somewhere in your file system and it should contain an exports statement.

    import {LogManager} from 'aurelia-framework'; // ./aurelia-framework.js
    import {LogManager} from '../libs/aurelia-framework'; // ../libs/aurelia-framework.js