__dirname is not defined in ES m

2023-03-13  本文已影响0人  Amy_yqh
image.png
// 解决__dirname不能使用问题
// 最新 node 核心包的导入写法
import { fileURLToPath } from 'node:url'
import { dirname } from 'node:path'
// 获取 __filename 的 ESM 写法
const __filename = fileURLToPath(import.meta.url)
// 获取 __dirname 的 ESM 写法
const __dirname = dirname(fileURLToPath(import.meta.url))

const getPath = _path =>path.resolve(__dirname,_path)
上一篇下一篇

猜你喜欢

热点阅读