简体   繁体   English

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

[英]Cannot deploy nest.js project to Google Firebase Functions

NestJs uses ES6, ES7 and ES8, but Firebase Functions is stuck at Node v.6.11. NestJs使用ES6,ES7和ES8,但Firebase功能停留在Node v.6.11。

I tried to write a webpack config file w/ babel to transpile both my files and the node_modules to node v6.11 but I'm not able to complete the deploy due to a syntax error caused by an async function in the @nestjs/common/interceptors/file-fields.interceptor.js file. 我试着编写一个带有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)

Here's my webpack.config.js file: 这是我的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()]
};

My tsconfig.json: 我的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"
  ]
}

What's wrong? 怎么了?

Exactly 3 days ago (after Google Cloud's Next conf) Google just announced the new Node 8 runtime and Firebase Cloud Functions 2.0.0 and Firebase tools to 4.0.0. 就在3天前(Google Cloud的Next conf之后),Google刚刚宣布了新的Node 8运行时和Firebase Cloud Functions 2.0.0以及Firebase工具4.0.0。

Here's what you need to do to get on the Node 8 train: 这是您进入Node 8列车所需要做的事情:

  1. Upgrade your firebase-functions version to 2.0.0 firebase-functions version升级到2.0.0
  2. Upgrade firebase-tools to 4.0.0 firebase-tools升级到4.0.0
  3. Add "engines": { “node": “8” } to your /functions/package.json 在您的/functions/package.json添加“引擎”: { “node": “8” }

More deets here: https://firebase.google.com/docs/functions/manage-functions#set_nodejs_version 更多deets: https ://firebase.google.com/docs/functions/manage-functions#set_nodejs_version

Node 6 won't run any code using the async keyword as it doesn't support async functions from ES2017. 节点6不会使用async关键字运行任何代码,因为它不支持 ES2017的异步功能。

I'd recommend trying to use TypeScript for the transpilation of your code, using es6 as a target in your tsconfig.json . 我建议你尝试使用打字稿为您的代码的transpilation,使用es6target在你的tsconfig.json It should transpile async functions. 它应该转换为异步函数。 Please keep in mind that you might have to load specific polyfills depending on your needs. 请记住,您可能需要根据需要加载特定的填充物。 And you're probably aware of this detail but NestJS specifies Node 8.9+, as documented here : 而你可能知道这个细节,但NestJS指定节点8.9+,如记录在这里

We follow the Node.js release schedule which recently moved to 8.x as an active LTS version. 我们遵循Node.js发布计划,该计划最近移动到8.x作为活动LTS版本。 Therefore, Nest 5 supports >= 8.9.0 as the lowest version now. 因此,Nest 5现在支持> = 8.9.0作为最低版本。 This shift gaves us sustainable performance boosts thanks to the es2017 target of the TypeScript compilation. 由于Type20编译的es2017目标,这一转变为我们带来了可持续的性能提升。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM