網站搜尋

node.js - NestJS 專案在 Google App Engine 上顯示錯誤「無法寫入檔案」以及「EROFS:唯讀檔案系統」


當我在 Google App Engine 上部署 NestJS 專案時,我遇到如下錯誤而感到困擾。

錯誤 TS5033:無法寫入檔案 '/workspace/dist/properties/dto/property.filter.dto.d.ts':EROFS:唯讀檔案系統,開啟 '/workspace/dist/properties/dto/property.filter.dto.d.ts'。

app.yaml 檔案如下

runtime: nodejs10
env: standard

default_expiration: "4d 5h"

## to specify database
beta_settings:
  cloud_sql_instances: "your instance connection name"

env_variables:
  DATABASE_HOST: "/cloudsql/your instance connection name"
  DATABASE_USERNAME: "user name"
  DATABASE_PASSWORD: "password"
  INSTANCE_CONNECTION_NAME: "your instance connection name"

handlers:
- url: /.*
  secure: always
  redirect_http_response_code: 301
  script: auto

resources:
  cpu: 1
  memory_gb: 0.5
  disk_size_gb: 10

tsconfig.json 文件如下

{
  "compileOnSave": true,
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "ES2017",
    "sourceMap": true,
    "outDir": "./dist",
    "allowJs": true,
    "baseUrl": "./",
    "incremental": true
  }
}

看起來我無法在 App Engine 的 dist 資料夾中寫入檔案,但我不確定為什麼會發生這種情況。

答案#1

如這份官方文件所示:

Java 8、Java 11、Node.js、Python 3、PHP 7、Ruby、Go 1.11 和 Go 1.12+ 可以對 /tmp 目錄進行讀寫操作。

所以,這意味著只有在 /tmp 目錄中您才能執行寫入操作,而其他目錄您將無法寫入,因為它們是唯讀的,這將包括 workspace,因此也包括 dist。正如這裡一個類似案例的回答中所說,您需要在 /tmp 目錄中處理您的文件,或者開始使用 App Engine Flex,在那裡您可以在其他目錄中寫入。

答案#2

你遇到的是 TypeScript 錯誤。TS5033(TS - TypeScript)。我在將我的 Node.js 應用程式部署到 Google App Engine 時也遇到了類似的問題,經過 24 小時的搜尋並嘗試了許多解決方法後,我最終發現並嘗試了這個:

https://cloud.google.com/appengine/docs/standard/nodejs/running-custom-build-step

你將在那篇文章中看到一個適合在 Google App Engine 上部署基於 TypeScript 的 Node.js 應用的 package.json。關鍵僅僅是在 package.json 中添加了以下腳本:

"gcp-build": "tsc -p ."

有些人會要求你將 typescript 從 devDependencies 移到 Dependencies。但如果你讀過那篇文章,我可以證實——這些都是不必要的。一旦你告訴 Google App Engine 如何通過添加上述腳本來構建 Typescript,那麼你的 start 腳本就可以只是:

"start": "node ./dist/index.js"

你就可以開始了…

我希望這能幫助到面臨類似問題的人!