Search code examples
node.jslinuxbashubuntuservice

How To Make Node Server Always Load Within Service


I have a simple service file that is supposed to run a bash script that starts a node js server in my "build" directory. Contents of the script are

#!/bin/bash
cd /home/path/to/build && npm install -g serve && serve -l 5679

However, I get the error => /usr/bin/env: ‘node’: No such file or directory Even when I try a symlink like so

#!/bin/bash
cd /home/path/to/build && ln -s /usr/bin/nodejs /usr/bin/node && npm install -g serve && serve -l 5679

I get the error => failed to create symbolic link ‘/usr/bin/node’: File exists

Please how do I fix this? I wouldn't want my user to have to manually start serving on each reboot. It also works if I add the script contents straight to terminal. Thanks for any help!


Solution

  • Turns out I had to install node with a different method (inspired by https://www.geeksforgeeks.org/installation-of-node-js-on-linux/) to ensure it actually existed in /usr/bin because ExecStart didn't like having the "serve" command working with my symlink node.

    Step 1: Open your terminal or press Ctrl + Alt + T and use the following commands to update and upgrade the package manager:

    sudo apt-get update
    sudo apt-get upgrade
    

    Step 2: Install Python software libraries using the following command:

    sudo apt-get install software-properties-common
    

    Step 3: Add Node.js PPA to the system.

    curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
    

    Note: Here, we are installing node.js version 14, if you want to install version 11, you can replace setup_14.x with setup_11.x.

    Step 4: To Install Node.js and NPM to your Ubuntu machine, use the command given below:

    sudo apt-get install nodejs
    

    Step 5: Once installed, verify it by checking the installed version using the following command:

    node -v or node –version
    npm -v or npm –version