Search code examples
node.jssveltesveltekitsystemctl

SvelteKit build (node adapter) outputs error when attempting to start it as a Systemd service but works fine when starting it directly


I'm attempting to deploy my SvelteKit App to a linux server and have it auto-start and auto-restart.

When launching it directly with node index.js as root user it works perfectly fine but when attempting to launch it as a systemd service or crontab it outputs the following error to my errors.log file:

SyntaxError: Unexpected token '?'
    at Loader.moduleStrategy (internal/modules/esm/translators.js:133:18)
file:///*path-to-app*/handler.js:768
    const DOMException = globalThis.DOMException ?? (() => {

This is my service systemctl file in /lib/systemd/system/.

[Unit]
Description = *description*

[Service]
Type           = simple
User           = root
Group          = root
LimitNOFILE    = 4096
Restart        = always
RestartSec     = 10s
StandardOutput = append:/*path-to-app*/logs.log
StandardError  = append:/*path-to-app*/errors.log
ExecStart      = node /*path-to-app*/index.js

[Install]
WantedBy = multi-user.target

When trying to troubleshoot, a lot of responses say that this happens on older NodeJS versions but on my server version 18.14.2 is the only one that is installed. Another common tip is to add "type": "module" to my package.json file which I have already done. This also happens when building an empty skeleton project with the newest SvelteKit version and node adapter. Can anyone tell my why the same command by the same user leads to different outcomes? Help would be greatly appreciated.

Node Version: 18.14.2

OS: Ubuntu 22.04.2 LTS


Solution

  • radavenport84 on reddit helped my find an answer. Running which node in the service revealed that is was using a different node version in "/usr/bin/node/" that must've been installed by default or something. The only version I ever installed was 18.14.2 through nvm. But specifying the full path to the 18.14.2 version in ExecStart fixed my problem:

     ExecStart = /root/.nvm/versions/node/v18.14.2/bin/node /*path-to-app*/index.js