Setting up your Node.js project for Codespaces

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

Codespaces está disponible para las organizaciones que utilicen GitHub Team o Nube de GitHub Enterprise. Para obtener más información, consulta la sección "Productos de GitHub".

Introduction

This guide shows you how to set up your JavaScript, Node.js, or TypeScript project in Codespaces. It will take you through an example of opening your project in a codespace, and adding and modifying a dev container configuration from a template.

Prerequisites

  • You should have an existing JavaScript, Node.js, or TypeScript project in a repository on GitHub.com. If you don't have a project, you can try this tutorial with the following example: https://github.com/microsoft/vscode-remote-try-node
  • You must have Codespaces enabled for your organization.

Step 1: Open your project in a codespace

  1. Under the repository name, use the Code drop-down menu, and in the Codespaces tab, click New codespace.

    New codespace button

    If you don’t see this option, Codespaces isn't available for your project. See Access to Codespaces for more information.

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

You can customize your codespace by adjusting the amount of vCPUs and RAM, adding dotfiles to personalize your environment, or by modifying the tools and scripts installed.

Codespaces uses a file called devcontainer.json to store configurations. On launch Codespaces uses the file to install any tools, dependencies, or other set up that might be needed for the project. For more information, see "Introduction to dev containers."

Step 2: Add a dev container to your codespace from a template

The default codespaces container will support running Node.js projects like vscode-remote-try-node out of the box. By setting up a custom container you can customize the tools and scripts that run as part of codespace creation and ensure a fully reproducible environment for all Codespaces users in your repository.

To set up your project with a custom container, you will need to use a devcontainer.json file to define the environment. In Codespaces you can add this either from a template or you can create your own. For more information on dev containers, see "Introduction to dev containers".

  1. Access the VS Code Command Palette (Shift + Command + P / Ctrl + Shift + P), then start typing "dev container". Selecciona Codespaces: Agregar archivos de configuración del contenedor de desarrollo....

    "Codespaces: Add Development Container Configuration Files..." in the VS Code Command Palette

  2. For this example, click Node.js. If you need additional features you can select any container that’s specific to Node or a combination of tools such as Node and MongoDB. Select Node option from the list

  3. Click the recommended version of Node.js. Node.js version selection

  4. Accede a la VS Code Command Palette (Shift + Command + P/ Ctrl + Shift + P) y luego comienza a escribir "rebuild". Selecciona Codespaces: Reconstruir contenedor.

    Opción de recompilar contenedor

Anatomy of your dev container

Adding the Node.js dev container template adds a .devcontainer folder to the root of your project's repository with the following files:

  • devcontainer.json
  • Dockerfile

The newly added devcontainer.json file defines a few properties that are described after the sample.

devcontainer.json

// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.162.0/containers/javascript-node
{
	"name": "Node.js",
	"build": {
		"dockerfile": "Dockerfile",
		// Update 'VARIANT' to pick a Node version: 10, 12, 14
		"args": { "VARIANT": "14" }
	},

	// Set *default* container specific settings.json values on container create.
	"settings": {
		"terminal.integrated.shell.linux": "/bin/bash"
	},

	// Add the IDs of extensions you want installed when the container is created.
	"extensions": [
		"dbaeumer.vscode-eslint"
	],

	// 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",

	// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
	"remoteUser": "node"
}
  • Name - You can name your dev container anything, this is just the default.
  • Build - The build properties.
    • dockerfile - In the build object, dockerfile is a reference to the Dockerfile that was also added from the template.
    • Args
      • Variant: This file only contains one build argument, which is the node variant we want to use that is passed into the Dockerfile.
  • Settings - These are Visual Studio Code settings that you can set.
    • Terminal.integrated.shell.linux - While bash is the default here, you could use other terminal shells by modifying this.
  • Extensions - These are extensions included by default.
    • Dbaeumer.vscode-eslint - ES lint is a great extension for linting, but for JavaScript there are a number of great Marketplace extensions you could also include.
  • forwardPorts - Any ports listed here will be forwarded automatically. For more information, see "Forwarding ports in your codespace."
  • postCreateCommand - If you want to run anything after you land in your codespace that’s not defined in the Dockerfile, you can do that here.
  • remoteUser - By default, you’re running as the vscode user, but you can optionally set this to root.

Dockerfile

# [Choice] Node.js version: 14, 12, 10
ARG VARIANT="14-buster"
FROM mcr.microsoft.com/vscode/devcontainers/javascript-node:0-${VARIANT}

# [Optional] Uncomment this section to install additional OS packages.
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
#     && apt-get -y install --no-install-recommends <your-package-list-here>

# [Optional] Uncomment if you want to install an additional version of node using nvm
# ARG EXTRA_NODE_VERSION=10
# RUN su node -c "source /usr/local/share/nvm/nvm.sh && nvm install ${EXTRA_NODE_VERSION}"

# [Optional] Uncomment if you want to install more global node modules
# RUN su node -c "npm install -g <your-package-list-here>"

You can use the Dockerfile to add additional container layers to specify OS packages, node versions, or global packages we want included in our Dockerfile.

Step 3: Modify your devcontainer.json file

With your dev container added and a basic understanding of what everything does, you can now make changes to configure it for your environment. In this example, you'll add properties to install npm when your codespace launches and make a list of ports inside the container available locally.

  1. In the Explorer, select the devcontainer.json file from the tree to open it. You might have to expand the .devcontainer folder to see it.

    devcontainer.json file in the Explorer

  2. Add the following lines to your devcontainer.json file after extensions:

    JSON
    "postCreateCommand": "npm install",
    "forwardPorts": [4000],

    For more information on devcontainer.json properties, see the devcontainer.json reference in the Visual Studio Code docs.

  3. Accede a la VS Code Command Palette (Shift + Command + P/ Ctrl + Shift + P) y luego comienza a escribir "rebuild". Selecciona Codespaces: Reconstruir contenedor.

    Opción de recompilar contenedor

    Rebuilding inside your codespace ensures your changes work as expected before you commit the changes to the repository. If something does result in a failure, you’ll be placed in a codespace with a recovery container that you can rebuild from to keep adjusting your container.

Step 4: Run your application

In the previous section, you used the postCreateCommand to installing a set of packages via npm. You can now use this to run our application with npm.

  1. Run your start command in the terminal withnpm start.

    npm start in terminal

  2. When your project starts, you should see a toast in the bottom right corner with a prompt to connect to the port your project uses.

    Port forwarding toast

Step 5: Commit your changes

Una vez que hayas hecho cambios a tu codespace, ya sea de código nuevo o de cambios de configuración, necesitarás confirmar tus cambios. El confirmar los cambios en tu repositorio garantiza que cualquiera que cree un codespace desde este repositorio tendrá la misma configuración. Esto también significa que cualquier personalización que hagas, tal como agregar extensiones de Visual Studio Code, aparecerá para todos los usuarios.

Para obtener más información, consulta la sección "Utilizar el control de código fuente en tu codespace"

Next steps

You should now be ready start developing your JavaScript project in Codespaces. Here are some additional resources for more advanced scenarios.

¿Te ayudó este documento?

Política de privacidad

¡Ayúdanos a hacer geniales estos documentos!

Todos los documentos de GitHub son de código abierto. ¿Notas algo que esté mal o que no sea claro? Emite una solicitud de cambios.

Haz una contribución

O, aprende cómo contribuir.