Michael Anello
Michael Anello

Reputation: 129

How can I add and use nvm in a DDEV web container?

Currently, the DDEV web container does not come with nvm (node version manager). How can I add and use it via the DDEV config.yaml file?

Upvotes: 8

Views: 4349

Answers (4)

rfay
rfay

Reputation: 12905

[Updated 2024-06-11] In current DDEV nvm is installed by default, and can be used with ddev nvm, so you don't have to do any of this. See docs.

However, nvm is a fairly tweaky and inconsistent tool, so we now recommend that in most cases you use nodejs_version, which can be used with any available node version, not just current ones, and can be used with major, minor, or patch versions.

So you can do nodejs_version: 20 or nodejs_version: 22.3 or nodejs_version: 21.7.3

Upvotes: 15

Mario Hernandez
Mario Hernandez

Reputation: 131

After using @rfay solution above for installing specific version of Node, I created a custom command in .ddev/commands/web which allows me to run any nvm command in the container without having to ddev ssh.

#!/bin/bash
## Description: Install or use node.
## Usage: nvm [flags] [args]
## Example: "nvm use or nvm install"
source /etc/profile && nvm $@

Upvotes: 3

Ryan McVeigh
Ryan McVeigh

Reputation: 181

Another potential solution would be to add it to the .ddev/web-build/Dockerfile as the DDEV documentation suggests here: https://ddev.readthedocs.io/en/stable/users/extend/customizing-images/#adding-extra-dockerfiles-for-webimage-and-dbimage

Upvotes: 2

Michael Anello
Michael Anello

Reputation: 129

With the help of @greggles and @heddn on the #ddev Slack channel (on the Drupal Slack workspace), I got it working with the following post-start hook:

hooks:
 post-start:
   - exec: curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
   - exec: rm -f ../.nvmrc && export NVM_DIR="$HOME/.nvm" && source "$NVM_DIR/nvm.sh" && nvm install 8.11.1 && nvm use 8.11.1

This installs nvm then sets node to version 8.11.1

-mike

Upvotes: 4

Related Questions