The Wayback Machine - https://web.archive.org/web/20230117162936/https://docs.github.com/ja/codespaces/setting-up-your-project-for-codespaces/adding-a-dev-container-configuration/setting-up-your-nodejs-project-for-codespaces
Skip to main content

Setting up a Node.js project for GitHub Codespaces

Get started with a Node.js, JavaScript, or TypeScript project in GitHub Codespaces by creating a custom dev container configuration.

Introduction

This guide shows you how to set up an example Node.js project Visual Studio Code デスクトップ アプリケーションか VS Code Web クライアントのいずれかが使用されている Codespaces で。 codespace でプロジェクトを開いて開発コンテナーの構成を変更する例について紹介します。

Step 1: Open the project in a codespace

  1. Go to https://github.com/microsoft/vscode-remote-try-node.

  2. Click Use this template, then click Open in a codespace.

    Screenshot of the 'Use this template' button and dropdown menu

When you create a codespace, your project is created on a remote virtual machine that is dedicated to you. By default, the container for your codespace has many languages and runtimes including Node.js, JavaScript, and Typescript. It also includes a common set of tools, such as nvm, npm, yarn, git, wget, rsync, openssh, and nano.

codespace をカスタマイズするには、vCPU と RAM の量を調整するか、dotfile を追加して環境をカスタマイズするか、インストールされているツールやスクリプトを変更します。

Codespaces では、devcontainer.json というファイルを使って、codespace で作業するときに使う開発コンテナーを構成します。 各リポジトリには 1 つ以上の devcontainer.json ファイルを含めることができ、codespace でコードを操作するために必要な開発環境を正確に提供できます。

起動時に、Codespaces は devcontainer.json ファイルと、開発コンテナーの構成を設定する依存ファイルを使って、ツールやランタイムをインストールし、プロジェクトが必要とするその他のセットアップ タスクを実行します。 詳細については、開発コンテナーの概要に関するページをご覧ください。

Step 2: Add a dev container configuration

The default development container, or "dev container," for GitHub Codespaces will allow you to work successfully on a Node.js project like vscode-remote-try-node. However, we recommend that you configure your own dev container to include all of the tools and scripts your project needs. This will ensure a fully reproducible environment for all GitHub Codespaces users in your repository.

カスタム開発コンテナーを使用するようにリポジトリを設定するには、devcontainer.json ファイルを 1 つ以上作成する必要があります。 これは、定義済みの構成テンプレートから追加するか、Visual Studio Code 内に追加するか、または独自に記述することができます。 開発コンテナーの詳しい情報については、「開発コンテナーの概要」を参照してください。

  1. Visual Studio Code Command Palette にアクセスし (Shift + Command + P (Mac) / Ctrl + Shift + P (Windows/Linux))、「開発コンテナー」と入力します。 [コードスペース: 開発コンテナー構成ファイルの追加...] を選択します。

    Visual Studio Code Command Palette の「コードスペース: 開発コンテナー構成ファイルの追加...」

  2. Type node and click Node.js & JavaScript. Other options are available if your project uses particular tools. For example, Node and MongoDB.

    Screenshot of the 'Node.js & JavaScript' option

  3. Click the latest version of Node.js.

    Screenshot of the Node.js version selection

  4. A list of additional features is displayed. We'll install JSHint, a code quality tool for detecting errors in JavaScript code. To install this tool, type js, select JSHint (via npm), then click OK.

    Screenshot of additional features for 'js'

  5. A message is displayed telling you that the dev container configuration file already exists. Click Overwrite.

    A devcontainer.json file is created and is opened in the editor.

Details of your custom dev container configuration

If you look in the Visual Studio Code Explorer you'll see that a .devcontainer directory has been added to the root of your project's repository containing the devcontainer.json file. This is the main configuration file for codespaces created from this repository.

devcontainer.json

The devcontainer.json file that you have added will contain values for the name, image, and features properties. Some additional properties that you may find useful are included but are commented out.

The file will look similar to this, depending on which image you chose:

// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/javascript-node
{
  "name": "Node.js",
  // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
  "image": "mcr.microsoft.com/devcontainers/javascript-node:0-18-bullseye",
  "features": {
    "ghcr.io/devcontainers-contrib/features/jshint:2": {}
  }

  // Features to add to the dev container. More info: https://containers.dev/features.
  // "features": {},

  // Use 'forwardPorts' to make a list of ports inside the container available locally.
  // "forwardPorts": [],

  // Use 'postCreateCommand' to run commands after the container is created.
  // "postCreateCommand": "yarn install",

  // Configure tool-specific properties.
  // "customizations": {},

  // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
  // "remoteUser": "root"
}
  • name: You can name your dev container anything you want. A default value is supplied.
  • image: The name of an image in a container registry (DockerHub, GitHub Container registry, or Azure Container Registry) that will be used to create the dev container for the codespace.
  • features: A list of one or more objects, each of which references one of the available dev container features. Features are self-contained, shareable units of installation code and development container configuration. They provide an easy way to add more tooling, runtime, or library features to your development container. For more information, see "Available Dev Container Features" on the Development Containers website. You can add features by going to the VS Code Command Palette and typing features.
  • forwardPorts: Any ports listed here will be forwarded automatically. For more information, see "Forwarding ports in your codespace."
  • postCreateCommand: Use this property to run commands after your codespace is created.
  • customizations: This property allows you to customize a specific tool or service when it is used for working in a codespace. For example, you can configure specific settings and extensions for VS Code. For more information, see "Supporting tools and services" on the Development Containers website.
  • remoteUser: By default, you’re running as the vscode user, but you can optionally set this to root. For a complete list of available properties, see the dev containers specification on the Development Containers website.

Additional dev container configuration files

If you are familiar with Docker, you may want to use a Dockerfile, or Docker Compose, to configure your codespace environment, in addition to the devcontainer.json file. You can do this by adding your Dockerfile or docker-compose.yml files alongside the devcontainer.json file. For more information, see "Using Images, Dockerfiles, and Docker Compose" on the Development Containers website.

Step 3: Modify your devcontainer.json file

With your dev container configuration added and a basic understanding of what everything does, you can now make changes to customize your environment further. In this example, you'll add properties that will:

  • Run npm install, after the dev container is created, to install the dependencies listed in the package.json file.
  • Automatically install a VS Code extension in this codespace.
  1. In the devcontainer.json file, add a comma after the features property, and delete the two commented out lines about features.

    JSON
      "features": {
        "ghcr.io/devcontainers-contrib/features/jshint:2": {}
      },
    
      // Features to add to the dev container. More info: https://containers.dev/features.
      // "features": {},
  2. Uncomment the postCreateCommand property and assign it the command npm install.

    JSON
    // Use 'postCreateCommand' to run commands after the container is created.
    "postCreateCommand": "npm install",
  3. Uncomment the customizations property and edit it as follows to install the "Code Spell Checker" VS Code extension.

    JSON
    // Configure tool-specific properties.
    "customizations": {
      // Configure properties specific to VS Code.
      "vscode": {
        // Add the IDs of extensions you want installed when the container is created.
        "extensions": [
          "streetsidesoftware.code-spell-checker"
        ]
      }
    }

    The devcontainer.json file should now look similar to this, depending on which image you chose:

    // README at: https://github.com/devcontainers/templates/tree/main/src/javascript-node
    {
      "name": "Node.js",
      // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
      "image": "mcr.microsoft.com/devcontainers/javascript-node:0-18-bullseye",
      "features": {
        "ghcr.io/devcontainers-contrib/features/jshint:2": {}
      },
    
      // Use 'forwardPorts' to make a list of ports inside the container available locally.
      // "forwardPorts": [],
    
      // Use 'postCreateCommand' to run commands after the container is created.
      "postCreateCommand": "npm install",
    
      // Configure tool-specific properties.
      "customizations": {
        // Configure properties specific to VS Code.
        "vscode": {
          // Add the IDs of extensions you want installed when the container is created.
          "extensions": [
            "streetsidesoftware.code-spell-checker"
          ]
        }
      }
    
      // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
      // "remoteUser": "root"
    }
    
  4. Save your changes.

  5. VS Code Command Palette にアクセスし (Shift + Command + P (Mac) / Ctrl + Shift + P (Windows/Linux))、「リビルド」と入力します。 [Codespaces: コンテナーのリビルド] を選択します。

    コマンド パレットのコンテナーのリビルド コマンドのスクリーンショット

    ヒント: キャッシュをクリアし、新しいイメージを使用してコンテナーを再構築するために、完全なリビルドを実行することが必要になる場合があります。 詳細については、「コンテナーの完全なリビルドの実行」を参照してください。

    リポジトリに変更をコミットする前に、codespace 内でリビルドすることにより期待どおりに変更が動作します。 何らかの失敗があった場合、コンテナの調整を継続するためにリビルドできるリカバリコンテナを備えた codespace に配置されます。

    After the dev container is rebuilt, and your codespace becomes available again, the postCreateCommand will have been run, installing npm, and the "Code Spell Checker" extension will be available for use.

Step 4: Run your application

In the previous section, you used the postCreateCommand to install a set of packages via the npm install command. With the dependencies now installed, you can run the application.

  1. In the Terminal of your codespace, enter npm start.

    Screenshot of running 'npm start' in the terminal

  2. When your project starts, you should see a "toast" notification message at the bottom right corner of VS Code, telling you that your application is available on a forwarded port. To view the running application, click Open in Browser.

    Screenshot of the port forwarding "toast" notification

Step 5: Commit your changes

新しいコードであれ、設定の変更であれ、codespaceに変更を加えたら、その変更をコミットしたくなるでしょう。 リポジトリに変更をコミットすれば、このリポジトリからcodespaceを作成する他の人が、同じ設定になることを保証できます。 これはまた、VS Code 拡張機能の追加など、行うすべてのカスタマイズが、すべてのユーザーに対して現れるようになるということでもあります。

詳細については、「コードスペースでソース管理を使用する」を参照してください。

Next steps

You should now be able to add a custom dev container configuration to your own Node.js, JavaScript, or TypeScript project.