Google Cloud Functions, Node JS 8.9.x (LTS) and KOA library

僤鯓⒐⒋嵵緔 提交于 2019-12-01 05:35:23

Reading from cloud functions doc:

Base Image Cloud Functions uses a Debian-based execution environment and includes contents of the gcr.io/google-appengine/nodejs Docker image, with the Node.js runtime installed in the version, specified above:

FROM gcr.io/google-appengine/nodejs
RUN install_node v6.14.0

To see what is included in the image, you can check its GitHub project, or pull and inspect the image itself. Updates to the language runtime (Node.js) are generally done automatically (unless otherwise notified), and include any changes in the definition of the base image.

And I saw a pull request back in November 2017, adding Nodejs v8. Here's hoping it can finally land in Google Cloud Functions 🤞🏻

UPDATE: Google Cloud Functions now support Node.js 8 and even Python!

Referring to the release notes from Google... Cloud Functions Release Notes

Node version supported is still at v6, same for firebase. You need to wait awhile before they release it in v8. Am pretty sure they will move to v8 when v6 no longer supported, but hopefully earlier...

Use babel:

index.js:
----------=

'use strict';

require('@babel/register')
require('babel-polyfill')
const http = require('http')

const createApp = require('./app/app.js')
const handle = createApp().callback()

if (process.env.IS_LOCAL_DEPLOYMENT) {
    // to use same code in local server
    http.createServer(handle).listen(3000)
} else {
    module.exports.http = (request, response) => {
        handle(request, response)
    };
}

app.js:
--------

'use strict';

const Koa = require('koa')

module.exports = () => {
    const app = new Koa()

    app.use(......)

    return app
}

package.json
------------

  "scripts": {
       .
       .
    "start": "export IS_LOCAL_DEPLOYMENT=true && node index"
       .
       .
  }

I just saw in Cloud Functions Console editor for one of my functions that Node 8 is now a runtime option. See screenshot:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!