Prefix-Only Core Modules
NodeJs 18 导入内置库,开始使用
node:作为前缀标识
import { types } from "node:util";
import { strict } from "node:assert"
用户空间的库,如果由机构维护,通常使用@机构名称作为前缀,如@vueuse/core。用户空间的库名无法使用:这个字符,提示不支持的URL_SCHME,同样也无法使用node:作为前缀命名
import sample from "core:sample";
// Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only URLs with a scheme in: file, data are supported by the default ESM loader. Received protocol 'core:'
import test from "node:tet";
// Error [ERR_UNKNOWN_BUILTIN_MODULE]: No such built-in module: node:tet
此外,node支持file和data格式导入包
import { foo } from "file:absolute-path/foo.mjs";
import { bar } from "data:text/javascript,export%20function%20bar()%20%7B%0A%20%20console.log('bar')%0A%7D";
foo();
bar();
deno支持http协议
import _ from "https://deno.land/x/lodash@4.17.15-es/lodash.js";
console.log(_.VERSION);
// 4.17.15
'fs' vs 'node:fs'
- 使用
node:作为前缀,可以避免标准库和用户空间的库名冲突。如新引入的node:test,如果使用test命名,有大概率已有npm库名冲突,不利于后续扩展 - 可以避免恶意相似库名攻击。 用户安装
@vue/hello,误拼为@vue/helo,有可能被恶意代码攻击。用户安装node:tet,系统则提示错误。