__dirnamere 沒法在 ES module 使用
nodejs es -ReferenceError: __dirname is not defined in ES module scope
改一個 __dirname 函式出來。
import path from 'path'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
Why
ES module 不支援 __dirname 這個變數。
How
使用 import.meta.url 在 eslint 會出現錯誤提示,可忽略。
沒辦法將他變成 module ,因為 import.meta.url 傳回的是他本身檔案的資源路徑,而不是呼叫時函式所在的檔案。
所以在不同檔案中用,用一次就要寫一次。
import { dirname } from 'path'
import { fileURLToPath } from 'url'
const __dirname = dirname(fileURLToPath(import.meta.url))
    
     安裝 ruby
    <<