本教程介绍如何准备本地机器以用于 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 开发者集成Google Cloud 服务(例如 Datastore 和 Cloud Storage)的惯用方式是使用 Node.js 版 Cloud 客户端库。例如,您可以使用以下命令为单个 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.
如需了解详情,请参阅使用客户端库时进行身份验证。
后续步骤
- 浏览 Google Cloud 产品的文档。
- 从 GitHub 克隆 Node.js 示例代码库。
- 浏览社区提交的 Node.js 教程。