在最新版Node中使用ES6語法-2019-03-27

描述

node中使用ES6語法,很簡單,網上的文章寫的太複雜,我根據網上的經驗折騰了一下午,最後終結了下,幾乎裝個babel就能用的。
下面是個人使用過程,分享如何使用及遇到的問題。html

配置環境

首先的有node環境,這個不介紹,當前個人版本是8.11.4
1.初始化node

npm init -y

2.安裝babel
安裝官網上介紹的操做就行,https://www.babeljs.cn/es6

npm install --save-dev babel-cli babel-preset-env

建立 .babelrc 文件npm

{
  "presets": ["env"]
}

這時候已經配置完了,能夠執行ES6語法了,可是importexport仍是不支持的。json

檢測對es6的支持狀況

安裝es-checker來幫助咱們查看對es6的支持狀況。babel

npm install --save-dev es-checker

藉助npx工具來運行es-checker工具

npx的介紹能夠看這篇文章: http://www.ruanyifeng.com/blo...
npx es-checker

結果以下:測試

ECMAScript 6 Feature Detection (v1.4.1)

Variables
  √ let and const
  √ TDZ error for too-early access of let or const declarations
  √ Redefinition of const declarations not allowed
  √ destructuring assignments/declarations for arrays and objects
  √ ... operator

...省略內容

Module
  × Module export command
  × Module import command


=========================================
Passes 39 feature Detections
Your runtime supports 92% of ECMAScript 6
=========================================
能夠看到仍是有一些不支持的。

測試code

.babelrcfetch

{
  "presets": ["env"]
}
上面的配置其實就好了;可是我在看babel的文檔時說設置node環境須要設置targets,因而個人配置以下,可是我試了上面的配置也是能夠的,下面的僅供參考。
{
  "presets": [
    [
      "env",
      {
        "targets": {
          "node": "current"
        }
      }
    ]
  ]
}

package.jsonui

{
  "name": "node",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-preset-env": "^1.7.0",
    "es-checker": "^1.4.1"
  }
}

Stack.js

const Stack = (function() {
  const items = new WeakMap();
  class Stack {
    constructor() {
      items.set(this, []);
    }
    push(value) {
      let stack = items.get(this);
      stack.push(value);
    }
    pop() {
      let stack = items.get(this);
      return stack.pop();
    }
    isEmpty() {
      let stack = items.get(this);
      return stack.length === 0;
    }
    size() {
      let stack = items.get(this);
      return stack.length;
    }
    print() {
      let stack = items.get(this);
      console.log(stack.toString());
    }
  }
  return Stack;
})();
module.exports.Stack = Stack;

index.js

const { Stack } = require("./Stack.js");
//import { Stack } from "./Stack";
let stack = new Stack();
stack.push("aaa");
stack.print();

在控制檯中執行

node index.js

輸出

aaa

解決import和export不能用

其實node版本9以上就已經支持了,可是須要把文件名改爲*.mjs,而且加上--experimental-modules 選項。

升級node

介紹一個node升級的好工具,名字就叫n,具體能夠去npm上查看。

npm install -g n

執行以下命令進行升級

n stable
或
n 10.15.3

結果

install : node-v11.12.0
       mkdir : /usr/local/n/versions/node/11.12.0
       fetch : https://nodejs.org/dist/v11.12.0/node-v11.12.0-darwin-x64.tar.gz
######################################################################## 100.0%
   installed : v11.12.0

升級成功後,①把文件都改爲*.mjs,②並把代碼改爲importexport的方式,執行

node --experimental-modules arithmetic/index.mjs
上面兩步都不能少。否則就執行不成功。
相關文章
相關標籤/搜索