Matt Bierner
Matt Bierner

Reputation: 65175

Should a vscode dev container run npm install as part of its setup?

I've created a VS Code dev container for my project. To help new contributors get started, should the dev container Dockerfile run npm install?

Upvotes: 9

Views: 6776

Answers (2)

Delgan
Delgan

Reputation: 19617

It is actually good practice to call npm install as part of the dev container configuration. It eases and accelerate setup of the development environment, ensuring it's ready to use out of the box without extra steps possibly missed.

Note that the npm install must be in a postCreateCommand where sources and package.json are available (contrary to the Dockerfile).

Due to the nature of docker and devcontainer, there is very little reason for the npm install command to fail and need debugging. If it ever happen, VSCode logs will provide meaningful information, otherwise the postCreateCommand can be disabled quickly to investigate the issue.

There exists an official VSCode video, so we can assume calling npm install as part of the dev setup is a common practice: How to automatically run npm install in a dev container.

Upvotes: 9

Matt Bierner
Matt Bierner

Reputation: 65175

No. Dev container define a development environment and should not perform actions that would be part of a normal development workflow (such as running npm install).

There are a couple of reasons why npm install in particular should be avoided:

  • If you run npm install in the dev container Dockerfile and it fails, the dev container will fail to launch. It's farbetter if the developer can open the dev container successfully, and then run npm install in an environment where they can investigate potential failures

  • npm install must be re-run when the project's dependencies change. Running npm install as part of dev container setup hides this from developers.

Dev container should only use npm install to install global scripts or tools that do not belong in the project's package.json.

Upvotes: 6

Related Questions