

AWS App Runner 自 2026 年 4 月 30 日起，不再開放給新客戶。如果您想要使用 App Runner，請在該日期之前註冊。現有客戶可以繼續正常使用該服務。如需詳細資訊，請參閱[AWS App Runner 可用性變更](https://docs.aws.amazon.com/apprunner/latest/dg/apprunner-availability-change.html)。

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 Node.js 平台
<a name="service-source-code-nodejs"></a>

**重要**  
App Runner 將於 **2025 年 12 月 1 日結束對 Node.js** 12、**Node.js 14**、**Node.js 16** 和 **Node.js 18** 的支援。如需建議和詳細資訊，請參閱 [受管執行時間版本的終止支援](service-source-code.md#service-source-code.managed-platforms.eos)。

 AWS App Runner Node.js 平台提供受管執行期。每個執行時間都可讓您根據 Node.js 版本使用 Web 應用程式輕鬆建置和執行容器。當您使用 Node.js 執行期時，App Runner 會從受管 Node.js 執行期映像開始。此映像以 [Amazon Linux Docker 映像](https://hub.docker.com/_/amazonlinux)為基礎，並包含 Node.js 版本和一些工具的執行時間套件。App Runner 使用此受管執行期映像做為基礎映像，並新增您的應用程式程式碼來建置 Docker 映像。然後，它會部署此映像，以在容器中執行您的 Web 服務。

 當您使用 App Runner 主控台或 [CreateService](https://docs.aws.amazon.com/apprunner/latest/api/API_CreateService.html) API 操作[建立服務時，](manage-create.md)您可以指定 App Runner 服務的執行時間。您也可以指定執行時間做為原始程式碼的一部分。在程式碼儲存庫中包含的 [App Runner 組態檔案中](config-file.md)使用 `runtime`關鍵字。受管執行時間的命名慣例為 *<language-name><major-version>*。

如需有效的 Node.js 執行時間名稱和版本，請參閱 [Node.js 執行時間版本資訊](service-source-code-nodejs-releases.md)。

App Runner 會在每次部署或服務更新時，將服務的執行時間更新為最新版本。如果您的應用程式需要特定版本的受管執行時間，您可以使用 [App Runner 組態檔案中](config-file.md)的 `runtime-version`關鍵字來指定它。您可以鎖定任何層級的版本，包括主要或次要版本。App Runner 只會對服務的執行時間進行較低層級的更新。

Node.js 執行時間的版本語法： `major[.minor[.patch]]`

例如：`22.14.0`

下列範例示範版本鎖定：
+ `22.14` – 鎖定主要和次要版本。App Runner 只會更新修補程式版本。
+ `22.14.0` – 鎖定至特定修補程式版本。App Runner 不會更新您的執行時間版本。

**Topics**
+ [Node.js 執行期組態](#service-source-code-nodejs.config)
+ [特定執行時間版本的呼叫](#service-source-code-nodejs.callouts)
+ [Node.js 執行時間範例](#service-source-code-nodejs.examples)
+ [Node.js 執行時間版本資訊](service-source-code-nodejs-releases.md)

## Node.js 執行期組態
<a name="service-source-code-nodejs.config"></a>

當您選擇受管執行時間時，您也必須至少設定建置和執行命令。您可以在[建立](manage-create.md)或[更新](manage-configure.md) App Runner 服務時設定它們。您可以使用下列其中一種方法執行此操作：
+ **使用 App Runner 主控台** – 在建立程序或組態索引標籤的設定**建置**區段中指定命令。
+ **使用 App Runner API** – 呼叫 [CreateService](https://docs.aws.amazon.com/apprunner/latest/api/API_CreateService.html) 或 [UpdateService](https://docs.aws.amazon.com/apprunner/latest/api/API_UpdateService.html) API 操作。使用 [CodeConfigurationValues](https://docs.aws.amazon.com/apprunner/latest/api/API_CodeConfigurationValues.html) 資料類型的 `BuildCommand`和 `StartCommand`成員指定命令。
+ **使用[組態檔案](config-file.md)** – 在最多三個建置階段指定一或多個建置命令，以及用於啟動應用程式的單一執行命令。還有其他選用的組態設定。

提供組態檔案是選用的。當您使用主控台或 API 建立 App Runner 服務時，您可以指定 App Runner 在建立時直接從組態檔案取得您的組態設定。

特別是使用 Node.js 執行時間，您也可以使用來源儲存庫根`package.json`目錄中名為 的 JSON 檔案來設定建置和執行時間。使用此檔案，您可以設定 Node.js 引擎版本、相依性套件和各種命令 （命令列應用程式）。npm 或 yarn 等套件管理員會將此檔案解譯為其命令的輸入。

例如：
+ **npm install** 在 中安裝由 `dependencies`和 `devDependencies`節點定義的套件`package.json`。
+ **npm start** 或 會**npm run start**執行 中`scripts/start`節點定義的命令`package.json`。

以下是範例 `package.json` 檔案。

### package.json
<a name="service-source-code-nodejs.config.package-json-example"></a>

```
{
  "name": "node-js-getting-started",
  "version": "0.3.0",
  "description": "A sample Node.js app using Express 4",
  "engines": {
    "node": "22.14.0"
  },
  "scripts": {
    "start": "node index.js",
    "test": "node test.js"
  },
  "dependencies": {
    "cool-ascii-faces": "^1.3.4",
    "ejs": "^2.5.6",
    "express": "^4.15.2"
  },
  "devDependencies": {
    "got": "^11.3.0",
    "tape": "^4.7.0"
  }
}
```

如需 的詳細資訊`package.json`，請參閱 *npm Docs* 網站上的[建立 package.json 檔案](https://docs.npmjs.com/creating-a-package-json-file)。

**提示**  
如果您的`package.json`檔案定義了**start**命令，您可以在 App Runner 組態檔案中將其用作**run**命令，如下列範例所示。  

**Example**  
package.json  

  ```
  {
    "scripts": {
      "start": "node index.js"
    }
  }
  ```
apprunner.yaml  

  ```
  run:
    command: npm start
  ```
當您**npm install**在開發環境中執行 時，npm 會建立 檔案 `package-lock.json`。此檔案包含剛安裝的套件版本 npm 的快照。之後，當 npm 安裝相依性時，它會使用這些確切版本。如果您安裝 yarn，它會建立 `yarn.lock` 檔案。將這些檔案遞交至原始碼儲存庫，以確保您的應用程式已安裝您開發和測試的相依性版本。
您也可以使用 App Runner 組態檔案來設定 Node.js 版本和啟動命令。當您這樣做時，這些定義會覆寫 中的定義`package.json`。中的`node`版本`package.json`與 App Runner 組態檔案中`runtime-version`的值衝突會導致 App Runner 建置階段失敗。

## 特定執行時間版本的呼叫
<a name="service-source-code-nodejs.callouts"></a>

### Node.js 22 和 Node.js 18 （已修訂 App Runner 組建）
<a name="service-source-code-nodejs.callouts.nodejs18"></a>

App Runner 現在會根據下列執行時間版本執行應用程式的更新建置程序：Python 3.11、Node.js 22 和 Node.js 18。如果您的應用程式在其中一個執行時間版本上執行，請參閱 [受管執行期版本和 App Runner 組建](service-source-code.md#service-source-code.build-detail) 以取得修訂建置程序的詳細資訊。使用所有其他執行時間版本的應用程式不會受到影響，而且會繼續使用原始建置程序。

## Node.js 執行時間範例
<a name="service-source-code-nodejs.examples"></a>

下列範例顯示用於建置和執行 Node.js 服務的 App Runner 組態檔案。

**注意**  
這些範例中使用的執行時間版本為 *22.14.0*。您可以將其取代為您想要使用的版本。如需最新支援的 Node.js 執行時間版本，請參閱 [Node.js 執行時間版本資訊](service-source-code-nodejs-releases.md)。

### 最小 Node.js 組態檔案
<a name="service-source-code-nodejs.examples.minimal"></a>

此範例顯示您可以搭配 Node.js 受管執行時間使用的最小組態檔案。如需 App Runner 使用最小組態檔案所做的假設，請參閱 [組態檔案範例](config-file-examples.md#config-file-examples.managed)。

**Example apprunner.yaml**  

```
version: 1.0
runtime: nodejs22
build:
  commands:    
    build:
      - npm install --production                                  
run:                              
  command: node app.js
```

### 擴充 Node.js 組態檔案
<a name="service-source-code-nodejs.examples.extended"></a>

此範例顯示搭配 Node.js 受管執行時間使用所有組態金鑰。

**注意**  
這些範例中使用的執行時間版本為 *22.14.0*。您可以將其取代為您想要使用的版本。如需最新支援的 Node.js 執行時間版本，請參閱 [Node.js 執行時間版本資訊](service-source-code-nodejs-releases.md)。

**Example apprunner.yaml**  

```
version: 1.0
runtime: nodejs22
build:
  commands:
    pre-build:
      - npm install --only=dev
      - node test.js
    build:
      - npm install --production
    post-build:
      - node node_modules/ejs/postinstall.js
  env:
    - name: MY_VAR_EXAMPLE
      value: "example"
run:
  runtime-version: 22.14.0
  command: node app.js
  network:
    port: 8000
    env: APP_PORT
  env:
    - name: MY_VAR_EXAMPLE
      value: "example"
```

### 擴充 Node.js 組態檔案 – Node.js 22 （使用修訂的組建）
<a name="service-source-code-nodejs.examples.extended-v2"></a>

此範例顯示使用 中具有 Node.js 受管執行時間的所有組態金鑰。 `apprunner.yaml`此範例包含 `pre-run`區段，因為此版本的 Node.js 使用修訂過的 App Runner 組建。

只有修訂後的 App Runner 組建才支援 `pre-run` 參數。如果您的應用程式使用原始 App Runner 組建支援的執行時間版本，請勿將此參數插入您的組態檔案中。如需詳細資訊，請參閱[受管執行期版本和 App Runner 組建](service-source-code.md#service-source-code.build-detail)。

**注意**  
這些範例中使用的執行時間版本為 *22.14.0。*您可以將其取代為您想要使用的版本。如需最新支援的 Node.js 執行時間版本，請參閱 [Node.js 執行時間版本資訊](service-source-code-nodejs-releases.md)。

**Example apprunner.yaml**  

```
version: 1.0
runtime: nodejs22
build:
  commands:
    pre-build:
      - npm install --only=dev
      - node test.js
    build:
      - npm install --production
    post-build:
      - node node_modules/ejs/postinstall.js
  env:
    - name: MY_VAR_EXAMPLE
      value: "example"
run:
  runtime-version: 22.14.0
  pre-run: 
    - node copy-global-files.js
  command: node app.js
  network:
    port: 8000
    env: APP_PORT
  env:
    - name: MY_VAR_EXAMPLE
      value: "example"
```

### Node.js 應用程式搭配 Grunt
<a name="service-source-code-nodejs.examples.grunt"></a>

此範例說明如何設定使用 Grunt 開發的 Node.js 應用程式。[Grunt](https://gruntjs.com/) 是命令列 JavaScript 任務執行器。它執行重複性任務並管理程序自動化，以減少人為錯誤。使用 npm 安裝和管理 Grunt 和 Grunt 外掛程式。您可以透過在來源儲存庫的根目錄中包含 `Gruntfile.js` 檔案來設定 Grunt。

**Example package.json**  

```
{
  "scripts": {
    "build": "grunt uglify",
    "start": "node app.js"
  },
  "devDependencies": {
    "grunt": "~0.4.5",
    "grunt-contrib-jshint": "~0.10.0",
    "grunt-contrib-nodeunit": "~0.4.1",
    "grunt-contrib-uglify": "~0.5.0"
  },
  "dependencies": {
    "express": "^4.15.2"
  },
}
```

**Example Gruntfile.js**  

```
module.exports = function(grunt) {

  // Project configuration.
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    uglify: {
      options: {
        banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
      },
      build: {
        src: 'src/<%= pkg.name %>.js',
        dest: 'build/<%= pkg.name %>.min.js'
      }
    }
  });

  // Load the plugin that provides the "uglify" task.
  grunt.loadNpmTasks('grunt-contrib-uglify');

  // Default task(s).
  grunt.registerTask('default', ['uglify']);

};
```

**Example apprunner.yaml**  
這些範例中使用的執行時間版本為 *22.14.0*。您可以將其取代為您想要使用的版本。如需最新支援的 Node.js 執行時間版本，請參閱 [Node.js 執行時間版本資訊](service-source-code-nodejs-releases.md)。

```
version: 1.0
runtime: nodejs22
build:
  commands:
    pre-build:
      - npm install grunt grunt-cli
      - npm install --only=dev
      - npm run build
    build:
      - npm install --production
run:
  runtime-version: 22.14.0
  command: node app.js
  network:
    port: 8000
    env: APP_PORT
```

# Node.js 執行時間版本資訊
<a name="service-source-code-nodejs-releases"></a>

**重要**  
App Runner 將於 **2025 年 12 月 1 日結束對 Node.js** 12、**Node.js 14**、**Node.js 16** 和 **Node.js 18** 的支援。如需建議和詳細資訊，請參閱 [受管執行時間版本的終止支援](service-source-code.md#service-source-code.managed-platforms.eos)。

**注意**  
App Runner 的標準棄用政策是在執行時間的任何主要元件達到社群長期支援 (LTS) 結束且不再提供安全性更新時棄用執行時間。在某些情況下，App Runner 可能會在執行時間支援的語言版本end-of-support日期之後，將執行時間的棄用延遲一段有限期間。這類案例的範例可能是擴展對執行時間的支援，以允許客戶有時間進行遷移。

本主題列出 App Runner 支援的 Node.js 執行時間版本的完整詳細資訊。


**支援的執行時間版本 — 修訂後的 App Runner 組建**  
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/apprunner/latest/dg/service-source-code-nodejs-releases.html)

**注意**  
App Runner 為最近發佈的特定主要執行時間提供修訂後的建置程序。因此，您會在本文件的特定章節中看到*修訂後的 App Runner 組建*和*原始 App Runner 組建*的參考。如需詳細資訊，請參閱 [受管執行期版本和 App Runner 組建](service-source-code.md#service-source-code.build-detail)。


**支援的執行時間版本 — 修訂後的 App Runner 組建**  
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/apprunner/latest/dg/service-source-code-nodejs-releases.html)




**支援的執行時間版本 — 原始 App Runner 組建**  
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/apprunner/latest/dg/service-source-code-nodejs-releases.html)