Search code examples
node.jsvpsovh

How to deploy correctly nodejs app on Vps?


I deployed my NodeJs (and Express) application on my VPS (the host is OVH - and the VPS is on Debian 10).

My app worked perfectly on my localhost but now on my vps here are the problems I'm facing:

1 - When I try to do npm start my vps puts an error : (Solved)

Solution : I just forgot in my package.json in scripts : "node server.js" i was just writing "server.js".

sh: 1: server.js: not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! [email protected] start: `server.js`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2022-03-18T10_06_42_146Z-debug.log

When i look in my logs here's what i've got:

0 info it worked if it ends with ok
1 verbose cli [ '/usr/bin/node', '/usr/bin/npm', 'start' ]
2 info using [email protected]
3 info using [email protected]
4 verbose config Skipping project config: /root/.npmrc. (matches userconfig)
5 verbose run-script [ 'prestart', 'start', 'poststart' ]
6 info lifecycle [email protected]~prestart: [email protected]
7 info lifecycle [email protected]~start: [email protected]
8 verbose lifecycle [email protected]~start: unsafe-perm in lifecycle true
9 verbose lifecycle [email protected]~start: PATH: /usr/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/root/node_modules/.bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
10 verbose lifecycle [email protected]~start: CWD: /root
11 silly lifecycle [email protected]~start: Args: [ '-c', 'server.js' ]
12 info lifecycle [email protected]~start: Failed to exec start script
13 verbose stack Error: [email protected] start: `server.js`
13 verbose stack spawn ENOENT
13 verbose stack     at ChildProcess.<anonymous> (/usr/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:48:18)
13 verbose stack     at ChildProcess.emit (events.js:400:28)
13 verbose stack     at maybeClose (internal/child_process.js:1058:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:293:5)
14 verbose pkgid [email protected]
15 verbose cwd /root
16 verbose Linux 4.19.0-18-cloud-amd64
17 verbose argv "/usr/bin/node" "/usr/bin/npm" "start"
18 verbose node v14.19.0
19 verbose npm  v6.14.16
20 error code ELIFECYCLE
21 error syscall spawn
22 error file sh
23 error errno ENOENT
24 error [email protected] start: `server.js`
24 error spawn ENOENT
25 error Failed at the [email protected] start script.
25 error This is probably not a problem with npm. There is likely additional logging output above.
26 verbose exit [ 1, true ]

but it works when i run the command : node server.js

2 - PORT in my domain

Concerning the PORT, I would like not to have to inform the port in the url of my app (for ex : domain.com:3000 -> domain.com)

because it works correctly when I go to domain.com:3000 but without the port nothing happens.

I tried to put in my env file the port 80 (for http) or 443 (for https) but when I do that, and I try to go to my app then this happens :

enter image description here

Solution : NGINX

3 - Let my app run when I quit powershell

Last point when I quit powershell, my app disconnects and I can't access it via the web browser (which is not useful when you want it to be online 24/7)

I can provide you more informations if you need to help me.

Solution : PM2


Solution

  • Here's the tutorial i followed to do it properly ! (tuto by Traversy Media's YT Channel)

    https://youtu.be/oykl1Ih9pMg