在Node中使用ES6的更多功能(除了已发货的功能)(例如“导入/导出”)

时间:2016-02-27 20:28:59

标签: node.js

目的

我打算用 reactJS + nodeJS + babel(ES6)+ webpack 启动一个Web项目。但我目前在ES6环境设置步骤中被阻止。

Tech Stack

  • React:客户端(视图)
  • 节点:服务器端(服务器)
  • Babel:ES6的转录程序 - > ES5
  • Webpack:模块捆绑器

问题

无法使用节点

运行某些ES6语句

启动应用程序的步骤

  

$ npm install

     

$ npm run build

     

$ set DEBUG=myapp:* & npm start

Errortrace:


    SyntaxError: Unexpected token export
        at exports.runInThisContext (vm.js:53:16)
        at Module._compile (module.js:387:25)
        at Object.Module._extensions..js (module.js:422:10)
        at Module.load (module.js:357:32)
        at Function.Module._load (module.js:314:12)
        at Module.require (module.js:367:17)
        at require (internal/module.js:16:19)
        ... ...

我的配置

  1. 的package.json
  2.     {
          "name": "myapp",
          "version": "0.0.0",
          "private": true,
          "scripts": {
            "build": "webpack",
            "start": "node ./bin/www"
          },
          "dependencies": {
            "alt": "^0.18.2",
            "body-parser": "~1.13.2",
            "cookie-parser": "~1.3.5",
            "debug": "~2.2.0",
            "ejs": "^2.4.1",
            "express": "~4.13.1",
            "morgan": "~1.6.1",
            "react": "^0.14.7",
            "react-dom": "^0.14.7",
            "serve-favicon": "~2.3.0",
            "webpack": "^1.12.14"
          },
          "devDependencies": {
            "babel-cli": "^6.5.1",
            "babel-core": "^6.5.2",
            "babel-loader": "^6.2.4",
            "babel-polyfill": "^6.3.14",
            "babel-preset-es2015": "^6.5.0",
            "babel-preset-react": "^6.5.0",
            "copy-webpack-plugin": "^0.2.0",
            "webpack": "^1.12.6",
            "webpack-dev-server": "^1.12.1"
          }
        }
    
    1. .babelrc
    2. 
          { 
            "presets": [
              "es2015"
            ] 
          }
      
      
      1. webpack.config.js enter image description here
      2. 项目存储库

        click here托管在Github上

1 个答案:

答案 0 :(得分:0)

并非所有ES6的功能都在生产中,并且在nodejs中默认启用。模块属于其中之一。您可能需要运行node.js - 和谐标志,或者特别是关于模块: - harmony_modules

查看所有正在进行的列表'功能使用以下命令:

$ node --v8-options | grep 'in progress'