部署Strapi 4.23.0至Google App Engine(Postgres)后遇错误求助
部署Strapi至Google App Engine时出现ENOENT错误:无法创建/workspace/.tmp目录
错误日志
Error: ENOENT: no such file or directory, mkdir '/workspace/.tmp' at Object.mkdirSync (node:fs:1373:26) at module.exports.makeDirSync (/layers/google.nodejs.yarn/yarn_modules/node_modules/fs-extra/lib/mkdirs/make-dir.js:23:13) at SqliteDialect.configure (/layers/google.nodejs.yarn/yarn_modules/node_modules/@strapi/database/dist/index.js:936:26) at new Database (/layers/google.nodejs.yarn/yarn_modules/node_modules/@strapi/database/dist/index.js:6185:18) at Database.init (/layers/google.nodejs.yarn/yarn_modules/node_modules/@strapi/database/dist/index.js:6170:16) at Strapi.bootstrap (/layers/google.nodejs.yarn/yarn_modules/node_modules/@strapi/strapi/dist/Strapi.js:372:39) at Strapi.load (/layers/google.nodejs.yarn/yarn_modules/node_modules/@strapi/strapi/dist/Strapi.js:426:16)
前端表现
- 访问链接:https://10615.ew.r.appspot.com/
- 提示:Service Unavailable
配置信息
Package.json
{ "name": "backend", "version": "0.1.0", "private": true, "description": "A Strapi application", "license": "MIT", "author": { "name": "A Strapi developer" }, "scripts": { "build": "strapi build", "develop": "strapi develop", "gcp-build": "strapi build", "start": "strapi start", "strapi": "strapi" }, "dependencies": { "@strapi/plugin-i18n": "4.23.0", "@strapi/plugin-seo": "^1.9.8", "@strapi/plugin-users-permissions": "4.23.0", "@strapi/strapi": "4.23.0", "better-sqlite3": "8.6.0", "pg": "^8.11.5", "react": "^18.0.0", "react-dom": "^18.0.0", "react-router-dom": "5.3.4", "styled-components": "5.3.3" }, "devDependencies": {}, "engines": { "node": ">=18.0.0 <=20.x.x", "npm": ">=6.0.0" }, "strapi": { "uuid": "4ab2-a874-3799d23b1640" } }
app.yaml
runtime: nodejs18 instance_class: F2 env_variables: HOST: '0.0.0.0' NODE_ENV: 'production' DATABASE_NAME: 'DB_NAME' DATABASE_USERNAME: 'postgres' DATABASE_PASSWORD: 'DB_PASS' INSTANCE_CONNECTION_NAME: 'double-41web' beta_settings: cloud_sql_instances: 'double-41web'
backend/config/env/production/database.js
module.exports = ({ env }) => ({ connection: { client: "postgres", connection: { host: `/cloudsql/${env("INSTANCE_CONNECTION_NAME")}`, database: env("DATABASE_NAME"), user: env("DATABASE_USER"), password: env("DATABASE_PASSWORD"), }, }, });
backend/.gcloudignore
.gcloudignore .git .gitignore node_modules/ #!include:.gitignore !.env yarn.lock
已尝试操作
为相关服务赋予以下权限:
- roles/cloudsql.client
- cloudsql.editor
- storage.objectAdmin
- appengine.deployer
- logging.admin
解决方案建议
修正数据库配置的变量名
你的database.js中使用env("DATABASE_USER")读取用户名,但app.yaml里的环境变量是DATABASE_USERNAME,变量名不匹配导致Postgres配置加载失败,Strapi fallback到Sqlite。把database.js里的user字段改为:user: env("DATABASE_USERNAME"),配置可写的临时目录
Google App Engine的/workspace目录是只读的,而Strapi默认会在当前工作目录创建.tmp文件夹。添加环境变量指定可写的临时目录(GAE的/tmp是可写的)到app.yaml:env_variables: # 保留原有变量,新增下面一行 STRAPI_TMP_DIR: '/tmp'移除生产环境不需要的Sqlite依赖
既然生产环境用Postgres,把better-sqlite3从dependencies移到devDependencies,避免Strapi默认加载Sqlite驱动:"devDependencies": { "better-sqlite3": "8.6.0" },或者直接删除该依赖(开发时若需要可重新安装)。
确认生产环境配置生效
确保部署时config/env/production/database.js被正确上传,检查.gcloudignore没有排除config目录(当前配置无问题),同时确认NODE_ENV确实被设置为production(你的app.yaml已配置)。
内容的提问来源于stack exchange,提问作者H.Husain
相关产品推荐
相关产品推荐

