C:\Users\wuwuwu\workspace\nestjs-template\node_modules\@nestjs\common\decorators\http\route-params.decorator.js:53
if (options?.passthrough) {
^
SyntaxError: Unexpected token '.'
at wrapSafe (internal/modules/cjs/loader.js:915:16)
at Module._compile (internal/modules/cjs/loader.js:963:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Module.require (internal/modules/cjs/loader.js:887:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (C:\Users\wuwuwu\workspace\nestjs-template\node_modules\@nestjs\common\decorators\http\index.js:5:22)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
START RequestId: 462345bb1ee97583da790723973ae0df/var/user/node_modules/@nestjs/common/decorators/http/route-params.decorator.js:53 if (options?.passthrough) { ^SyntaxError: Unexpected token '.' at wrapSafe (internal/modules/cjs/loader.js:1072:16) at Module._compile (internal/modules/cjs/loader.js:1122:27) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10) at Module.load (internal/modules/cjs/loader.js:1002:32) at Function.Module._load (internal/modules/cjs/loader.js:901:14) at Module.require (internal/modules/cjs/loader.js:1044:19) at require (internal/modules/cjs/helpers.js:77:18) at Object.<anonymous> (/var/user/node_modules/@nestjs/common/decorators/http/index.js:5:22) at Module._compile (internal/modules/cjs/loader.js:1158:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)Init Report RequestId: 462345bb1ee97583da790723973ae0df Coldstart: 77ms (PullCode: 0ms InitRuntime: 1ms InitFunction: 76ms) Memory: 512MB MemUsage: 0.18MBERROR RequestId: 462345bb1ee97583da790723973ae0df Result: {"errorCode": -1, "errorMessage": "Failed to initialize the container. Please confirm that the container can be started locally.", "statusCode": 405}END RequestId: 462345bb1ee97583da790723973ae0dfReport RequestId: 462345bb1ee97583da790723973ae0df Duration: 0ms Memory: 512MB MemUsage: 0.18MB
把一个NestJS的项目部署到到腾讯云的Serverless,但是报错了,这是版本不对,NestJS10 不再支持Node.js12了,需要Node.js16才能运行。Migration guide – FAQ | NestJS – A progressive Node.js framework。既然不支持Node.js12那么就改用Node.js16,但是腾讯云的函数运行环境一旦创建了,不能修改了,只能删除该函数,重新创建,在创建的阶段指定runtime,不能在创建了以后再去修改。
faas:
runtime: Nodejs16.13
还需要新增 scf_bootstrap
文件来设置启动的版本、方式:
#!/bin/bash
# SERVERLESS=1 /var/lang/node12/bin/node ./dist/main.js
SERVERLESS=1 /var/lang/node16/bin/node ./dist/main.js
参考:https://cloud.tencent.com/document/product/1154/59341
查看腾讯云Serverless支持的Node.js版本:云函数 环境说明-代码开发-文档中心-腾讯云 (tencent.com)
环境变量设置
环境变量设置方式一
不打包 .env
,而是直接读取 .env
文件的环境变量到 serverless.yml
中,使用 ${env:环境变量名}
这种方式可以实现。
component: http
name: nestjstemplate
inputs:
src:
dist: ./
hook: npm run build
exclude: // 排除需要打包的文件
- .env
faas:
runtime: Nodejs16.13
framework: nestjs
name: ${name}
environments:
- key: SERVERLESS
value: '1'
- key: DB_HOST
value: ${env:DB_HOST}
- key: DB_NAME
value: ${env:DB_NAME}
- key: DB_USERNAME
value: ${env:DB_USERNAME}
- key: DB_PASSWORD
value: ${env:DB_PASSWORD}
- key: PORT
value: 9000
- key: clientUrl
value: ${env:clientUrl}
apigw:
protocols:
- http
- https
app: demo-nestjs
环境变量设置方式二
另一种就是比较传统的方式就是让应用程序自行读取 .env
文件,所以在 scf deploy
时,需要连同 .env
文件一起打包。
include:
- .env
参考
删除应用
傻逼腾讯云居然在Serverless应用控制台无法删除Nuxt应用,只能通过CLI删除。如果你之前项目不是通过CLI创建的,那么在控制台->进入对应的应用->更新应用->下载代码->然后本地进入代码目录(也就是serverless.yml
文件同级目录)->执行scf remove
即可删除。
参考:Serverless 应用中心 删除应用-操作指南-文档中心-腾讯云 (tencent.com)
正文完
发表至: NuxtJS
2023-09-14