Nodejs 中通过 babel 来体验 es6 语法
1.安装依赖:
npm install --save-dev @babel/core @babel/cli @babel/preset-env @babel/node
npm install --save @babel/polyfill
2.创建 babel.config.js 文件
const presets = [
["@babel/env", {
targets: {
edge: "17",
firefox: "60",
chrome: "67",
safari: "11.1"
}
}]
];
module.exports = { presets };
npx babel-node index.js
举例:
1.test.js
let a = 10;
let b = 5;
let c = 4;
let d = 6;
function show() {
console.log('11111')
}
export default {
a,
b,
c,
show
}
2.index.js
import i from './test.js'
console.log(i)
结果:
{ a: 10, b: 5, c: 4, show: [Function: show] }