es6features
es6features copied to clipboard
Module lookup in ES6.
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
- it will lookup for
dir1
which is defined ABOVE the current file. - If it finds
dir1
it tries to finddir2/dir3/file
there. - if it fails it continues step 1-2 again and so on.
Is my assumption correct?
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.
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 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
yeah. Here is my understand:
Because of compiler, like babel
, compile es6 import
to AMDand
commonJS. so, the path/module resolving rule is the
amdand
commonjs` path/module resolving rule?
My English is not good.Sorry for that! Do you understand me ?
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 OK, I know it. Anyway, Thank you!