如何更改 systemctl 服务的节点版本

How can i change node version for a systemctl service

我已经为 运行 节点应用程序创建了一个 systectl 服务,但是应用程序使用了 discordJs(npm 包),它只适用于节点 v16.* 及更高版本(我猜)。

现在的问题是,此服务使用的是节点 v10.21.0,这就是我的应用程序一启动就崩溃的原因!

我找不到更改此服务的节点版本的方法。


node系统版本为v16.13.1

animeland.service 配置:

[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

启动后的服务状态:

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'.

结论:

如果你们知道如何更改 systemctl 服务的节点版本,请告诉我这将是一个很大的帮助!

谢谢

所以我得到了我自己问题的答案

阅读此 github 问题线程: https://github.com/nodenv/nodenv/issues/117

然后来到这里: https://github.com/nodenv/nodenv/wiki/Use-with-systemd


如果你还没有弄明白,

获取您的节点(您要使用的)的位置

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

所以我想使用的节点位于/home/pi/.nvm/versions/node/v16.13.1/bin/node

然后我将其添加到节点应用程序的package.json

  ...,
  "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": [],
  ...

现在终于更新服务配置了:

[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

这里 npm run systemd 将 运行 使用我想要的特定节点版本的应用程序 (v16.13.1)