如何在node環境中,使用ES6

創建測試文件

mkdir test
cd test
npm init -y
npm install --save-dev @babel/core @babel/cli @babel/preset-env @babel/node
npm install --save @babel/polyfill
touch babel.config.js
touch index.js
touch test.js

babel.config.js

const presets = [
  ["@babel/env",{
    targets: {
      edge:"17",
      firefox:"60",
      chrome:"67",
      safari:"11.1"
    }
  }]
]

module.exports = { presets }

test.js

const a = 1;

export default a;

index.js

import a from "./test"

console.log(a)

運行

 npx babel-node ./index.js

當然最後的話,你可以自己封裝一個script 命令

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章