设置babel-node并表达

时间:2017-10-31 10:43:24

标签: javascript babeljs

我尝试使用babel和nodemon设置ExpressJS服务器。我的 package.json

{
  "name": "dnz-home-server",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "start": "babel-node ./src/index.jsx --presets=react-app"
  },
  "dependencies": {
    "express": "^4.16.2"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.0",
    "babel-preset-react-app": "^3.1.0"
  }
}

当我运行命令yarn start时,我收到此错误

Error: Using `babel-preset-react-app` requires that you specify `NODE_ENV` or `BABEL_ENV` environment variables. Valid values are "development", "test", and "production". Inste

ad,收到:undefined。

我将cross-env添加到脚本start:

  "scripts": {
    "start": "cross-env NODE_ENV=development BABEL_ENV=development babel-node ./src/index.jsx --presets=react-app"
  },

然后我收到此错误

(function (exports, require, module, __filename, __dirname) { import express from 'express';
                                                              ^^^^^^

SyntaxError: Unexpected token import

为什么我做错了?

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。

<强> .babelrc

{
  "presets": [
    [
      "env",
      {
        "targets": {
          "node": "7.1"
        }
      }
    ],
    "react-app"
  ]
}