# 04 · import.meta.url **演示**:`import.meta.url` 是模块自身的绝对 URL;用 `new URL(relative, import.meta.url)` 解析同目录/上级路径,替代 CJS 的 `__dirname`。 **怎么看**:`node scripts/serve.mjs` 后打开 ,开 Console。 **预期输出**: ```text 模块 URL:http://localhost:8080/examples/14-module-resolution-import-maps/04-import-meta/main.js 同目录文件:http://localhost:8080/examples/14-module-resolution-import-maps/04-import-meta/data.json 模块目录:http://localhost:8080/examples/14-module-resolution-import-maps/04-import-meta/ 上一级文件:http://localhost:8080/examples/14-module-resolution-import-maps/README.md import.meta.url 由宿主提供,Node 是 file:,浏览器是 http(s):。 ``` **注意**:`new URL('data.json', import.meta.url)` 与 `new URL('./data.json', ...)` 等价;目录 URL 必须以 `/` 结尾,否则最后一段会被当作文件替换掉。Node 里读文件时要再用 `fileURLToPath` 转成路径(第 11 章)。`import.meta.resolve()` 可直接把说明符解析成 URL,但浏览器支持较新。需 `http://` 协议。