Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 (ECMAScript 2015) modules: import index.js

Looking on the Internet I'm confused with the special "index.js" module file.

Using babelJS + Node.js or Browserify/Webpack I can import an "index.js" module inside a "libs" directory using import myLib from "./libs" (i.e., omitting the /index or /index.js part).

Is the "index.js" module resolution (specifying the containing folder) supported by the ES6 (ECMAScript 2015) modules official standard? Or is it just "custom" Node.js /CommonJS transpiling behaviour?

Will it be possible to omit the /index|/index.js part of the import in all browsers as well (when modules will be supported on all browsers)?

like image 485
Zorgatone Avatar asked May 11 '16 10:05

Zorgatone


People also ask

Can you import modules in JavaScript?

You can import modules into a file in two ways, based on if they are named exports or default exports. Named exports are constructed using curly braces. Default exports are not.

How do I import ES6 into browser?

Safari, Chrome, Firefox and Edge all support the ES6 Modules import syntax. Here's what they look like. Simply add type="module" to your script tags and the browser will load them as ES Modules. The browser will follow all import paths, downloading and executing each module only once.

Does ES6 support import?

ES6 modules are automatically strict-mode code, even if you don't write "use strict"; in them. You can use import and export in modules.


2 Answers

Is the "index.js" module resolution (specifying the containing folder) supported by the ES6 (ECMAScript 2015) modules official standard?

No. ES2015 doesn't contain anything about the module loader or how to interpret module identifiers.


Or is it just "custom" Node.js/CommonJS transpiling behaviour?

Yes. You can read about the module resolution algorithm in the documentation. The relevant part:

require(X) from module at path Y
1. If X is a core module,
   a. return the core module
   b. STOP
2. If X begins with './' or '/' or '../'
   a. LOAD_AS_FILE(Y + X)
   b. LOAD_AS_DIRECTORY(Y + X)
3. LOAD_NODE_MODULES(X, dirname(Y))
4. THROW "not found"
[...]
LOAD_AS_DIRECTORY(X)
1. If X/package.json is a file,
   a. Parse X/package.json, and look for "main" field.
   b. let M = X + (json main field)
   c. LOAD_AS_FILE(M)
2. If X/index.js is a file, load X/index.js as JavaScript text.  STOP
3. If X/index.json is a file, parse X/index.json to a JavaScript object. STOP
4. If X/index.node is a file, load X/index.node as binary addon.  STOP

Will it be possible to omit the /index|/index.js part of the import in all browsers as well (when modules will be supported on all browsers)?

Hopefully browser implementations will aim for maximum compatibility with existing module loaders, but for now we don't know. Maybe it doesn't have anything to do with the browser either, but with how the server resolves module identifiers. I confess that I haven't followed the development lately, so any other insights are much appreciated :)

like image 54
Felix Kling Avatar answered Oct 05 '22 06:10

Felix Kling


Node.js team is working on a workaround : https://nodejs.org/api/esm.html#esm_customizing_esm_specifier_resolution_algorithm

node --experimental-specifier-resolution=node index
like image 39
Poyoman Avatar answered Oct 05 '22 08:10

Poyoman