【问题标题】:How to run Node.js app with ES2017 features enabled on Heroku?如何在 Heroku 上启用 ES2017 功能运行 Node.js 应用程序?
【发布时间】:2017-08-16 04:54:57
【问题描述】:

我是 Node 新手,我创建了一个应用,其中包含一些 async/await 语法,如下所示:

const express = require('express');
const app = express();

const someLibrary = require('someLibrary');

function asyncWrap(fn) {
  return (req, res, next) => {
    fn(req, res, next).catch(next);
  };
};

app.post('/getBlock', asyncWrap(async (req,res,next) => {
  let block = await someLibrary.getBlock(req.body.id);
  [some more code]
}));

app.listen(process.env.PORT || 8000);

它在我的机器上运行良好,但是当我部署到 Heroku 时出现错误,因为不支持语法:

2017-03-23T10:11:13.953797+00:00 app[web.1]: app.post('/getBlock', asyncWrap(async (req,res,next) => {
2017-03-23T10:11:13.953799+00:00 app[web.1]: SyntaxError: Unexpected token (

让 Heroku 支持这种语法的最简单方法是什么?

【问题讨论】:

  • async/await 是 ES2017 的一部分,而不是 ES7。

标签: node.js heroku async-await babeljs ecmascript-2017


【解决方案1】:

来自 Heroku 文档

https://devcenter.heroku.com/articles/getting-started-with-nodejs#declare-app-dependencies

应该在你的 package.json 文件中声明应该可以访问哪个引擎:

{
  "name": "node-js-getting-started",
  "version": "0.2.5",
  ...
  "engines": {
    "node": "5.9.1"
  },
  "dependencies": {
    "ejs": "2.4.1",
    "express": "4.13.3"
  },
  ...
}

【讨论】:

    【解决方案2】:

    在 package.json 中指定要使用的节点版本:https://devcenter.heroku.com/articles/nodejs-support#specifying-a-node-js-version

    因此,对于 async/await 支持,您需要指定 >= 7.6.0

    {
      "engines": {
        "node": ">= 7.6.0"
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-05-01
      • 2015-02-26
      • 1970-01-01
      • 2022-01-26
      • 2021-12-31
      • 2013-09-23
      • 2012-01-28
      • 2012-10-12
      • 2015-02-07
      相关资源
      最近更新 更多