簡體   English   中英

如何在Heroku上啟用ES2017功能的情況下運行Node.js應用程序?

[英]How to run Node.js app with ES2017 features enabled on Heroku?

我是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支持此語法的最簡單方法是什么?

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

因此,對於異步/等待支持,您需要指定> = 7.6.0

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

從此處的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"
  },
  ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM