ESM import.meta All In One
ESM import.meta All In One
获取 ES Module 的
meta
原数据
import.meta
The import.meta meta-property
exposes context-specific metadata
to a JavaScript module.
It contains information
about the module, such as the module's URL
.
import.meta.url
The full URL
to the module, includes query
parameters and/or hash
(following the ? or #).
In browsers
, this is either the URL from which the script
was obtained (for external scripts), or the URL of the containing document (for inline scripts
).
In Node.js
, this is the file path
(including the file://
protocol).
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import.meta
使用场景
- 使用
ESM
在 Node.js 中实现__dirname
功能, 获取模块所在的文件夹
的绝对路径
- ...
errors
solutions
- 浏览器
<script type="module">
- Node.js
import.meta.resolve
https://nodejs.org/docs/latest-v15.x/api/esm.html#esm_import_meta
https://deno.land/manual@v1.36.3/runtime/import_meta_api
demos
ESM 中实现
__dirname
CJS
__dirname
node:fs
vs fs
node:
类似https://
,file://
是一种通信协议 ❓
Modules: node:module
API
ESM
CJS
https://nodejs.org/dist/latest-v18.x/docs/api/module.html
refs
https://stackoverflow.com/questions/67554506/what-are-the-nodefs-nodepath-etc-modules
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17659727.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2022-08-27 iPhone 外接移动固态硬盘 All In One
2022-08-27 三星固态硬盘 All In One
2022-08-27 2022 最新中国电影票房排行榜 All In One
2021-08-27 how to using js get canvas text content All In One
2020-08-27 Dart Generic All In One
2020-08-27 how to create react custom hooks with arguments All In One
2020-08-27 Go lang All In One