> ## Documentation Index
> Fetch the complete documentation index at: https://developer.upsun.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Manage Node.js versions

> See how to manage different Node.js versions in your Upsun Cloud containers."

> [!TIP]
>
> You can now use composable image to install runtimes and tools in your application container. To find out more, see the [Composable image](https://developer.upsun.com/docs/configure-apps/app-reference/composable-image.md) topic.

Each Upsun Cloud container image includes a specific language in a specific version.
A set of dependencies is also provided based on that language version.
This ensures that your application container is as small and efficient as possible.

Therefore, by default, when you use an Upsun Cloud container image,
you use the Node.js version that's included in that image, if any.

If you want to use a different Node.js version, use a version manager to install it yourself.
You can use one of the following version managers:

* [Use `n`](#use-n)
* [Use `nvm`](#use-nvm)

Both of the recommendations use a `.nvmrc` file to specify the desired Node.js version.
You could also specify a different file or use [environment variables](https://developer.upsun.com/docs/development/variables).

## Use `n`

The [`n` package](https://github.com/tj/n) works for various Unix-like systems,
including Windows Subsystem for Linux.

1. Add the desired Node.js version to your environment using `.nvmrc`, `.n-node-version`, `.node-version`, or `package.json`.

> [tab] .nvmrc
>
> Create a `.nvmrc` file in [your app root](https://developer.upsun.com/docs/configure-apps/app-reference/single-runtime-image.md#root-directory):
>
> ```yaml .nvmrc theme={null}
> v16.13.2
> ```

> [tab] .n-node-version/.node-version
>
> Create a `.n-node-version` or `.node-version` file in [your app root](https://developer.upsun.com/docs/configure-apps/app-reference/single-runtime-image.md#root-directory):
>
> ```yaml .n-node-version or .node-version theme={null}
> 16.13.2
> ```

> [tab] package.json
>
> Add an `engines.node` property to your `package.json`.
> This property accepts either an exact version or a range:
>
> ```json package.json theme={null}
> {
>   "engines": {
>     "node": ">=0.10.3 <15"
>   }
> }
> ```

2. Add it as a dependency:


&#x20;   applications:
&#x20;     \# The app's name, which must be unique within the project.
&#x20;     myapp:
&#x20;       type: 'python:\{\{version:python:latest}}'
&#x20;       dependencies:
&#x20;         nodejs:
&#x20;           n: "\*"

Adding it as a dependency ensures it's cached for future builds.

3. Set the location of the `n` files using the `N_PREFIX` environment variable:


&#x20;   applications:
&#x20;     \# The app's name, which must be unique within the project.
&#x20;     myapp:
&#x20;       type: 'python:\{\{version:python:latest}}'
&#x20;       dependencies:
&#x20;         nodejs:
&#x20;           n: "\*"
&#x20;       variables:
&#x20;         env:
&#x20;           N\_PREFIX: /app/.global

4. Install the specified version of Node.js in a [`build` hook](https://developer.upsun.com/docs/configure-apps/hooks/hooks-comparison#build-hook):


&#x20;   applications:
&#x20;     \# The app's name, which must be unique within the project.
&#x20;     myapp:
&#x20;       type: 'python:\{\{version:python:latest}}'
&#x20;       dependencies:
&#x20;         nodejs:
&#x20;           n: "\*"
&#x20;       variables:
&#x20;         env:
&#x20;           N\_PREFIX: /app/.global
&#x20;       hooks:
&#x20;         build: |
&#x20;           \# Exit the hook on any failure
&#x20;           set -e

&#x20;           \# Install the version specified in the .nvmrc file
&#x20;           n auto

&#x20;           \# Reset the location hash to recognize the newly installed version
&#x20;           hash -r

Now your hooks should be able to use the specified version of Node.js.
You can verify this by running `node -v`.

Your final app configuration should look something like this:


&#x20;   applications:
&#x20;     \# The app's name, which must be unique within the project.
&#x20;     myapp:
&#x20;       type: 'python:\{\{version:python:latest}}'
&#x20;       dependencies:
&#x20;         nodejs:
&#x20;           n: "\*"
&#x20;       variables:
&#x20;         env:
&#x20;           N\_PREFIX: /app/.global
&#x20;       hooks:
&#x20;         build: |
&#x20;           \# Exit the hook on any failure
&#x20;           set -e

&#x20;           \# Install the version specified in the .nvmrc file
&#x20;           n auto

&#x20;           \# Reset the location hash to recognize the newly installed version
&#x20;           hash -r

## Use `nvm`

[Node Version Manager (`nvm`)](https://github.com/nvm-sh/nvm) is a bash script for managing Node.js versions.

You can use it to:

* Make a specific version available in the build and optionally the runtime container.
* Control the specific versions to be installed with [environment variables](https://developer.upsun.com/docs/development/variables),
  meaning you can also have different versions in different environments.

To use `nvm`, follow these steps:

1. Define which `nvm` version to use using an [environment variable](https://developer.upsun.com/docs/development/variables).
   Add it to your [app configuration](https://developer.upsun.com/docs/configure-apps):


&#x20;   applications:
&#x20;     \# The app's name, which must be unique within the project.
&#x20;     myapp:
&#x20;       type: 'python:\{\{version:python:latest}}'
&#x20;       variables:
&#x20;         env:
&#x20;           \# Update for your desired NVM version.
&#x20;           NVM\_VERSION: v0.39.3

2. Define your desired Node.js version using an environment variable.

   For your base version, set it in your app configuration:


&#x20;   applications:
&#x20;     \# The app's name, which must be unique within the project.
&#x20;     myapp:
&#x20;       type: 'python:\{\{version:python:latest}}'
&#x20;       variables:
&#x20;         env:
&#x20;           \# Update these for your desired NVM and Node versions.
&#x20;           NVM\_VERSION: v0.39.3
&#x20;           NODE\_VERSION: v18.14.2

To get different versions in different environments, [set environment-specific variables](https://developer.upsun.com/docs/development/variables/set-variables.md#create-environment-specific-variables).

3. Add a `.nvm` directory to your cache in your [build hook](https://developer.upsun.com/docs/configure-apps/hooks):


&#x20;   applications:
&#x20;     \# The app's name, which must be unique within the project.
&#x20;     myapp:
&#x20;       type: 'python:\{\{version:python:latest}}'
&#x20;       variables:
&#x20;         env:
&#x20;           \# Update these for your desired NVM and Node versions.
&#x20;           NVM\_VERSION: v0.39.3
&#x20;           NODE\_VERSION: v18.14.2
&#x20;       hooks:
&#x20;         build: |
&#x20;           set -e
&#x20;           unset NPM\_CONFIG\_PREFIX
&#x20;           export NVM\_DIR="\$PLATFORM\_APP\_DIR/.nvm"

&#x20;           \# Link cache with app
&#x20;           if \[ ! -d "\$PLATFORM\_CACHE\_DIR/.nvm" ]; then
&#x20;               mkdir -p \$PLATFORM\_CACHE\_DIR/.nvm
&#x20;           fi
&#x20;           ln -s \$PLATFORM\_CACHE\_DIR/.nvm \$NVM\_DIR

> [!NOTE]
>
> Instead of using a symlink between your cache and application directories,
> you might need to copy the content of `$PLATFORM_CACHE_DIR/.nvm` into `$PLATFORM_APP_DIR/.nvm` manually.
> To do so, run the following command:
>
> ```bash theme={null}
> rsync -av $PLATFORM_CACHE_DIR/.nvm $PLATFORM_APP_DIR
> ```

4. Use the cache directory and install based on the variables if not present:


&#x20;   applications:
&#x20;     \# The app's name, which must be unique within the project.
&#x20;     myapp:
&#x20;       type: 'python:\{\{version:python:latest}}'
&#x20;       variables:
&#x20;         env:
&#x20;           \# Update these for your desired NVM and Node versions.
&#x20;           NVM\_VERSION: v0.39.3
&#x20;           NODE\_VERSION: v18.14.2
&#x20;       hooks:
&#x20;         build: |
&#x20;           ...
&#x20;           \# Check for Node.js version and install if not present
&#x20;           if \[ ! -d "\$PLATFORM\_CACHE\_DIR/.nvm/versions/node/\$NODE\_VERSION" ]; then

&#x20;             \# Get nvm install script if correct version not present
&#x20;             export NVM\_INSTALL\_FILE="\$\{PLATFORM\_CACHE\_DIR}/nvm\_\$\{NVM\_VERSION}\_install.sh"
&#x20;             if \[ ! -f "\$NVM\_INSTALL\_FILE" ]; then
&#x20;                 wget -nc -O "\$NVM\_INSTALL\_FILE" "https\://raw\.githubusercontent.com/nvm-sh/nvm/\$NVM\_VERSION/install.sh"
&#x20;             fi

&#x20;             \# Install, automatically using NODE\_VERSION
&#x20;             bash \$NVM\_INSTALL\_FILE
&#x20;           fi

&#x20;           \# Activate nvm
&#x20;           \[ -s "\$NVM\_DIR/nvm.sh" ] && \\. "\$NVM\_DIR/nvm.sh"

&#x20;           \# Use the specified version
&#x20;           nvm use "\$NODE\_VERSION"

5. Optional: To use the specified Node.js version in the runtime container and not just the build,
   activate `nvm` via [script](https://developer.upsun.com/docs/development/variables/set-variables.md#set-variables-via-script):

   ```bash .environment theme={null}
   unset NPM_CONFIG_PREFIX
   export NVM_DIR="$PLATFORM_APP_DIR/.nvm"
   [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
   ```

   Your final app configuration should look something like the following:


&#x20;   applications:
&#x20;     \# The app's name, which must be unique within the project.
&#x20;     myapp:
&#x20;       type: 'python:\{\{version:python:latest}}'
&#x20;       variables:
&#x20;         env:
&#x20;           \# Update these for your desired NVM and Node versions.
&#x20;           NVM\_VERSION: v0.39.3
&#x20;           NODE\_VERSION: v18.14.2
&#x20;       hooks:
&#x20;         build: |
&#x20;           set -e
&#x20;           unset NPM\_CONFIG\_PREFIX
&#x20;           export NVM\_DIR="\$PLATFORM\_APP\_DIR/.nvm"

&#x20;           \# Link cache with app
&#x20;           if \[ ! -d "\$PLATFORM\_CACHE\_DIR/.nvm" ]; then
&#x20;             mkdir -p \$PLATFORM\_CACHE\_DIR/.nvm
&#x20;           fi
&#x20;           ln -s \$PLATFORM\_CACHE\_DIR/.nvm \$NVM\_DIR
&#x20;           \# Check for Node.js version and install if not present
&#x20;           if \[ ! -d "\$PLATFORM\_CACHE\_DIR/.nvm/versions/node/\$NODE\_VERSION" ]; then

&#x20;             \# Get nvm install script if correct version not present
&#x20;             export NVM\_INSTALL\_FILE="\$\{PLATFORM\_CACHE\_DIR}/nvm\_\$\{NVM\_VERSION}\_install.sh"
&#x20;             if \[ ! -f "\$NVM\_INSTALL\_FILE" ]; then
&#x20;                 wget -nc -O "\$NVM\_INSTALL\_FILE" "https\://raw\.githubusercontent.com/nvm-sh/nvm/\$NVM\_VERSION/install.sh"
&#x20;             fi

&#x20;             \# Install, automatically using NODE\_VERSION
&#x20;             bash \$NVM\_INSTALL\_FILE
&#x20;           fi

&#x20;           \# Activate nvm
&#x20;           \[ -s "\$NVM\_DIR/nvm.sh" ] && \\. "\$NVM\_DIR/nvm.sh"

&#x20;           \# Use the specified version
&#x20;           nvm use "\$NODE\_VERSION"


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.