官术网_书友最值得收藏!

Finding and loading ES6 modules using import

The import statement is used to load ES6 modules, and it only works inside an ES6 module. Because ES6 modules are loaded asynchronously, the require() statement cannot load ES6 modules. As we said earlier, ES6 modules are recognized by Node.js by the .mjs extension.  The ECMAScript TC-39 committee has (or plans to) officially register that file extension with the recognized authorities so that regular tools will recognize both file extensions as JavaScript.

The module specifier one hands to the import statement is interpreted as a URL. For the time being, Node.js will only accept file: URL because of the security implications of loading modules over the Internet. Because it's a URL, some characters such as :, ?, #, or % must receive special treatment. For example:

import './foo?search';
import './foo#hash';

These are valid module specifiers where ?search and #hash have the sort of meaning you'd expect in a URL. So long as Node.js only supports file: URL for import statements, we cannot make use of that feature, but we have to keep it in mind and avoid using these strings in module URL.

One can install custom module loader hooks that could conceivably use those URL parts for some purpose.

The module search algorithm is similar to what we described for require. If the specifier begins with ./, ../, or /, the specifier is interpreted as a pathname. Otherwise, it is interpreted as a top-level module similar to the require statement, with one big difference. The import statement will not search for a global module. This is frowned on, but if one must use a global module, that can be accomplished with a symbolic link.

For documentation, see https://nodejs.org/api/esm.html.

主站蜘蛛池模板: 福安市| 龙胜| 读书| 乌拉特前旗| 云浮市| 田东县| 清新县| 合肥市| 乌拉特后旗| 呼和浩特市| 吉木萨尔县| 洛隆县| 铜川市| 平罗县| 水富县| 大渡口区| 桃江县| 汽车| 阿城市| 德钦县| 澄城县| 彭水| 丰原市| 临漳县| 高州市| 荔波县| 嘉义县| 伊通| 大足县| 龙州县| 民勤县| 库尔勒市| 佛教| 康马县| 万盛区| 尚义县| 顺昌县| 深水埗区| 罗田县| 额济纳旗| 营山县|