无法将 nest.js 项目部署到 Google Firebase 函数

Cannot deploy nest.js project to Google Firebase Functions

NestJs 使用 ES6、ES7 和 ES8,但 Firebase Functions 卡在 Node v.6.11。

我尝试编写一个带有 babel 的 webpack 配置文件来将我的文件和 node_modules 转换为节点 v6.11,但由于语法错误导致我无法完成部署@nestjs/common/interceptors/file-fields.interceptor.js 文件中的异步函数。

⚠  functions[api]: Deployment error.
Function load error: Code in file dist/index.js can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: /user_code/node_modules/@nestjs/common/interceptors/file-fields.interceptor.js:10
        async intercept(context, call$) {
              ^^^^^^^^^

SyntaxError: Unexpected identifier
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:549:28)
    at Object.Module._extensions..js (module.js:586:10)
    at Module.load (module.js:494:32)
    at tryModuleLoad (module.js:453:12)
    at Function.Module._load (module.js:445:3)
    at Module.require (module.js:504:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/user_code/node_modules/@nestjs/common/interceptors/index.js:6:10)

这是我的 webpack.config.js 文件:

'use strict';
const nodeExternals = require('webpack-node-externals');
module.exports = {
    entry: './src/server.ts',
    output: {
        filename: 'index.js',
        libraryTarget: 'this'
    },
    target: 'node',
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                use: [
                    { 
                        loader: 'babel-loader',
                        options: {
                            presets: [
                                [
                                    '@babel/preset-env',
                                    {
                                        "targets": {
                                        "node": "6.11.1"
                                        }
                                    },
                                    '@babel/stage-0'
                                ]
                            ],
                            plugins: [require('@babel/plugin-transform-async-to-generator')]
                        }
                    }, 
                    {
                        loader: 'ts-loader',
                        options: {
                            transpileOnly: true
                        }
                    }
                ]
            },
            {
                test: /\.js$/,
                use: [
                    { 
                        loader: 'babel-loader',
                        options: {
                            presets: [
                                [
                                    '@babel/preset-env',
                                    {
                                        "targets": {
                                        "node": "6.11.1"
                                        }
                                    },
                                    '@babel/stage-0'
                                ]
                            ],
                            plugins: [require('@babel/plugin-transform-async-to-generator')]
                        }
                    }
                ]
            }
        ]
    },
    resolve: {
        extensions: [ '.ts', '.tsx', '.js' ]
    },
    externals: [nodeExternals()]
};

我的tsconfig.json:

{
  "compilerOptions": {
    "lib": ["es6", "es2015.promise"],
    "module": "commonjs",
    "noImplicitAny": false,
    "outDir": "",
    "sourceMap": true,
    "removeComments": true,
    "noLib": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowJs": true,
    "target": "es6",
    "typeRoots": [
      "node_modules/@types"
    ]
  },
  "include": [
    "src/**/*.ts",
    "spec/**/*.ts"
  ],
  "exclude": [
    "**/*.spec.ts"
  ]
}

怎么了?

节点 6 不会 运行 使用 async 关键字的任何代码,因为它 doesn't support ES2017 的异步函数。

我建议尝试使用 TypeScript 来转换您的代码,在您的 tsconfig.json 中使用 es6 作为 target。它应该转换异步函数。请记住,您可能需要根据需要加载特定的 polyfill。你可能知道这个细节,但 NestJS 指定了 Node 8.9+,如文档所述 here:

We follow the Node.js release schedule which recently moved to 8.x as an active LTS version. Therefore, Nest 5 supports >= 8.9.0 as the lowest version now. This shift gaves us sustainable performance boosts thanks to the es2017 target of the TypeScript compilation.

恰好 3 天前(在 Google Cloud 的 Next conf 之后)Google 刚刚宣布了新的 Node 8 运行时和 Firebase Cloud Functions 2.0.0 以及 Firebase 工具到 4.0.0。

要登上 Node 8 列车,您需要执行以下操作:

  1. 将您的 firebase-functions version 升级到 2.0.0
  2. 升级 firebase-tools 到 4.0.0
  3. 将 "engines": { “node": “8” } 添加到您的 /functions/package.json

这里有更多信息:https://firebase.google.com/docs/functions/manage-functions#set_nodejs_version