javascript - 设置 babel-node 和express

标签 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

广告,收到:未定义。

我在脚本启动中添加了跨环境:

  "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

为什么我做错了?

最佳答案

我解决了这个问题。

.babelrc

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

关于javascript - 设置 babel-node 和express,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47033399/

相关文章:

javascript - 无法使用 webpack 4 和 babel 7 导入 soap-npm 模块

javascript - JSPM Babel ES6,导入条件模块的首选方式

node.js - 如何在 Heroku 上运行启用了 ES2017 功能的 Node.js 应用程序?

javascript - react 意外 token <

webpack - 即使使用 babel 插件,解构赋值在 IE 11 中也不起作用

javascript - 代码拆分如何隐藏应用程序的某些部分?

javascript - 咕噜声 : Warning: Object #<Object> has no method 'indexOf'

javascript - 如何选择 "sub"元素内的所有 "main"元素

javascript - 更改 window.location 时丢失窗口引用?

javascript - 将表格从 HTML 导出为 PDF