Search code examples
node.jslinuxserviceautomationsystemd

How can i change node version for a systemctl service


I've created a systectl service to run a node application, but application uses discordJs (npm package) which only works with node v16.* and above (i guess).

Now problem is, this service is using node v10.21.0 & that's why my application is crashing as soon it starts!

I can't find a way to change node version for this service.


node version of system is v16.13.1

animeland.service config:

[Unit]
Description=bot for animeland server
Wants=network-online.target
After=network.target

[Service]
Type=simple
User=pi
WorkingDirectory=/home/pi/bots/chika-bot/
ExecStart=npm start

[Install]
WantedBy=multi-user.target

status of service after starting:

pi@rootz491:~ $ sudo systemctl start animeland.service 
pi@rootz491:~ $ sudo systemctl status animeland.service 
● animeland.service - bot for animeland server
   Loaded: loaded (/etc/systemd/system/animeland.service; disabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Fri 2022-01-28 16:44:42 IST; 5s ago
  Process: 2195 ExecStart=/usr/bin/npm start (code=exited, status=1/FAILURE)
 Main PID: 2195 (code=exited, status=1/FAILURE)

Jan 28 16:44:42 rootz491 npm[2195]: npm ERR! errno 1
Jan 28 16:44:42 rootz491 npm[2195]: npm ERR! chika@1.0.0 start: `node index.js`
Jan 28 16:44:42 rootz491 npm[2195]: npm ERR! Exit status 1
Jan 28 16:44:42 rootz491 npm[2195]: npm ERR!
Jan 28 16:44:42 rootz491 npm[2195]: npm ERR! Failed at the chika@1.0.0 start script.
Jan 28 16:44:42 rootz491 npm[2195]: npm ERR! This is probably not a problem with npm. There is likely additional l
Jan 28 16:44:42 rootz491 npm[2195]: npm ERR! A complete log of this run can be found in:
Jan 28 16:44:42 rootz491 npm[2195]: npm ERR!     /home/pi/.npm/_logs/2022-01-28T11_14_42_719Z-debug.log
Jan 28 16:44:42 rootz491 systemd[1]: animeland.service: Main process exited, code=exited, status=1/FAILURE
Jan 28 16:44:42 rootz491 systemd[1]: animeland.service: Failed with result 'exit-code'.

conclusion:

if you guys know how to change node version of systemctl service, please leme know it'll be a great help!

thankyou


Solution

  • so i got the answer to my own question 😅

    read this github issue thread: https://github.com/nodenv/nodenv/issues/117

    then came here: https://github.com/nodenv/nodenv/wiki/Use-with-systemd


    if you still haven't figured out,

    get localtion of your node (that you want to use)

    pi@rootz491:~ $ whereis node
    node: /usr/bin/node /usr/local/bin/node /home/pi/.nvm/versions/node/v16.13.1/bin/node /usr/share/man/man1/node.1.gz
    

    So node that i want to use is located at /home/pi/.nvm/versions/node/v16.13.1/bin/node.

    Then i added this to package.json of node app:

      ...,
      "main": "index.js",
      "scripts": {
        "systemd": "/home/pi/.nvm/versions/node/v16.13.1/bin/node index.js",
        "start": "node index.js",
        "dev": "nodemon index.js",
      },
      "keywords": [],
      ...
    

    now finally update service config:

    [Unit]
    Description=bots for animeland server
    Wants=network-online.target
    After=network.target
    
    [Service]
    Type=simple
    User=pi
    WorkingDirectory=/home/pi/bots/chika-bot/
    ExecStart=npm run systemd
    
    [Install]
    WantedBy=multi-user.target
    

    Here npm run systemd will run the app using that specific version of node which i want (v16.13.1) 🙃