首页 > Node.js 使用 ES6 出错?

Node.js 使用 ES6 出错?

import React from 'react';

Node.js版本

v6.2.1

错误信息

SyntaxError: Unexpected token import
    at Object.exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:513:28)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
    at Function.Module.runMain (module.js:575:10)
    at startup (node.js:160:18)
    at node.js:456:3

为什么使用import会出错?


目前为止,node尚未支持ES6的module方式
推荐终端使用es-checker命令查看当前node对es6的支持情况,对那些不支持的情况,只能使用babel转下咯
不过要先npm install -g es-checker


https://kangax.github.io/compat-table/es6/

es6各种兼容性


目前node还没有实现import,可以使用babel进行转码


Node一直木有考虑好如何实现,所以想要使用import就只能用babel-cli转码器了。

【热门文章】
【热门文章】