xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

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 功能, 获取模块所在的文件夹绝对路径
  • ...

image

errors

image

solutions

  1. 浏览器 <script type="module">

image

  1. 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/21194934/how-to-create-a-directory-if-it-doesnt-exist-using-node-js/71735771#71735771

https://stackoverflow.com/questions/67554506/what-are-the-nodefs-nodepath-etc-modules



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(48)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!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
点击右上角即可分享
微信分享提示