1
0
mirror of synced 2025-12-20 10:28:40 -05:00
Files
docs/content/codespaces/getting-started-with-codespaces/getting-started-with-your-nodejs-project-in-codespaces.md
Amy Burns c863678e73 New organization for codespaces docs (#18910)
* adding new tables and linking to info on audit logs

* editing some text:

* adding new info on how to add image registry login

* adding link and fixing table

* formatting

* formatting

* Apply suggestions from code review

Co-authored-by: Laura Coursen <lecoursen@github.com>

* updaing private image registry secret info and other improvements

* some minor fixes

* fixing test errors

* rewriting registry secret section

* rewriting registry secret section

* updating link to reflect title

* Update content/github/developing-online-with-codespaces/managing-encrypted-secrets-for-codespaces.md

Co-authored-by: Laura Coursen <lecoursen@github.com>

* updating bullet point

* moving codespaces to top-level item

* fixing duplicated redirect

* Adding some organization for contributors

* adding learning codespaces to index file

* do not error out if category array is empty

* Apply suggestions from code review

Co-authored-by: Kevin Heis <heiskr@users.noreply.github.com>

* fixing broken link

* Document choosing machine type for a codespace in the GitHub UI (#19055)

* Document choosing machine type in the GitHub UI

* Replace link placeholder with actual path

* Update content/codespaces/changing-the-machine-type-for-your-codespace.md

Co-authored-by: Amy Burns <timeyoutakeit@github.com>

Co-authored-by: Amy Burns <timeyoutakeit@github.com>

* reoganizing based on CD

* putting machine type doc in correct location

* fixing broken redirects

* removing quickstart example from index page

* updating main index file

* Fixing broken links

* adding intros

* adding js quickstart (#18960)

* adding js quickstart

* clarify walkthrough

* adding more images and last few sections

* fixing broken links

* fixing broken links and formatting

* formatting fixes

* Apply suggestions from code review

Co-authored-by: hubwriter <hubwriter@github.com>

* adding index file for learn folder

* adding some feedback changes

* adding some review comments

* fixing broken links

Co-authored-by: hubwriter <hubwriter@github.com>

* doing some additional re-org

* fixing topics

* adding Codespaces as a topic

* fixing so title matches slug

* Update data/reusables/codespaces/codespaces-machine-types.md

Co-authored-by: Meg Bird <megbird@github.com>

* expanding intro

Co-authored-by: Laura Coursen <lecoursen@github.com>
Co-authored-by: Sarah Schneider <sarahs@github.com>
Co-authored-by: Kevin Heis <heiskr@users.noreply.github.com>
Co-authored-by: hubwriter <hubwriter@github.com>
Co-authored-by: Meg Bird <megbird@github.com>
2021-05-03 17:06:49 +00:00

12 KiB
Raw Blame History

title, shortTitle, intro, product, versions, topics
title shortTitle intro product versions topics
Getting started with your Node.js project in Codespaces Getting started with your Node.js project You can create a custom dev container with all the tools necessary to get started with your JavaScript, Node.js, or TypeScript project in {% data variables.product.prodname_codespaces %}. {% data reusables.gated-features.codespaces %}
free-pro-team
*
Codespaces

Introduction

This guide shows you how to set up your JavaScript, Node.js, or TypeScript project in {% data variables.product.prodname_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 {% data variables.product.prodname_dotcom_the_website %}. 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 {% data variables.product.prodname_codespaces %} enabled for your organization.

Step 1: Open your project in a codespace

  1. Navigate to your project's repository. Use the {% octicon "download" aria-label="The download icon" %} Code drop-down menu, and select Open with Codespaces. If you dont see this option, your project isnt available for {% data variables.product.prodname_codespaces %}.

Open with Codespaces button

  1. To create a new codespace, click {% octicon "plus" aria-label="The plus icon" %} New codespace. New codespace button

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.

{% data variables.product.prodname_codespaces %} uses a file called devcontainer.json to store configurations. On launch {% data variables.product.prodname_codespaces %} uses the file to install any tools, dependencies, or other set up that might be needed for the project. For more information, see "Configuring Codespaces for your project."

The next section shows you how to modify your tools by adding a dev container.

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 {% data variables.product.prodname_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 {% data variables.product.prodname_codespaces %} you can add this either from a template or you can create your own. For more information on dev containers, see Configuring your codespace.

This example guides you through adding a devcontainer.json file from a template.

  1. Access the command palette (shift command P / shift control P), then start typing "dev container". Click Codespaces: Add Development Container Configuration Files... "Codespaces: Add Development Container Configuration Files..." in the command palette
  2. For this example, click Node.js. In practice, you could select any container thats 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. To rebuild your container, access the command palette (shift command P / shift control P), then start typing "rebuild". Click Codespaces: Rebuild Container. Rebuild container option

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 - We can name our dev container anything, this is just the default
  • Build - Our build properties
    • Dockerfile - In our build object, Dockerfile is a reference to the Dockerfile in the same folder that was the second file added to our project. This is the reference path.
    • Args
      • Variant: We only have one build argument here which is the node variant we want to use which is passed into our Dockerfile.
  • Settings - These are {% data variables.product.prodname_vscode %} settings we wish to set
    • Terminal.integrated.shell.linux - While bash is the default here, we could use zsh for example 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 - By default we can forward a port, like port 3000, but these will also forward automatically
  • postCreateCommand - If we want to run anything after we land in our codespace thats not defined in our Dockerfile, like yarn install or npm install, we can do that here
  • remoteUser - Were running as the node 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 exand the .devcontainer folder to see it.

"Codespaces: Rebuild Container" in the command palette

  1. Add the following lines to your devcontainer.json file after extensions:
"postCreateCommand": "npm install",
"forwardPorts": [4000],

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

  1. To rebuild your container, access the command palette (shift command P / shift control P), then start typing "rebuild". Click Codespaces: Rebuild Container.

Rebuild container option

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, youll 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

  1. 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

Once you've made changes to your codespace, either new code or configuration changes, you'll want to commit your changes. Committing changes to your repository ensures that anyone else who creates a codespace from this repository has the same configuration. This also means that any customization you do, such as adding {% data variables.product.prodname_vscode %} extensions, will appear for all users.

  1. In the Activity Bar, click on the Source Control view.

Source control view

  1. To stage your changes, click +.

Explorer with staging button highlighted

  1. Type a commit message and then use Ctrl+Enter / cmd+Enter to commit the changes.

Explorer with commit message added

  1. To create a PR, click the PR icon.

Explorer with staging button highlighted

  1. Select the branches that you want to merge into, then click Create.

Explorer with staging button highlighted

Next steps

You should now be ready start developing your JavaScript project in {% data variables.product.prodname_codespaces %}. Here are some additional resources for more advanced scenarios.