简体   繁体   English

无法找出我的服务有什么问题

[英]Cannot find out what's wrong with my service

I have a little node application.我有一个小节点应用程序。 It works fine when I run当我运行时它工作正常

pi@rpb:/opt/linear-actuator $ /home/pi/.nvm/versions/node/v14.8.0/bin/npm start

I created a service to make sure it starts automatically on boot.我创建了一个服务以确保它在启动时自动启动。

[Unit]
Description=LinearActuator
After=network.target

[Service]
WorkingDirectory=/opt/linear-actuator
ExecStart=/home/pi/.nvm/versions/node/v14.8.0/bin/npm start
Restart=always
User=pi
Environment=PORT=8081

[Install]
WantedBy=multi-user.target

But my service fails to start (I have enabled it before).但是我的服务无法启动(我之前已启用它)。 journalctl says journalctl 说

août 27 21:37:16 rpb npm[1913]: > linearactuator@1.0.0 start /opt/linear-actuator
août 27 21:37:16 rpb npm[1913]: > node server.js
août 27 21:37:18 rpb npm[1913]: /opt/linear-actuator/server.js:4
août 27 21:37:18 rpb npm[1913]: import express from 'express';
août 27 21:37:18 rpb npm[1913]: ^^^^^^
août 27 21:37:18 rpb npm[1913]: SyntaxError: Unexpected token import

I had this error when running my app with an older version of node.使用旧版本的节点运行我的应用程序时出现此错误。 But the absolute path used in my service is fine.但是我的服务中使用的绝对路径很好。

pi@rpb:/opt/linear-actuator $ which node
/home/pi/.nvm/versions/node/v14.8.0/bin/node
pi@rpb:/opt/linear-actuator $ node -v
v14.8.0
pi@rpb:/opt/linear-actuator $ which npm
/home/pi/.nvm/versions/node/v14.8.0/bin/npm
pi@rpb:/opt/linear-actuator $ npm -v
6.14.7

Any lighting on what I am missing ?关于我所缺少的任何照明? :) :)

The import syntax is only valid for modules. import语法仅对模块有效。 You should rename the file from server.js to server.mjs .您应该将文件从server.js重命名为server.mjs

The import syntax is only supported (without experimental flag) in Node 14 onwards.仅在 Node 14 及更高版本中支持import语法(无实验标志)。 Based on your node and npm versions you printed, it does appear that you have Node 14+ installed using nvm.根据您打印的 node 和 npm 版本,您似乎确实使用 nvm 安装了 Node 14+。 However, when your service is run, an older version of NodeJS is being picked up.但是,当您的服务运行时,会选择旧版本的 NodeJS。 Most probably something that you installed using the bundled package manager (apt?).很可能是您使用捆绑包管理器(apt?)安装的东西。

NVM doesn't store or link the node binaries in one of the standard linux binary locations (/usr/bin or /usr/local/bin). NVM 不会在标准 linux 二进制位置之一(/usr/bin 或 /usr/local/bin)中存储或链接节点二进制文件。 It however, adds some scripts in .profile or .bashrc (based on your terminal) to update the PATH env var with correct Node binary paths.但是,它会在 .profile 或 .bashrc(基于您的终端)中添加一些脚本,以使用正确的 Node 二进制路径更新 PATH 环境变量。 Typically:通常:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm

Now when your service is executed the systemd must be running your execStart command as is;现在,当您的服务执行时,systemd 必须按execStart运行您的execStart命令; without loading the .profile / .bashrc file, wherever the nvm script is executed from.不加载 .profile / .bashrc 文件,无论从哪里执行 nvm 脚本。 If you don't plan to be changing versions of the node often, you could forget about nvm and just set the PATH env var in your service as the current $PATH from your terminal.如果您不打算经常更改节点的版本,则可以忘记 nvm,只需将服务中的 PATH 环境变量设置为终端中的当前 $PATH。 Just run echo $PATH pick the result and paste is in the placeholder below.只需运行echo $PATH选择结果并粘贴在下面的占位符中。

[Unit]
Description=LinearActuator
After=network.target

[Service]
WorkingDirectory=/opt/linear-actuator
ExecStart=/home/pi/.nvm/versions/node/v14.8.0/bin/npm start
Restart=always
User=pi
Environment=PORT=8081
Environment=PATH=<Put the result of `echo $PATH` here>

[Install]
WantedBy=multi-user.target

A better approach would be to accomodate the nvm in your 'start command'.更好的方法是在“启动命令”中容纳 nvm。 Basically create a new start-my-server.sh file, make is executable and add a series of commands something like this (change paths to suit your setup):基本上创建一个新的start-my-server.sh文件,make 是可执行的,并添加一系列类似这样的命令(更改路径以适合您的设置):

#!/bin/bash
[ -s "/home/pi/.nvm/nvm.sh" ] && \. "/home/pi/.nvm/nvm.sh"
cd /opt/linear-actuator
npm start

After this, point the execStart in your service to this newly created shell script instead of running npm start directly.之后,将服务中的 execStart 指向这个新创建的 shell 脚本,而不是直接运行 npm start。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM