es6features icon indicating copy to clipboard operation
es6features copied to clipboard

Module lookup in ES6.

Open franza opened this issue 9 years ago • 6 comments

I'm trying to understand the way modules are loaded in ES6. I was googling this question for like half of hour but never found the exact definition. See the example:

import * as smth from 'dir1/dir2/dir3/file'

You can see that it's different from relative path like ./dir1/dir2/dir3/file. I was messing with this in TypeScript + NodeJS and it's lookup strategy is different from the one I used to work with in NodeJS. What I found is that

  1. it will lookup for dir1 which is defined ABOVE the current file.
  2. If it finds dir1 it tries to find dir2/dir3/file there.
  3. if it fails it continues step 1-2 again and so on.

Is my assumption correct?

franza avatar Jul 22 '15 12:07 franza

ES6 doesn't specify anything about the meaning of the contents of the module specifier (the string at the end of import statements). From ES6's perspective, it's an opaque value that just represents the module's location in an abstract way.

The hosting environment (browser, node, etc) has to provide a module loader that interprets the contents of the module specifier in a way that makes sense for that env. In node, I imagine the paths will be local file system paths (and behave accordingly), while in the browser, the paths will likely have to be interpreted as remote URLs (and behave accordingly).

The differences you observe may just be an artifact of the reality that module loaders are still un/under specified, so it's all still being sorted out. In any case, it's not an ES6 issue but a web platform issue.

getify avatar Jul 22 '15 12:07 getify

Save Questions. What's the ES6 module lookup rules? How does it work? ex.

import React from 'react';

How does es6 know where is the react. If I define a module named 'react' too, which one es6 import ?

mrdulin avatar Jul 11 '16 02:07 mrdulin

@mrdulin Again, this is not a part of ES6 standard. Path/module resolving should be made by current environment and could differ from one to another.

hlomzik avatar Jul 12 '16 11:07 hlomzik

@hlomzik yeah. Here is my understand: Because of compiler, like babel, compile es6 import to AMDandcommonJS. so, the path/module resolving rule is theamdandcommonjs` path/module resolving rule? My English is not good.Sorry for that! Do you understand me ?

mrdulin avatar Jul 13 '16 01:07 mrdulin

This is not a general support forum. ES6 doesn't specify how the modules are loaded. This repo has nothing to add to your questions. Perhaps try other support forums?

getify avatar Jul 13 '16 04:07 getify

@getify OK, I know it. Anyway, Thank you!

mrdulin avatar Jul 13 '16 05:07 mrdulin