ESModule中载入commjs

it2025-12-31  0

开门见山
在上篇文章中介绍了,在node中如何引入ESModule模块,所以呢在本篇文章中将介绍在ESModule中载入commjs模块 1、ESModule载入默认成员 module.exports = { foo: "foo" } //导入 import mod from './commonjs.js'; console.log(mod); //命令行运行:node --experimental-modules esmodule.mjs log => { foo: 'foo' } 2、ESModule载入单个成员 //导出单个成员 exports.foo = "commonjs"; //导入 import foo from "./commonjs.js"; console.log(foo)//=>commonjs //命令行运行:node --experimental-modules esmodule.mjs 3、commonjd特性 //commonjs模块始终导出一个默认成员,也就是说ESModule只能使用载入默认成员的方式 import {foo} from "./commonjs.js" ;//不能直接提取成员 console.log(foo) //=> error 直接报错, 最后要注意一点 import {} 不是解构,这一点很多人都会理解为机构,其实根本不是

谢谢观看,如有不足,敬请指教

最新回复(0)