简体   繁体   English

为 Google Cloud App Engine 灵活指定 Node.js 版本

[英]Specifying Node.js version for Google Cloud App Engine Flexible

I'm trying to deploy a GCloud App Engine Flexible service.我正在尝试部署 GCloud App Engine 灵活服务。 I have a yaml file, in which it has the Node.js runtime and the env specified.我有一个 yaml 文件,其中包含 Node.js 运行时和指定的env

runtime: nodejs
env: flex

As the documentation says "You can specify a different Node.js version in your application's package.json file by using the engines field.", I also added the following to package.json :正如文档所说“您可以使用engines 字段在应用程序的package.json 文件中指定不同的Node.js 版本。”,我还在package.json添加了以下内容:

"name": "@bindr/dev",
"version": "1.0.0",
"engines": {
  "node": ">=14.0.0"
},

However, when I run gcloud app deploy , I get the following error:但是,当我运行gcloud app deploy ,出现以下错误:

error @bindr/dev@1.0.0: The engine "node" is incompatible with this module. Expected version ">=14.0.0". Got "12.19.0"

It seems like the deployment process doesn't take the engines property into account, because even if I specify an invalid version (eg >=18.0.0 ) it still doesn't complain, only the yarn install fails.似乎部署过程没有考虑到engines属性,因为即使我指定了一个无效版本(例如>=18.0.0 )它仍然没有抱怨,只有yarn install失败。 How can I make the build process use the specified Node version?如何让构建过程使用指定的 Node 版本?

I found that I could specify the version of Node in cloudbuild.yaml for the certain steps of the build, like so:我发现我可以在cloudbuild.yaml为构建的某些步骤指定 Node 的版本,如下所示:

steps:
  - name: node:node-14.10.0
    args: ['predeploy.js', 'content-server']
  - name: 'gcr.io/cloud-builders/yarn:node-14.17.1'
    args: ['install']
  - name: 'gcr.io/cloud-builders/gcloud'
    args: ['app', 'deploy']
timeout: '900s'

In this process, the yarn install step succeeds, but the gcloud app deploy step still fails while trying to install the dependencies (I couldn't find how I could specify the node version to gcr.io/cloud-builders/gcloud , it doesn't seem to be such tag).在此过程中, yarn install步骤成功,但gcloud app deploy步骤在尝试安装依赖项时仍然失败(我找不到如何将节点版本指定为gcr.io/cloud-builders/gcloud ,它没有'似乎不是这样的标签)。

I also checked and the same 12.19.0 version is running on the production instances, so it is not only the build environment that has an older version.我还检查了相同的12.19.0版本正在生产实例上运行,因此不仅仅是构建环境具有旧版本。

What am I doing wrong?我究竟做错了什么?

So take a look at this doc , with particular attention to this line所以看看这个文档,特别注意这一行

The engines.node property is optional, but if present, the value must be compatible with the Node.js version specified in your app.yaml file. engine.node 属性是可选的,但如果存在,该值必须与 app.yaml 文件中指定的 Node.js 版本兼容。 For example:例如:

I believe the default version is 12 (ie runtime: nodejs ) to correct this in your app.yaml file set runtime as follows runtime: nodejs14 or newer我相信默认版本是 12(即runtime: nodejs )以在您的app.yaml文件集运行时更正此问题,如下runtime: nodejs14或更高版本

Also bear in mind that minor patches are updated automatically so you can only specify the major version ie 14.XX Additionally if your stated version is not available the build process will fail.另请记住,次要补丁会自动更新,因此您只能指定主要版本,即 14.XX。此外,如果您指定的版本不可用,则构建过程将失败。

Note: If you are using cloud build with cloudbuild.yaml and a flex environment you may get a build error, move cloudbuild.yaml into its own folder to prevent this error and use --config option to state the location of the yaml.注意:如果您使用带有cloudbuild.yaml和 flex 环境的云构建,您可能会遇到构建错误,请将cloudbuild.yaml移动到其自己的文件夹中以防止出现此错误,并使用 --config 选项来说明 yaml 的位置。 See this doc for further guidance请参阅此文档以获取进一步指导

I encountered the same issue and created an issue for it here .我遇到了同样的问题并在这里创建了一个问题。 I suspect it is a bug with Google App Engine and not with your app.我怀疑这是 Google App Engine 的错误,而不是您的应用程序的错误。

As a workaround, I ended up using a custom runtime for my app.作为一种解决方法,我最终为我的应用程序使用了自定义运行时 To do this, in your GAE configuration file you switch from runtime: nodejs to runtime: custom and add a Dockerfile to your project root.为此,在您的 GAE 配置文件中,您从runtime: nodejs切换到runtime: custom并将 Dockerfile 添加到您的项目根目录。 There are good docs on writing a dockerfile here but here is a simple one you can use:有上写dockerfile良好的文档在这里,但这里是一个简单的,你可以使用:

# syntax=docker/dockerfile:1

FROM node:14.10.0
ENV NODE_ENV=production

WORKDIR /app

COPY ["package.json", "package-lock.json*", "./"]

RUN npm install --production

COPY . .

CMD [ "node", "server.js" ]

You'll probably also want a .dockerignore file that at least contains node_modules .您可能还需要一个至少包含node_modules.dockerignore文件。

Ultimately I think a GAE fix would be nicer, since it would be simpler to just configure your node version in package.json , like you're supposed to be able to.最终,我认为 GAE 修复会更好,因为在package.json配置您的节点版本会更简单,就像您应该能够做到的那样。 But this should be enough to get things working and sidestep what appears to be a GAE bug.但这应该足以让事情正常工作并避开似乎是 GAE 错误的东西。

To wrap it up, as @CharlieA mentioned, App Engine Flex environment has a default image for Node.js which is currently version 12.19.01 .总结一下,正如@CharlieA 所提到的,App Engine Flex 环境有一个 Node.js 的默认图像,目前版本为12.19.01

As @Cleanbeans pointed out, according to the Flex environment docs , the runtime for Node.js should be specified in the app.yaml as runtime: nodejs , unile other languages where you can specify the version number.正如@Cleanbeans 指出的,根据Flex 环境文档,Node.js 的运行时应在app.yaml指定为runtime: nodejs ,unile 其他语言,您可以在其中指定版本号。 This explains why the Engine was returning the default version despite specifying the version 14.这解释了为什么尽管指定了版本 14,但引擎仍返回默认版本。

Circling back to @CharlieA comment, this can be fixed by using the custom runtime in your app.yaml as follows: runtime: custom .回到@CharlieA 评论,这可以通过在app.yaml使用自定义运行时来修复,如下所示: runtime: custom

Alternatively, try using a fixed version in your package.json as being discussed in thisGithub thread .或者,尝试在你的package.json使用一个固定版本,正如这个Github thread 中所讨论的那样。

Apart from the Github thread mentioned above, @CharlieA also created a Public Issue tracker for Google to review this.除了上面提到的 Github 线程之外,@CharlieA 还创建了一个公共问题跟踪器供 Google 审查。 You may want to request a documentation update that would elaborate on how to specify the Node.js version.您可能需要请求文档更新,详细说明如何指定 Node.js 版本。

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

相关问题 Google Cloud node.js灵活的环境 - Google Cloud node.js flexible environment Winston不会在Google Cloud Compute Engine上托管的Node.js应用程序的制作版本中记录事件 - Winston not logging events in productions version of a Node.js app hosted on Google Cloud Compute Engine 无法将Node.js应用程序部署到Google Cloud App Engine - Unable to deploy node.js app to google cloud app engine 如何在 Google Cloud App Engine for Node.js 中使用 Memcache - How to use Memcache in Google Cloud App Engine for Node.js 针对Google App Engine灵活生产环境中公共目录的Node.js应用404错误 - Node.js app 404 errors for public directory in Google App Engine Flexible production environment Node.js版本和Google Cloud功能 - Node.js version and Google Cloud functions 如何保留部署在谷歌云应用引擎上的 node.js 应用的统计数据并保持我的数据持久化 - How to keep stats for a node.js app deployed on google cloud app engine and keep my data persistent Google Cloud App Engine是否具有特殊的Node.js运行时? - Does Google Cloud App Engine have a special Node.js runtime? Google App Engine上的node.js:运行版本与package.json中指定的版本不同 - node.js on google app engine: different version running vs specified in package.json Node.js “node-cron”未在 Google 应用引擎上运行 - Node.js “node-cron” not running on Google app engine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM