Skip to content

Instantly share code, notes, and snippets.

@joepie91
Last active June 20, 2025 08:12

Revisions

  1. joepie91 revised this gist Jul 3, 2019. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions .md
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,8 @@ Hi there! Since this post was originally written, `nvm` has gained some new tool

    -----

    # The original article

    Trickier than it seems.

    ## 1. Set up nvm
  2. joepie91 revised this gist Jul 3, 2019. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions .md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,9 @@
    # Read this first!

    Hi there! Since this post was originally written, `nvm` has gained some new tools, and some people have suggested alternative (and potentially better) approaches for modern systems. Make sure to have a look at the comments to this article, *before* following this guide!

    -----

    Trickier than it seems.

    ## 1. Set up nvm
  3. joepie91 revised this gist Apr 2, 2016. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions .md
    Original file line number Diff line number Diff line change
    @@ -56,6 +56,12 @@ npm start

    This script is necessary, because we can't load nvm via the service file directly.

    Make sure to make it executable:

    ```sh
    chmod +x /home/myapp/start.sh
    ```

    ## 5. Enable and start your service

    Replace `my-application` with whatever you've named your service file after, running the following __as root__:
  4. joepie91 revised this gist Apr 2, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion .md
    Original file line number Diff line number Diff line change
    @@ -58,7 +58,7 @@ This script is necessary, because we can't load nvm via the service file directl

    ## 5. Enable and start your service

    Replace `my-application` with whatever you've named your service file after.
    Replace `my-application` with whatever you've named your service file after, running the following __as root__:

    1. `systemctl enable my-application`
    2. `systemctl start my-application`
  5. joepie91 revised this gist Apr 2, 2016. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions .md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    Trickier than it seems.

    # 1. Set up nvm
    ## 1. Set up nvm

    Let's assume that you've already created an unprivileged user named `myapp`. You should never run your Node.js applications as root!

    @@ -9,7 +9,7 @@ Switch to the `myapp` user, and do the following:
    1. `curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash` (however, this will immediately run the nvm installer - you probably want to just download the `install.sh` manually, and inspect it before running it)
    2. Install the latest stable Node.js version: `nvm install stable`

    # 2. Prepare your application
    ## 2. Prepare your application

    Your package.json must specify a `start` script, that describes what to execute for your application. For example:

    @@ -21,7 +21,7 @@ Your package.json must specify a `start` script, that describes what to execute
    ...
    ```

    # 3. Service file
    ## 3. Service file

    Save this as `/etc/systemd/system/my-application.service`:

    @@ -44,7 +44,7 @@ WantedBy=multi-user.target

    You'll want to change the `User`, `Description` and `ExecStart`/`WorkingDirectory` paths to reflect your application setup.

    # 4. Startup script
    ## 4. Startup script

    Next, save this as `/home/myapp/start.sh` (adjusting the username in both the path *and* the script if necessary):

    @@ -56,7 +56,7 @@ npm start

    This script is necessary, because we can't load nvm via the service file directly.

    # 5. Enable and start your service
    ## 5. Enable and start your service

    Replace `my-application` with whatever you've named your service file after.

  6. joepie91 created this gist Apr 2, 2016.
    75 changes: 75 additions & 0 deletions .md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    Trickier than it seems.

    # 1. Set up nvm

    Let's assume that you've already created an unprivileged user named `myapp`. You should never run your Node.js applications as root!

    Switch to the `myapp` user, and do the following:

    1. `curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash` (however, this will immediately run the nvm installer - you probably want to just download the `install.sh` manually, and inspect it before running it)
    2. Install the latest stable Node.js version: `nvm install stable`

    # 2. Prepare your application

    Your package.json must specify a `start` script, that describes what to execute for your application. For example:

    ```json
    ...
    "scripts": {
    "start": "node app.js"
    },
    ...
    ```

    # 3. Service file

    Save this as `/etc/systemd/system/my-application.service`:

    ```
    [Unit]
    Description=My Application
    [Service]
    EnvironmentFile=-/etc/default/my-application
    ExecStart=/home/myapp/start.sh
    WorkingDirectory=/home/myapp/my-application-directory
    LimitNOFILE=4096
    IgnoreSIGPIPE=false
    KillMode=process
    User=myapp
    [Install]
    WantedBy=multi-user.target
    ```

    You'll want to change the `User`, `Description` and `ExecStart`/`WorkingDirectory` paths to reflect your application setup.

    # 4. Startup script

    Next, save this as `/home/myapp/start.sh` (adjusting the username in both the path *and* the script if necessary):

    ```sh
    #!/bin/bash
    . /home/myapp/.nvm/nvm.sh
    npm start
    ```

    This script is necessary, because we can't load nvm via the service file directly.

    # 5. Enable and start your service

    Replace `my-application` with whatever you've named your service file after.

    1. `systemctl enable my-application`
    2. `systemctl start my-application`

    To verify whether your application started successfully (don't forget to `npm install` your dependencies!), run:

    ```sh
    systemctl status my-application
    ```

    ... which will show you the last few lines of its output, whether it's currently running, and any errors that might have occurred.

    Done!