Node.js and nvm commands cheat sheet

This cheat sheet brings together the commands for working with Node.js on a server: installing and managing versions with nvm, a .nvmrc file for each project, useful node options, and running the application as a permanent service. You can search for a command, filter by topic, and copy it with one click.

Checking the version and environment

  • node -v

    Shows the active version of Node.js. The full form is node --version.

  • npm -v

    Shows the version of npm, which comes with Node.js.

  • which node

    Shows the full path of the node binary the terminal runs. With an nvm installation, the path is inside the user's ~/.nvm directory.

  • node -p "process.versions"

    Prints the versions of Node.js and of the components built into it, such as V8 and OpenSSL.

Installing nvm

  • curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.41.0/install.sh | bash

    Downloads and runs the official nvm install script. The script installs nvm in the current user's ~/.nvm directory and adds the lines that load it to the profile file, for example ~/.bashrc. The version number in the URL changes from time to time, so copy the current command from the nvm README on GitHub. Run it without sudo, as the user who will run the application.

  • wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.41.0/install.sh | bash

    The same installation with wget, for a server where curl isn't installed.

  • source ~/.bashrc

    Reloads the profile file, so the nvm command is available in the open terminal. You can also close the terminal and open a new connection.

  • command -v nvm

    Checks that the installation succeeded: the output is nvm. The which nvm command won't find it, because nvm is a shell function and not an executable file.

Installing Node.js versions

  • nvm ls-remote --lts

    Lists all the LTS versions available for installation. Without --lts, all versions are listed.

  • nvm install --lts

    Installs the latest LTS version and switches to it. This is the right choice for a production server. The first version you install becomes the default.

  • nvm install node

    Installs the latest version of Node.js, even when it isn't an LTS version. Suited to trying out new features, not to a production server.

  • nvm install 24

    Installs the latest version in a specific major release line, by the number in the command.

  • nvm ls

    Lists the versions installed for the user and their aliases, such as default.

  • nvm install --reinstall-packages-from=current 'lts/*'

    Installs the latest LTS version and moves the global npm packages from the active version to it. Handy when you move to a new LTS version.

  • nvm install-latest-npm

    Updates npm to the latest version that supports the active Node.js version.

Switching versions

  • nvm use --lts

    Switches to the latest LTS version among the installed versions. The switch applies to the current terminal only.

  • nvm use node

    Switches to the latest version among the installed versions.

  • nvm alias default --lts node

    Sets the latest LTS version among the installed versions as the default for every new terminal.

  • nvm current

    Shows the version that's active in the current terminal.

  • nvm which node

    Shows the full path of the node binary of an installed version, in the example the latest version. This is the path you write in a systemd service file and in cron jobs.

  • nvm deactivate

    Undoes the effect of nvm in the current terminal, so the node command points to the system's Node.js again, if it's installed.

A .nvmrc file for each project

  • echo "lts/*" > .nvmrc

    Creates a .nvmrc file in the project directory, which sets the project to work with the latest LTS version. You can also write an exact version number in the file. When you commit the file to the code repository, everyone who works on the project, and the server too, uses the same version.

  • nvm install

    Without a version number, installs the version set in the .nvmrc file of the current directory.

  • nvm use

    Without a version number, switches to the version set in the .nvmrc file.

  • nvm which

    Without a version number, shows the path of the node binary for the version set in the .nvmrc file.

Removing versions

  • nvm uninstall 22

    Caution: removes an installed version, by the number in the command, together with the global packages installed in it. You can't remove the version that's active in the terminal, so switch to another version with nvm use first. Before you remove it, make sure no service uses the path of that version.

Running node

  • node app.js

    Runs the application in the terminal. When the terminal closes, the application stops, so on a production server, run it as a service.

  • node --check app.js

    Checks the file's syntax without running it. Handy before you deploy a new version to the server.

  • node --watch app.js

    Restarts the application whenever the main file or a file it loads changes. Suited to a development environment.

  • node --run build

    Runs a script from package.json, in the example build, directly in Node.js and without npm. When the script name doesn't exist, the list of scripts is shown.

  • node -p "os.cpus().length"

    Evaluates an expression and prints the result, in the example the number of CPUs the operating system reports. The node -e command runs code without printing the result.

Environment variables and memory

  • NODE_ENV=production node app.js

    Starts the application in production mode. Node.js itself doesn't change its behavior based on the variable, but some libraries read it and turn off development messages and checks in production mode. On a production server, always set NODE_ENV=production.

  • node --env-file=.env app.js

    Loads environment variables from a .env file without an extra package. When a variable is set both in the environment and in the file, the value from the environment wins. The option is stable in the current LTS versions, and when the file is missing, an error is shown.

  • node --env-file-if-exists=.env app.js

    Like --env-file, but continues without an error when the file doesn't exist.

  • node --max-old-space-size=2048 app.js

    Sets the maximum memory for the application's objects (old space) in MiB, in the example 2048. As the application gets close to the limit, Node.js spends more time on garbage collection. Leave free memory for the other services on the server too.

  • NODE_OPTIONS="--max-old-space-size=2048" npm run build

    Passes options to Node.js through an environment variable, even when the application is started through npm, PM2, or a build tool. Handy when the build of a large project fails because it runs out of memory. Options written on the command line take precedence over NODE_OPTIONS.

Debugging

  • node --inspect app.js

    Caution: starts the debugger (inspector) on 127.0.0.1:9229, which is reachable only from within the server. Never use --inspect=0.0.0.0 on a server that's open to the internet, because anyone who reaches the port can run code on the server. When you finish debugging, restart the application without --inspect.

  • ssh -L 9229:127.0.0.1:9229 deploy@server.example.com

    Opens an SSH tunnel from your computer to the debugger on the server, so you connect to it from your local browser without opening the port in the firewall.

Corepack: pnpm and Yarn

  • npm install -g corepack

    Installs Corepack, which manages the versions of pnpm and Yarn for each project. The Node.js developers decided to stop bundling Corepack with Node.js: the older major versions still include it, and in the new versions, including the current LTS version, you install it separately. Before you install it, remove any global installation of Yarn and pnpm with the npm uninstall -g yarn pnpm command.

  • corepack enable pnpm

    Creates the pnpm command, which Corepack runs in the version the project sets. When you don't specify a name, the pnpm and Yarn commands are created together.

  • corepack use pnpm@latest

    Sets the project to the latest version of pnpm, records it in the packageManager field of package.json, and installs the dependencies.

  • corepack up

    Updates the project's package manager to the latest version within the same major version.

Running as a systemd service

  • sudo nano /etc/systemd/system/nodeapp.service

    Creates a new systemd service file, in the example named nodeapp.

  • [Unit] Description=Node.js app After=network.target [Service] User=deploy WorkingDirectory=/home/deploy/app Environment=NODE_ENV=production Environment=PORT=3000 ExecStart=/usr/bin/node app.js Restart=on-failure RestartSec=5 [Install] WantedBy=multi-user.target

    A sample service file: the application runs as the deploy user and not as root, from the application directory, with production environment variables, and restarts automatically after a crash. In ExecStart, write the full path to the node binary. With an nvm installation, replace /usr/bin/node with the output of nvm which, and update it after you switch to another version.

  • sudo systemctl daemon-reload

    Reloads the service files after you create or edit the file.

  • sudo systemctl enable --now nodeapp

    Starts the service now, and on every server boot as well.

  • sudo systemctl restart nodeapp

    Restarts the application, for example after you deploy a new version.

  • systemctl status nodeapp

    Shows the status of the service and the latest lines in its log.

  • journalctl -u nodeapp -f

    Shows the application's log in real time, including its output and errors. Press Ctrl+C to exit the view.

Every major version of Node.js has a fixed lifecycle. In the Current phase, it gets new features for about six months, and in the LTS (Long Term Support) phase, it gets bug and security fixes for about two and a half years. In the major versions released so far, only the even-numbered versions move to LTS, and the odd-numbered versions reach end of life after six months. The Node.js developers have set a new schedule that applies from the next major version: one major version a year, which starts in an Alpha phase for testing, after which every version moves to the Current phase and, at its end, to LTS. The Node.js developers recommend running production applications only on an LTS version, so install an LTS version on the server and plan the move to a new LTS version before support ends. The dates for each version are on the Node.js lifecycle page.

nvm is installed separately for each user, in their home directory, and is loaded through the shell's profile file. That's why the node command is available only to the user who installed nvm, and only in a terminal that loaded it. A systemd service and a cron job don't load the profile file, so write the full path to the node binary in them, which you get with the nvm which command. The pm2 startup command also saves the path of the version that was active when you set it up, so run it again after you switch versions. The alternative is a system-wide installation: the nodejs package from the distribution's repositories, in the version the distribution chooses, updated together with system updates, or the NodeSource package repository, which installs the release line of your choice system-wide. nvm fits when you need several versions side by side, or when you work without root privileges.

On a production server, the application listens on a local port, for example 3000, and an Nginx server in front of it receives the requests from the internet, handles the SSL certificate, and passes them to the application (reverse proxy). Ready-made configurations are on the Nginx cheat sheet. Don't open the application's port in the firewall, as explained on the UFW commands and firewalld commands pages. Run the application as a regular user and not as root, either as a systemd service like the example on this page, or with PM2, which adds cluster mode, log management, and new version deploys without downtime.

Did you know?

On a Node.js VPS, the environment is ready in advance: Node.js comes preinstalled and configured on delivery, and you get full root access over SSH, so you upload your application and start it right away, and from then on, the versions are fully in your hands. On a Linux VPS, you connect over SSH with full root access, install Node.js the way that suits you, with nvm or system-wide, and run every command on this page.

Node.js VPS plans

Node.js and nvm FAQ

Which Node.js version is right for a production server?

Choose an LTS version. The Node.js developers recommend running production applications only on LTS versions, which get bug and security fixes over a long period. A Current version is suited to trying out new features. With nvm, install the latest LTS version with the nvm install --lts command. You can check the support dates for each version on the Node.js lifecycle page.

What's the advantage of installing Node.js with nvm?

nvm lets you install several versions of Node.js side by side and switch between them with one command. The installation goes into the user's home directory, without sudo, and global npm installations don't require root privileges either. This way, you can test a new LTS version and easily go back to the previous one.

How does each project work with its own Node.js version?

Save a .nvmrc file with the version in the project directory, for example lts/* or an exact version number. Inside the directory, run nvm use, and nvm switches to the version in the file, or nvm install when that version isn't installed yet. When you commit the file to the code repository, everyone who works on the project, and the server too, uses the same version.

How does a Node.js application start automatically with the server?

Create a systemd service file with the user name, the application directory, and the full path to the node binary, and enable it with the systemctl enable --now command. systemd then starts the application on every server boot and restarts it if it crashes. A convenient alternative is PM2, which adds cluster mode and log management.

How do I increase the memory available to a Node.js application?

Add the --max-old-space-size option with the size in MiB when you start the application, for example node --max-old-space-size=2048 app.js. When the application is started through npm or a build tool, pass the option in the NODE_OPTIONS environment variable. On a VPS with guaranteed resources, you can give the application the memory it needs and leave enough memory for the other services.

Shopping cart

Your cart is emptyYou can start with one of our popular plans.

Site search

Accessibility

Text size
100%
Accessibility statement