zl程序教程

您现在的位置是:首页 >  Javascript

当前栏目

在 Node.js 中使用 ES6 模块 import 和 export

2023-03-14 09:46:02 时间

1、Node版本 >= 13

2、使用 ES6 模块

a.js

export const A = 'A';

index.js

import { A } from './a.js';

console.log(A);

3、创建 package.json

{
    "type": "module"
}

4、直接运行即可

$ node index.js

 

其他方法参考 https://nodejs.org/api/esm.html