本教學課程說明如何準備本機電腦環境以進行 Node.js 開發工作,包括開發在Google Cloud上執行的 Node.js 應用程式。請按照本教學課程的指示,安裝 Node.js 和相關工具。
目標
- 安裝 Node Version Manager (NVM)。
- 安裝 Node.js 與 npm (Node Package Manager)。
- 安裝編輯器。
- 安裝 Google Cloud CLI。
- 安裝 Node.js 適用的 Cloud 用戶端程式庫。
- 設定驗證方法。
安裝 NVM
NVM 是 bash 指令碼,用來管理 Node.js 和 npm 的安裝作業。
NVM 不支援 Windows。如要進一步瞭解如何管理 Windows 的 Node.js 安裝作業,請參閱 nvm-windows。
如要詳細瞭解如何安裝 NVM,請參閱安裝操作說明。
安裝 Node.js 和 npm
安裝 NVM 後,便可接著安裝 Node.js 與 npm。
請執行以下指令安裝最新版本的 Node.js:
nvm install stable(選用) 如要將此版本設為預設版本,請執行以下指令:
nvm alias default stable(選用) 如要檢查您目前執行的 Node.js 版本,請執行以下指令:
node -v
npm 是 Node.js 適用的 Node Package Manager,通常會隨著 Node.js 一併安裝。您可以使用 npm 從 npm 存放區安裝 Node.js 套件,例如:
npm install express
安裝編輯器
您可以利用幾種編輯器開發 Node.js 應用程式,部分常見的編輯器包括:
- Microsoft 的 Visual Studio Code
- JetBrains 的 IntelliJ IDEA 及/或 Webstorm
這些編輯器 (有時需安裝外掛程式) 提供許多功能,像是語法醒目顯示、智慧感知、程式碼自動完成及全面整合偵錯功能,可提升 Node.js 開發效率。
安裝 Google Cloud CLI
gcloud CLI 是一組 Google Cloud工具,其中包含 gcloud 和 bq,方便您透過指令列存取 Compute Engine、Cloud Storage、BigQuery 和其他產品與服務。您可以利用這些工具進行互動操作,或是在自動化指令碼中執行這些工具。
npm start 啟動應用程式。
gcloud app deploy
安裝 Node.js 適用的 Cloud 用戶端程式庫
Node.js 開發人員常運用 Node.js 適用的 Cloud 用戶端程式庫整合各項Google Cloud 服務,例如 Datastore 和 Cloud Storage。舉例來說,您可以使用以下指令為個別 API 安裝套件:
npm install @google-cloud/storage
設定驗證方法
如要在本機開發環境中使用 Cloud 用戶端程式庫,請設定應用程式預設憑證。
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
詳情請參閱「進行驗證以使用用戶端程式庫」一文。
後續步驟
- 參閱 Google Cloud 產品說明文件。
- 從 GitHub 複製 Node.js 範例存放區。
- 探索由社群提交的 Node.js 教學課程。