javascriptnode.jsecmascript-6

What does the plus (+) symbol mean in an ES6 module specifier?


What does the + symbol denote in this import declaration?

import app from '+/api';

as opposed to just:

import app from '/api';

The import snippet is from a Node app using es6 which is transpiled with Babel.

./api/index.js exports an Express instance using export default app;

I have also seen this syntax, using a tilde:

import app from '~/api';


Solution

  • The structure of the module identifier or how it is supposed to be interpreted is not part of the ECMAScript specification.

    The meaning of the module identifier is defined by the module loader, i.e. the part of the system that actually takes the value and uses it to find the corresponding module.

    Of course Node.js has a module loader. It is Node.js that defines that module identifiers are either names of shared modules or paths to files.

    However, the + (or ~) here doesn't have any meaning in Node.js. I can only assume that the project might be using a module bundler or something similar which is processing module files and its dependencies. It is that part of the system that defines the meaning of +.


    I think this is a point that many people don't understand: While ES6 defines a standard syntax for declaring dependencies, the resolution of the module identifier to the actual module is not part of the specification and may differ greatly between runtimes/environments. Given Node.js' popularity most will likely be compatible with the CommonJS module system (which is what Node uses), but again, there is no standard for that.