running npm application using forever start in server - node.js

I have an angular 6 application with typescript and webpack.
This angular application has backend for serving api calls and the backend is running as a separate application.
To start the application in my local, From the project home folder,
npm start
To build the application,
npm run build
When I run the above command, I get, dist folder which has the follwoings.
- index.html
- assets
- fonts
- icon
- img
- main.sdgsdgfdsff.css
- main.sdfgsdfgdsfg.js
- manifest.3asdasdfsad.bundle.js
- polyfills.3462436425654af34.bundle.js
- vendor.sdgsdfg3453244354.bundle.js
- webpack-assets.json
- server
- config.js
- index.js
- routes.js
- socket.js
Now, I would like to run this application in a remote server.
So, copied the entire dist folder and tried the following.
forever start -o otput.log server/index.js
The console logs,
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info: Forever processing file: server/index.js
In output.log.
Node NOT Exiting...
Nothing is happening.
My goal is to start this in a remote server. Not sure what am i missing here.
I tried forever in my local mac also.
forever start -o otput.log server/index.js
I installed the below stuffs also.
npm install express compression forever minimum-tls-version body-parser fs https path
method-override

Related

Cannot connect to my databse addon on heroku

I attempted to use the command Heroku pg:psql to connect to my database addon in heroku but got a response below
--> Connecting to postgresql-regular-61345
unrecognized win32 error code: 123could not find a "psql" to execute
unrecognized win32 error code: 123could not find a "psql" to execute
psql: fatal: could not find own program executable
! psql exited with code 1
After using the heroku logs --tail command i got the following errors
sh: 1: nodemon: not found
Process exited with status 127
State changed from starting to crashed
I can also see all processes stopping with SIGTERM and the process exiting with status 143
Resolution steps I have taken
Verified that the environment variables have the path for installed postgress14 on my PC
Added a procfile to the root file in my backend code and spcified "web: node matthewfaceappback/server.js in the file"
Changed my set port to a variable port using process.env.PORT || 3000
Set all environment variable including my database url(set by default) on config variable in heroku
Verified there is a start up script
Updated all my packages using "npm update". after doing this i started expereincing the issue of processes stopping with SIGTERM and the process exiting with status 143
I moved nodemon from devDependencies to dependencies. nodemon version is 2.0.15
In package.json i inputed an engines parameter using the version of node in my case
{"engines": {
"node": "14.17.4"
}}
I restarted heroku using "heroku restart"
Below are links to the screenshots of the error
https://www.dropbox.com/s/5bdbyi9e99lbxhu/pic1.PNG?dl=0
https://www.dropbox.com/s/41euniaes5q68c9/pic2.PNG?dl=0
https://www.dropbox.com/s/50oqzbwmwrqogax/pic3.PNG?dl=0
Put nodemon back in the devDependencies and add it as a second node script in package.json:
"scripts": {
"start": "node matthewfaceappback/server.js",
"dev": "nodemon matthewfaceappback/server.js"
},
These two errors are completely unrelated.
The database connection error
The first issue, which I believe is the one you actually care about at the moment based on the title of the question, indicates that the Heroku CLI can't find a PostgreSQL client on your local machine.
The documentation makes the following recommendation
Set up Postgres on Windows
Install Postgres on Windows by using the Windows installer.
Remember to update your PATH environment variable to add the bin directory of your Postgres installation. The directory is similar to: C:\Program Files\PostgreSQL\<VERSION>\bin. Commands like heroku pg:psql depend on the PATH and do not work if the PATH is incorrect.
If you haven't already installed Postgres locally, do so. (This is a good idea anyway as you should be developing locally and you'll probably need a database.)
Then make sure to add its bin/ directory to your PATH environment variable.
The Nodemon error
The second issue is because you are trying to use nodemon in production. Heroku strips development dependencies out of Node.js applications after building them, which normally makes sense. Nodemon is a development tool, not something that should be used for production hosting.
Depending on the contents of your package.json, this might be as simple as changing your start script from nodemon some-script.js to node some-script.js. Alternatively, you can add a Procfile with the command you actually want to run on Heroku:
web: node some-script.js
See also Need help deploying a RESTful API created with MongoDB Atlas and Express

nuxt.js: where is the .js for running nuxt start

nuxt version: 2.4.3
I'm using node process manager (pm2) for my node.js hosting. To run nuxt on development server, you can simply nuxt or npm run dev but that's not the case if you're using pm2's ecosystem.config.js which needs you to specify which file actually runs that.
For example, to run a developement server I have to point to the file that runs it which is ./node_modules/nuxt/bin/nuxt.js
But I have no idea which file that run production server or nuxt start
Can someone points me which file in ./node_modules/nuxt or ./node_modules/#nuxt that perform nuxt start?
--edit
This article covers how you run nuxt start with pm2 but aforementioned file ./node_modules/nuxt/bin/nuxt-start does not exists on mine. I suspect because we are using different nuxt version
Js file you are looking for (nuxt > 2.3)
node_modules/nuxt/bin/nuxt.js
When using pm2, I'm doing it this way (ecosystem.yml)
apps:
- name: client
script: node_modules/nuxt-start/bin/nuxt-start.js
cwd: /root/app/client
max_memory_restart: "250M"
args: "start"
Notice that for production in this case I'm using nuxt-start dependency to speed up npm i.

How to run a node app after production build by WebPack when output is index.html?

***Updated for further clarification:
Thanks much for taking the time to reply. Running 'npm run build' (I'm using the Vuetify/Webpack template) created an index.html and a directory named 'static' with the respective html, css, and js (in 'dist' directory). The question is - how can I run this in production using node or pm2 (for example, 'pm2 start myapp.js')?
Doing some further research and testing, the 'npm run build' built as static. It was my lack of understanding how vue-cli was to build for production (via WebPack) (vue-cli builds static meant to be served via web server). Doing further testing, I can load and bring up my app on a couple local Linux servers (the static content running on one servers, while MongoDB and API running on another server (API running with pm2)). Also, I was able to bring it up on AWS S3 (as static). I think I have my answer now. Thanks much for your time.
***From Original Post:
How to run the following node app in production? Webpack build produced an index.html (I assume as it's using html-webpack-plugin). I can view the site if I run under a web server (e.g., nginx), but I want to run my final app using pm2, or running node process.
I am using the Vuetify WebPack template (I'm using Vue.js), when I run the npm run build for production (in package.json: “build”: “node build/build.js”) the output is as follows:
$ ls
index.html static/
and
$ ls static/js/
app.f7301c212bc5fefd6563.js manifest.2ae2e69a05c33dfc65f8.js vendor.1aae36aa0dc798d4d36d.js
app.f7301c212bc5fefd6563.js.map manifest.2ae2e69a05c33dfc65f8.js.map vendor.1aae36aa0dc798d4d36d.js.map
So, how do I run this via node (npm start) or pm2 (pm2 start) (can't use index.html or static/js files).
When I run the webpack build I was expecting a .js output (for example ‘build.js’) to run via node or pm2 (npm start that runs ‘node build.js’ or pm2 start build.js). I suppose this will work for serverless, but was looking to run via pm2 (with nginx as proxy) as the app will have other factors (e.g., MongoDB) that won’t allow running in serverless (e.g., AWS Lambda).
Would this be a matter of just re-configuring WebPack to output to a .js file (for example, build.js), or will the static files (served via a webserver, nginx/Apache/http-server) allow for the same functionality? That is, will there be any factors that would limit my app by running as static verses via ‘node build.js’ or ‘pm2 start build.js’, (for example, will my API connects to the back-end still work (back-end being node/JS))?

How to run nuxt.js in real service?

I used vue-cli in
vue init nuxt/express myProject
and,
npm run dev
developed.
but,
npm run build
after, was created dist file.
How i can run in real service in pm2?
(I will use ubuntu in AWS EC.)
you just need to start your app like that:
pm2 start npm --name "your-project-name" -- start
Check the status:
pm2 status
and after you can restart / stop:
pm2 restart your-project-name
pm2 stop your-project-name
Prerequisites
node.js installed on web server
nginx installed and configured on web server
pm2 installed and configured on web server
Then
Add to your universal Nuxt app for serving it though PM2 is a file called ecosystem.config.js. Create a new file with that name in your root project directory and add the following content:
module.exports = {
apps: [
{
name: 'project-name',
exec_mode: 'cluster',
instances: 'max', // Or a number of instances
script: './node_modules/nuxt/bin/nuxt.js',
args: 'start'
}
]
}
Connect to your linux server via FTP (FileZilla or etc..)
Send the blue files I marked to the server.
(you don't need to upload node_modues, .nuxt, dist, .git, .idea, etc... folders)
Connect server via ssh console, (windows : putty)
and go to projects folder that you uploaded files.
cd /
cd var/www/project-name
Install node_modules folder by;
npm install
Execute nuxt build and create .nuxt folder by;
npm run build
Finally, ready to start starts pm2 server by;
pm2 start

Forever with npm start and environment variable

I used to start my application using:
DEBUG=chakka ENVIRONMENT=production npm start
How can i start it using forever so i wouldn't have to do it everytime i want to test the application? Thanks!
First you need to know what the application's main script file is. Open up your package.json file and find out what the start script is. If you're using Express it might be app.js. So we'll assume app.js for this example, replace with whatever your file is.
To start the application:
DEBUG=chakka ENVIRONMENT=production forever start app.js
to restart the application after you've made changes:
forever restart app.js

Resources