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功能, 获取模块所在的文件夹的绝对路径 - ...
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
console.log(`✅ import.meta.url =`, import.meta.url, typeof import.meta.url)
console.log(`✅ __filename =`, __filename, typeof __filename)
console.log(`✅ __dirname`, __dirname, typeof __dirname)

errors

solutions
- 浏览器
<script type="module">
<script type="module">
console.log(`import.meta.url =`, import.meta.url);
</script>
<!-- 或 -->
<script src="./main.mjs" type="module"></script>
// main.mjs
console.log(`✅ import.meta.url =`, import.meta.url);

- Node.js
import.meta.resolve
const absolutePath = await import.meta.resolve(specifier[, parent]);
// main.mjs
const resolvedPath = await import.meta.resolve('./helper.mjs');
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
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
// import fs from 'fs';
// import path from 'path';
// import { fileURLToPath } from 'url';
// create one custom `__dirname`, because it doesn't exist in es-module env.
// use `import.meta.url` to get the current module's URL, ✅
// then get the module's folder absolute path
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const dir = path.resolve(path.join(__dirname, 'upload');
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
// OR
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, {
mode: 0o744, // Not supported on Windows. Default: 0o777
});
}
CJS
__dirname
const fs = require('fs');
const path = require('path');
const dir = path.resolve(path.join(__dirname, 'upload');
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
// OR
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, {
mode: 0o744, // Not supported on Windows. Default: 0o777
});
}
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
node:fs vs fs
import fs from 'node:fs';
import fs from 'fs';
node:类似https://,file://是一种通信协议 ❓
Modules: node:module API
ESM
// module.mjs
// In an ECMAScript module
import { builtinModules as builtin } from 'node:module';
CJS
// module.cjs
// In a CommonJS module
const builtin = require('node:module').builtinModules;
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
未经授权禁止转载,违者必究!

浙公网安备 33010602011771号