Binary from app using winston package don't run as linux service - node.js

I have a very simple node js application that uses the npm Winston package.
The code is only a loop printing date using the Winston package The app runs just fine. I created a binary using
npm run build
or
npx pkg -t linux,macos,win . --out-path dist
and it also runs just fine. the problem is that when I run it as a service using the file
[Unit]
Description=Cryptween daemon service
After=systend-user-sessions.service
[Service]
Type=simple
WorkingDirectory=$CRYPTWEEN_BIN_DIR
ExecStart=$CRYPTWEEN_BIN_DIR/cryptween-daemon-linux
ExecReload=/bin/kill -HUP $MAINPID
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=CttWBot_Console
[Install]
WantedBy=multi-user.target
I get the error
cryptween.service - Cryptween daemon service
Loaded: loaded (/etc/systemd/system/cryptween.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Thu 2022-03-24 10:54:45 -03; 1s ago
Process: 213784 ExecStart=/opt/cryptween/bin/cryptween-daemon-linux (code=exited, status=1/FAILURE)
Main PID: 213784 (code=exited, status=1/FAILURE)
mar 24 10:54:45 gintama CttWBot_Console[213784]: at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
mar 24 10:54:45 gintama CttWBot_Console[213784]: at Module.load (node:internal/modules/cjs/loader:981:32)
mar 24 10:54:45 gintama CttWBot_Console[213784]: at Function.Module._load (node:internal/modules/cjs/loader:822:12)
mar 24 10:54:45 gintama CttWBot_Console[213784]: at Module.require (node:internal/modules/cjs/loader:1005:19)
mar 24 10:54:45 gintama CttWBot_Console[213784]: at Module.require (pkg/prelude/bootstrap.js:1719:31)
mar 24 10:54:45 gintama CttWBot_Console[213784]: at require (node:internal/modules/cjs/helpers:102:18)
mar 24 10:54:45 gintama CttWBot_Console[213784]: at Object.<anonymous> (/snapshot/test-error-daemon/src/config/winston-logger.js:2:16)
mar 24 10:54:45 gintama CttWBot_Console[213784]: at Module._compile (pkg/prelude/bootstrap.js:1794:22)
mar 24 10:54:45 gintama systemd[1]: cryptween.service: Main process exited, code=exited, status=1/FAILURE
mar 24 10:54:45 gintama systemd[1]: cryptween.service: Failed with result 'exit-code'.
Here is the code:
code
I am using ubuntu 20.04
Thank you in advance

Related

Core Node.js module "fs" Not Found when #fastify/static deployed on render.com

It is my understanding that the core modules, like "fs" are part of the node.js build, and no special configuration is needed to make them available for importing, so I'm at a loss as to how "fs" could be missing when running on render.com. I have no problems building or running in development mode locally. And the service deploys and builds perfectly on render.com, but running fails with:
Jan 31 01:13:22 PM ==> Starting service with 'node index.js'
Jan 31 01:13:23 PM internal/modules/cjs/loader.js:888
Jan 31 01:13:23 PM throw err;
Jan 31 01:13:23 PM ^
Jan 31 01:13:23 PM
Jan 31 01:13:23 PM **Error: Cannot find module 'node:fs'**
Jan 31 01:13:23 PM Require stack:
Jan 31 01:13:23 PM - /opt/render/project/src/node_modules/#fastify/send/lib/SendStream.js
Jan 31 01:13:23 PM - /opt/render/project/src/node_modules/#fastify/send/index.js
Jan 31 01:13:23 PM - /opt/render/project/src/node_modules/#fastify/static/index.js
Jan 31 01:13:23 PM - /opt/render/project/src/index.js
Jan 31 01:13:23 PM at Function.Module._resolveFilename (internal/modules/cjs/loader.js:885:15)
Jan 31 01:13:23 PM at Function.Module._load (internal/modules/cjs/loader.js:730:27)
Jan 31 01:13:23 PM at Module.require (internal/modules/cjs/loader.js:957:19)
Jan 31 01:13:23 PM at require (internal/modules/cjs/helpers.js:88:18)
Jan 31 01:13:23 PM at Object.<anonymous> (/opt/render/project/src/node_modules/#fastify/send/lib/SendStream.js:10:12)
Jan 31 01:13:23 PM at Module._compile (internal/modules/cjs/loader.js:1068:30)
Jan 31 01:13:23 PM at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
Jan 31 01:13:23 PM at Module.load (internal/modules/cjs/loader.js:933:32)
Jan 31 01:13:23 PM at Function.Module._load (internal/modules/cjs/loader.js:774:14)
Jan 31 01:13:23 PM at Module.require (internal/modules/cjs/loader.js:957:19) {
Jan 31 01:13:23 PM code: 'MODULE_NOT_FOUND',
Jan 31 01:13:23 PM requireStack: [
Jan 31 01:13:23 PM '/opt/render/project/src/node_modules/#fastify/send/lib/SendStream.js',
Jan 31 01:13:23 PM '/opt/render/project/src/node_modules/#fastify/send/index.js',
Jan 31 01:13:23 PM '/opt/render/project/src/node_modules/#fastify/static/index.js',
Jan 31 01:13:23 PM '/opt/render/project/src/index.js'
Jan 31 01:13:23 PM ]
Jan 31 01:13:23 PM }
The service was running merrily along until I deployed a new version today that requires the #fastify/static package, like this:
fastify.register(require('#fastify/static'), { root: path.join(__dirname,'public'), prefix:'/public/' })
I never import "fs" directly, but #fastify/static apparently does, like this:
const statSync = require('fs').statSync
I tried importing fs explicitly before importing #fastify, but the error doesn't change. Webpack is not involved. I've tried building using both npm and yarn, no difference - not that building should affect core modules. Is there some critical environmental setup I have neglected to do on Render.com?
Ronnie's comment (thks!) made me start wondering if my assumption that "node:fs" was the same as "fs" was correct. It was not. Render.com defaults to node.js version 14.17.0. The "node:" module reference syntax was not added until a later version.
The solution was to request node version 16.0.0 on render.com using an environment variable containing the version string, and it fixed the error.
The details for specifying the version on render.com is at https://render.com/docs/node-version
An explanation of the core modules syntax is at https://nodejs.org/api/modules.html

How can I start Docusaurus as a service on Linux (Ubuntu)

Hi guys I try to start Docusaurus V2 as a service on ubuntu headless. Currently I have a script in my Docusaurus folder as a temporarly solution. It works when I start it with ./start.sh:
/var/www/citro-docs-2/start.sh
#!/bin/bash
npm run serve
I tried to create a service like that:
/etc/systemd/system/docusaurus.service
[Unit]
Description=Docusaurus Service
[Service]
ExecStart=/var/www/citro-docs-2/start.sh
WorkingDirectory=/var/www/citro-docs-2
[Install]
WantedBy=multi-user.target
But when I start it with sudo systemctl start docusaurus.service and check the status I get these errors:
● docusaurus.service - Docusaurus Service
Loaded: loaded (/etc/systemd/system/docusaurus.service; disabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Wed 2021-08-25 11:53:20 UTC; 4s ago
Process: 3494 ExecStart=/var/www/citro-docs-2/start.sh (code=exited, status=1/FAILURE)
Main PID: 3494 (code=exited, status=1/FAILURE)
Aug 25 11:53:20 citro-docs2 start.sh[3521]: at require (node:internal/modules/cjs/helpers:94:18)
Aug 25 11:53:20 citro-docs2 start.sh[3521]: at Object.<anonymous> (/var/www/citro-docs-2/node_modules/#docusaurus/c>
Aug 25 11:53:20 citro-docs2 start.sh[3521]: at Module._compile (node:internal/modules/cjs/loader:1101:14)
Aug 25 11:53:20 citro-docs2 start.sh[3521]: at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153>
Aug 25 11:53:20 citro-docs2 start.sh[3521]: at Module.load (node:internal/modules/cjs/loader:981:32)
Aug 25 11:53:20 citro-docs2 start.sh[3521]: at Function.Module._load (node:internal/modules/cjs/loader:822:12)
Aug 25 11:53:20 citro-docs2 start.sh[3521]: at Function.executeUserEntryPoint [as runMain] (node:internal/modules/r>
Aug 25 11:53:20 citro-docs2 start.sh[3521]: at node:internal/main/run_main_module:17:47
Aug 25 11:53:20 citro-docs2 systemd[1]: docusaurus.service: Main process exited, code=exited, status=1/FAILURE
Aug 25 11:53:20 citro-docs2 systemd[1]: docusaurus.service: Failed with result 'exit-code'.
I found a solution where I can start the .js file directly: How do I run a node.js app as a background service? but I don't know if this is also possible for Docusaurus. At least I don't know which .js file I should start. It would be awesome if someone could help.
Yes, it is possible to run Docusaurus as a service on Ubuntu. It's important to mention that what you're trying to do here is quite normal for Node.js apps. After all, if you don't run your Node.js app as a service, then the service would stop running as soon as you close the shell you used to start the app.
However, it seems like most folks don't run Docusaurus on Ubuntu since it's "just a static site" and static sites don't really need a full blown server to run on. For example, you could leverage GitHub Pages to run your docs site.
That being said, Docusaurus is just a Node.js app. It's similar to all other Node.js apps, and you can certainly run it in Ubuntu if you wanted to.
Take a look at the docs on PM2.
The way I would start your app as a service is:
Install Node.js on your Ubuntu server (if it isn't already installed)
Install pm2 globally on your server via npm install -g pm2
Navigate to your app's directory and run npm install to install the dependencies of your app.
Now you can run pm2 start /var/www/citro-docs-2/start.sh --name myDocsSite
Your Docusaurus app should now be running. To stop it, you can run pm2 stop myDocsSite.

node systemctl for discord.js bot error EACCES

I want to start my discord.js bot with a systemctl.
When I enter
[Unit]
Description=Bots by Tjum28
After=network-online.target
[Service]
User=pi
Type=simple
ExecStart=/usr/bin/node /home/pi/Bots/bot.js
[Install]
WantedBy=multi-user.target
this in my .service file the bot starts but when I send a message I get this error:
Before I send a message:
● bot.service - Bots by Tjum28
Loaded: loaded (/lib/systemd/system/bot.service; disabled; vendor preset: enabled)
Active: active (running) since Thu 2021-03-25 19:01:05 CET; 13s ago
Main PID: 9627 (node)
Tasks: 11 (limit: 2062)
CGroup: /system.slice/bot.service
└─9627 /usr/bin/node /home/pi/Bots/bot.js
Mar 25 19:01:05 Server systemd[1]: Started Bots by Tjum28.
Mar 25 19:01:10 Server node[9627]: Bot ist gestartet und einsatzbereit als T&M Transporte
After I send a message:
● bot.service - Bots by Tjum28
Loaded: loaded (/lib/systemd/system/bot.service; disabled; vendor preset: enabled)
Active: active (running) since Thu 2021-03-25 19:01:05 CET; 13s ago
Main PID: 9627 (node)
Tasks: 11 (limit: 2062)
CGroup: /system.slice/bot.service
└─9627 /usr/bin/node /home/pi/Bots/bot.js
Mar 25 19:01:05 Server systemd[1]: Started Bots by Tjum28.
Mar 25 19:01:10 Server node[9627]: Bot ist gestartet und einsatzbereit als T&M Transporte
Mar 25 19:01:18 Server node[9627]: [Error: EACCES: permission denied, open './xpfile.json'] {
Mar 25 19:01:18 Server node[9627]: errno: -13,
Mar 25 19:01:18 Server node[9627]: code: 'EACCES',
Mar 25 19:01:18 Server node[9627]: syscall: 'open',
Mar 25 19:01:18 Server node[9627]: path: './xpfile.json'
Mar 25 19:01:18 Server node[9627]: }
I use a rank system with a .json file.
I know that there is a problem with the .json file (xpfile.json) but I don't know how to fix it.
Here is the import in my bot.js file
let xpfile = JSON.parse(fs.readFileSync("/home/pi/Bots/xpfile.json", "utf8"));
The result from ls -l /home/pi/Bots/xpfile.json:
-rwxr-xr-x 1 pi pi 7416 [...] /home/pi/Bots/xpfile.json
When I start the bot with screen all is fine.

Systemd service enabled but does not start

I have a custom Spring Boot web application running on a Linux virtual machine. I have a systemd script to start/restart the application.
I have tried changing to WantedBy=default.target and it still does not work. I have tried adding After or Type and still does not work
[Unit]
Description=SpringBootSampleApp
[Service]
WorkingDirectory=/home/mecpro
ExecStop=/usr/bin/sudo /home/mecpro/webapp/webapp-0.0.1-SNAPSHOT.war
ExecStart=/usr/bin/sudo /home/mecpro/webapp/webapp-0.0.1-SNAPSHOT.war
[Install]
WantedBy=multi-user.target
My console screen looks like this
Loaded: loaded (/etc/systemd/system/webapp.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2019-08-12 13:26:27 UTC; 22ms ago
Main PID: 9104 (sudo)
Tasks: 2 (limit: 4675)
When I run systemctl status webapp.service, I get this
webapp.service - SpringBootSampleApp
Loaded: loaded (/etc/systemd/system/webapp.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Mon 2019-08-12 13:26:27 UTC; 3min 17s ago
Process: 9104 ExecStart=/usr/bin/sudo /home/mecpro/webapp/webapp-0.0.1-SNAPSHOT.war (code=exited, status=2)
Main PID: 9104 (code=exited, status=2)
Aug 12 13:26:28 x01133361 sudo[9104]: /home/mecpro/webapp/webapp-0.0.1-SNAPSHOT.war: 2: /home/mecpro/webapp/webapp-0.0.1-SNAPSHOT.war: Syntax error: "(" unexpected
Aug 12 13:26:27 x01133361 systemd[1]: Started SpringBootSampleApp.
Aug 12 13:26:27 x01133361 systemd[1]: webapp.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
Aug 12 13:26:27 x01133361 systemd[1]: webapp.service: Failed with result 'exit-code'.
Aug 12 13:26:27 x01133361 sudo[9104]: root : TTY=unknown ; PWD=/home/mecpro ; USER=root ; COMMAND=/home/mecpro/webapp/webapp-0.0.1-SNAPSHOT.war
Aug 12 13:26:27 x01133361 sudo[9104]: pam_unix(sudo:session): session opened for user root by (uid=0)
Aug 12 13:26:27 x01133361 sudo[9104]: pam_unix(sudo:session): session closed for user root
This is my first question on StackOverflow so kindly excuse if the question is badly formatted. Thanks in advance.

Systemd node app failing to start

I have a node app, that I'm trying to add as a service, the app works fine if I go to the directory where it's installed and do node start.js however when I do systemctl start app it just hangs for a few mins then journalctl -u darknet shows:
Dec 24 01:46:33 Skynet systemd[1]: Started darknet.
Dec 24 01:46:33 Skynet systemd[1]: Starting darknet...
Dec 24 01:46:34 Skynet darknet[32246]: module.js:434
Dec 24 01:46:34 Skynet darknet[32246]: return process.dlopen(module, path._makeLong(filename));
Dec 24 01:46:34 Skynet darknet[32246]: ^
Dec 24 01:46:34 Skynet darknet[32246]: Error: Module version mismatch. Expected 46, got 51.
Dec 24 01:46:34 Skynet darknet[32246]: at Error (native)
Dec 24 01:46:34 Skynet darknet[32246]: at Object.Module._extensions..node (module.js:434:18)
Dec 24 01:46:34 Skynet darknet[32246]: at Module.load (module.js:343:32)
Dec 24 01:46:34 Skynet darknet[32246]: at Function.Module._load (module.js:300:12)
Dec 24 01:46:34 Skynet darknet[32246]: at Module.require (module.js:353:17)
Dec 24 01:46:34 Skynet systemd[1]: darknet.service: main process exited, code=exited, status=1/FAILURE
Dec 24 01:46:34 Skynet systemd[1]: Unit darknet.service entered failed state.
Dec 24 01:46:34 Skynet systemd[1]: darknet.service failed.
This is my .service file:
[Unit]
Description=darknet
After=network.target
[Service]
ExecStart=/usr/bin/node /home/botty/Darknet/start.js
Restart=always
RestartSec=180
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=darknet
User=botty
Group=botty
Environment=NODE_ENV=production
WorkingDirectory=/home/botty/Darknet
[Install]
WantedBy=multi-user.target
I tried running the process both as regular user and root and it works fine .. but no matter what I try it gives errors if started with systemd.
I had this problem. I wasn't using the same node version in systemd as when running locally.
$ which node
/usr/local/bin/node
Replacing /usr/bin/node with /usr/local/bin/node (or the equivalent in which node, such as when using nvm) in ExecStart fixed it for me.
Contrary to a popular misconception, systemd is very different from traditional Unix concept of init scripts. It is explained in much more detail here and here. I have noticed that a lot of people here are asking about problems with running Node with systemd and they rarely get any solutions. My recommendation would be to use traditional SysV init scripts or Upstart that also works with no surprises whenever you see that you cannot do something with systemd. I have never seen situations where a Node application couldn't be easily run with Upstart or SysV init scripts but I've seen problems with systemd. I've heard that it is especially problematic with clustering and other more complicated deployment scenarios. Just follow the Unix philosophy and use simple tools that do one thing and do it well. Init scripts are not that complicated.
See also:
create systemd nodejs express-generator

Resources