|
| 1 | +# Developing Node.js using Dev Containers |
| 2 | + |
| 3 | +[Dev Containers](https://containers.dev/) allows development inside a containerized environment, ensuring |
| 4 | +consistency across different development setups. |
| 5 | + |
| 6 | +Node.js provides a [nightly Dev Container image](https://hub.docker.com/r/nodejs/devcontainer) that can |
| 7 | +be used to set up a development environment quickly and enable incremental compilation based on nightly builds. |
| 8 | +Most of the times, if you only have some changes in the main branch and do not need to change the V8 headers |
| 9 | +(which is rare), using the nightly image will allow you to compile your changes from a fresh checkout |
| 10 | +in a few seconds instead of spending tens of minutes or even hours to compile the entire codebase from scratch. |
| 11 | +The Dev Container also allows you to test your changes in a different operating system, and to test |
| 12 | +third-party code from bug reports safely with your work-in-progress Node.js branches in an isolated environment. |
| 13 | + |
| 14 | +This guide will walk you through the steps to set up a Dev Container for Node.js development using |
| 15 | +[Visual Studio Code (VS Code)](https://code.visualstudio.com/). |
| 16 | + |
| 17 | +## Prerequisites |
| 18 | + |
| 19 | +Before you begin, ensure you have the following installed on your machine: |
| 20 | + |
| 21 | +* [Docker](https://www.docker.com/get-started) |
| 22 | +* [Visual Studio Code](https://code.visualstudio.com/) |
| 23 | +* [Dev Containers extension for VS Code](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) |
| 24 | + |
| 25 | +## Setting Up the Dev Container |
| 26 | + |
| 27 | +### 1. Clone the Node.js Repository |
| 28 | + |
| 29 | +If you haven't already, clone the Node.js repository to your local machine. |
| 30 | + |
| 31 | +```bash |
| 32 | +git clone https://github.com/nodejs/node.git |
| 33 | +``` |
| 34 | + |
| 35 | +### 2. Open the Repository in VS Code |
| 36 | + |
| 37 | +Launch VS Code and open the cloned Node.js repository. |
| 38 | + |
| 39 | +### 3. Start the Dev Container |
| 40 | + |
| 41 | +* Press `Ctrl+Shift+P` or `Cmd+Shift+P` to open the command palette. |
| 42 | +* Type `Dev Containers: Reopen in Container` and select it. |
| 43 | +* Select the appropriate Dev Container configuration from the drop down. The base configuration in this |
| 44 | + repository is `Node.js Dev Container` located in |
| 45 | + [`.devcontainer/base/devcontainer.json`](../../.devcontainer/base/devcontainer.json), which should be |
| 46 | + automatically detected by VS Code. |
| 47 | + |
| 48 | +### 4. Wait for the Container to Build |
| 49 | + |
| 50 | +VS Code will build the Dev Container based on the configuration file. This may take some time depending |
| 51 | +on your internet connection and system performance. |
| 52 | + |
| 53 | +After the container is built, it will start automatically. By default it will run `git restore-mtime` to |
| 54 | +restore the modification times of the files in the working directory, in order to keep the build cache valid, |
| 55 | +and you will see something like this in the terminal. |
| 56 | + |
| 57 | +```text |
| 58 | +Running the postCreateCommand from devcontainer.json... |
| 59 | +
|
| 60 | +[10136 ms] Start: Run in container: /bin/sh -c git restore-mtime |
| 61 | +``` |
| 62 | + |
| 63 | +This may take a few seconds. Wait until it finishes before you open a new terminal. |
| 64 | + |
| 65 | +### 5. Build your changes |
| 66 | + |
| 67 | +Once the container is running, you can open a terminal in VS Code and run the build commands. By default, your |
| 68 | +local repository is mounted inside a checkout in the container, so any changes you make will be reflected in |
| 69 | +the container environment. |
| 70 | + |
| 71 | +In the Dev Container, the necessary dependencies are already installed. For better developer experience, the |
| 72 | +build tool used in the Dev Container is `ninja`, instead of `make`. See |
| 73 | +[Building Node.js with Ninja](./building-node-with-ninja.md) for more details on using `ninja` to build Node.js. |
| 74 | + |
| 75 | +```bash |
| 76 | +./configure --ninja |
| 77 | +ninja -C out/Release |
| 78 | +``` |
| 79 | + |
| 80 | +As long as your local checkout is not too different from the main branch in the nightly image, the build |
| 81 | +should be incremental and fast. |
| 82 | + |
| 83 | +### 6. Leaving the Container |
| 84 | + |
| 85 | +When you're done working in the Dev Container, open the command palette again and select |
| 86 | +`Dev Containers: Reopen Folder locally` to go back to your local development environment. |
| 87 | + |
| 88 | +## Customizing the Dev Container |
| 89 | + |
| 90 | +The default configuration is located in |
| 91 | +[`.devcontainer/base/devcontainer.json`](../../.devcontainer/base/devcontainer.json) which pairs with the |
| 92 | +official [Node.js Nightly Dev Container image](https://github.com/nodejs/devcontainer). |
| 93 | +This is tracked in version control. You can create a personal configuration by creating a new |
| 94 | +directory in `.devcontainer/` and adding a `devcontainer.json` file there (for example, |
| 95 | +`.devcontainer/local/devcontainer.json`), and configure VS Code to use it. |
| 96 | + |
| 97 | +## Rebuilding the Dev Container |
| 98 | + |
| 99 | +Docker will cache the already downloaded Dev Container images. If you wish to pull a new nightly image, you can do |
| 100 | +so by running the following command in the terminal on your host machine: |
| 101 | + |
| 102 | +```bash |
| 103 | +docker pull nodejs/devcontainer:nightly |
| 104 | +``` |
| 105 | + |
| 106 | +The default configuration creates a volume to cache the build outputs between container restarts. If you wish to clear |
| 107 | +the build cache, you can do so by deleting the volume named `node-devcontainer-cache`. |
| 108 | + |
| 109 | +```bash |
| 110 | +docker volume rm node-devcontainer-cache |
| 111 | +``` |
| 112 | + |
| 113 | +To rebuild the container in VS Code, open the command palette and select |
| 114 | +`Dev Containers: Rebuild and Reopen in Container`. |
| 115 | + |
| 116 | +## Further Reading |
| 117 | + |
| 118 | +* [Visual Studio Code Dev Containers Documentation](https://code.visualstudio.com/docs/remote/containers) |
| 119 | +* If you want to propose changes to the official nightly Dev Container image, the repository is |
| 120 | + [nodejs/devcontainer](https://github.com/nodejs/devcontainer) which contains the nightly workflows and |
| 121 | + the Dockerfile. |
0 commit comments