Skip to main content

misc

ESM 中使用 __dirname

__dirname 是旧式 CommonJS 的变量,在 ESM 模式中可以如下使用:

import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

// You can now use __dirname as you normally would
console.log(__dirname);