Passing environment variable to pm2 is not working - node.js

I have two API in node js using babel and I have a package.json commands to use to make the application working this is the commands:
"build": "del-cli dist/ && babel src -d dist --copy-files",
"serve": "cross-env NODE_ENV=production node dist/index.js",
"start:noupdate": "cross-env NODE_ENV=development babel-node src/index.js",
"start:serve": "cross-env NODE_ENV=production node dist/index.js",
I have two domains one is https://api.website1.com.br and another is https://website2.com.br/api.
They have the same env file name but with another data for each database, that is .env.production and .env.development
When I make this "yarn build", my Linux execute this command :
"build": "del-cli dist/ && babel src -d dist --copy-files",
And this is working fine when I try to put in production mode on my real webservers, i go to the folder from the project and run this command to make the app online with PM2:
pm2 start npm -- run-script start:serve NODE_ENV=production
That will make this command work:
"cross-env NODE_ENV=production node dist/index.js"
The app runs just fine, but I have a problem he only runs one and doesn't create a new PM2 APP he just restarts what I start.
Example if I go to the folder in my https://api.website1.com.br and run this command first in this he starts, but I go to the another he doesn't start that but reload my already early app don't create a new one, what I'm doing wrong?

I manage to work this using pm2 ecosystem, that I found in this documentation from http://pm2.keymetrics.io/docs/usage/application-declaration/
I configure the default file and put a name my APP:
module.exports = {
apps : [{
name: "app",
script: "./app.js",
env: {
NODE_ENV: "development",
},
env_production: {
NODE_ENV: "production",
}
}]
}
and use this command pm2 start ecosystem.config.js and now is working, I post here to know if someone has the same problem

Related

How do i run one script on development and another on production

"scripts": {
"start": "$env:NODE_ENV=\"development\"; nodemon server.js",
"start:prod": "$env:NODE_ENV=\"production\"; nodemon server.js"
},
I want to set the NODE_ENV variable to development when i run 'start' script and to production when i run 'start:prod' script but i get error that says:
$env:NODE__ENV="development"; nodemon server.js
The filename, directory name, or volume label syntax is incorrect.

npm run points to different .env file

We are working on nodeJs/ExpressJs we have configured multiple .env files for development and production and pointing it to package.json for different execution process, we have naming conversation issues at scripts.
Whenever we run npm run prod it takes to preprod configuration. what could be the issues?
Update: we have figured that the suffix of the script key is the same in the next script, after update/rename preprod to preProd the both runs fine. but why?
Eg :
"scripts": {
"dev": "clear; env-cmd -f ./config/hostedDev.env nodemon --exec babel-node index.js",
"prod": "clear; env-cmd -f ./config/prod.env nodemon --exec babel-node index.js",
"preprod": "clear; env-cmd -f ./config/preprod.env nodemon --exec babel-node index.js"
},
Apparently the issue is with the word 'pre'.
If you would have noticed it runs both preprod and prod commands (pre running first).
If you change the script name to 'postprod' the postprod script will run later.
So, I guess npm uses 'pre' as to run before the 'prod' script and then running 'prod' script itself.

Node NextJS app recompiling when running in dev = true mode

I have an NextJS app that I build on a build server then deploy to another server to host it.
When I start the app in development mode npm wants to recompile the app even though all of the built components still exist. (.next folder, etc...)
When I run the app in a non development mode for next the app will start up with no build attempts.
Why does npm want to rebuild the app when const app = next({ true });?
In server/server.js
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
In package.json
"scripts": {
"dev": "NODE_ENV=development npm start",
"staging": "NODE_ENV=staging npm start",
"prod": "NODE_ENV=production npm start",
"build": "next build",
"start": "node server/server.js"
}
How I'm starting the app:
Development: npm run dev
Production: npm run prod
For a lack of better description the .next folder is the "optimized" version of your application. In development you need to continuously rebuild the .next folder because this is where you are serving your files from. During development you can actually see the .next rebuilding itself. In production you should only build the application once.
I imagine if you're deploying in Now it will build the .next folder as long as you have scripts like they define in setup. However, I use Docker for my builds so I have to build my .next folder via a build step. Here is an example of a Dockerfile and a corresponding package.json.
Dockerfile
FROM node:10-alpine AS builder
WORKDIR /app
COPY ./app .
RUN yarn install && yarn next-build
EXPOSE 80
CMD yarn start
package.json
...
"scripts": {
"next-build": "next build",
"start": "NODE_ENV=production node server/app.js",
...
So in production my build steps are
1. Install npm modules
2. Build my .next folder
3. Start my server.
In short:
In production since you aren't running a "next-build" script before you run "npm run prod" you aren't rebuilding the .next folder. In development next.js rebuilds each time you start your server so it can capture your changes in the .next folder.

File on heroku not found

I have a single-page application which is written in NodeJS and deploy on Heroku.
I define my Procfile like:
web: PORT=$PORT webpack -p --config ./config/webpack/serverProd.js --progress && node ./dist/server.js
The reason why I do not use heroku-postbuild deploy hook is because I can not get the PORT variable which is assigned by Heroku dynamically, and this is the only way I can access PORT variable.
But when I run heroku run bash and search around in the container of Heroku...
ta da!
I can not found my server.js which it should be exist in the /app/dist/ directory.
I am pretty sure there is something I misunderstand. Can anyone point that out?
Really appreicate!
This is how I setup my nodejs\npm project on heroku
Procfile
web: npm run build && node dist/index.js
package.json
"scripts": {
"clean": "rm -rf dist && mkdir dist",
"serve": "node dist/index.js",
"start": "nodemon --inspect src/index.js --exec babel-node",
"build": "npm run clean && npm run build-server",
"build-server": "babel src -d dist",
"test": "mocha --compilers js:babel-register"
},
Try to start the app from dist/server.js instead of ./dist/server.js

nodemon to exec "prestart" script at package.json

I want to setup nodemon to run the "prestart" script when server restarts, the reason, I'm using webpack and I want to build bundle.js every time a file changes, I'm not using webpack-dev-server cause I don't know how to set it up with an existing node app running, I got my backend on node.js, help on that will be appreciated.
The way I've been working is: run npm start every time I make a change on the code, but that too much effort.
Here's the scripts object at package.json so that you have an idea of what's going on:
"scripts": {
"bundle": "webpack --config webpack.config.js",
"prestart": "npm run bundle",
"start": "node server.js"
}
The way I've accomplished to do that is by creating a nodemon.json file with the exec property:
{
// ... Other conf (see an example in https://github.com/remy/nodemon/blob/master/doc/sample-nodemon.md)
"watch": [
"app" // Set your directories/files to watch
],
"exec": "npm run prestart && node server/index.js"
}

Resources