本文主要记录 require 升级为 import 遇到的问题
__dirname is not defined in ES module scope
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 
 | import path from 'path';import {fileURLToPath} from 'url';
 
 const __filename = fileURLToPath(import.meta.url);
 
 
 const __dirname = path.dirname(__filename);
 console.log('directory-name 👉️', __dirname);
 
 
 console.log(path.join(__dirname, '/dist', 'index.html'));
 
 
 | 
import json file
| 12
 
 | import json from "./foo.json" assert { type: "json" };import("foo.json", { assert: { type: "json" } });
 
 | 
import 条件导入
| 12
 3
 4
 
 | if (condition) { import moduleA from './moduleA';
 }
 
 
 | 
import 函数
| 12
 3
 4
 5
 
 | import('./moduleA .js').then(moduleA => {
 console.log(moduleA);
 });
 
 
 | 
| 12
 3
 4
 
 | if(condition){import('moduleA').then(...);
 }
 
 
 |