Is there a way to get PM2 to run npm install when package.json updates? - pm2

I set up pm2 with a watch, so each time a git pull is done, it auto reloads the app.
This works, unless the app dependencies have changed, then it needs to run npm install first. This causes pm2 to restart and crash over and over until the install is done.
Is there a way to have pm2 watch automatically run npm install before restarting?

there is no way but, you can add script in package.json
...
scripts: {
....
"postinstall" "pm2 restart (your process id from pm2)"
}
...
Run pm2 start (your entry file server) and get process id.
Always run npm install and then pm2 will restart process after that
Note that you need to install pm2 globally using "npm i -g pm2" on the machine

Related

How to get React Project to work Because NPM Start Wont Work

Hello I downloaded a react project from https://github.com/fabiau/jc-calendar and when I do npm start I get error messages. I tried to do "NPM Fund" and "NPM Update" and none of them worked. Obviously i have no clue what I am doing so if anyone can point me in the right direction I would apprecaite it.
Before starting dev server you'll have to install the dependencies of the project using npm install , as generally node_modules/dependencies are not part of the repository.
This is mentioned in Readme.md of JC-calender.
Try deleting folder node_modules and package-lock.json if exists and run command npm install
Checkout scripts in package.json in usual case start will be as given below but as per the code you consider npm-run-all package is used so be sure to run npm install
"scripts": {
"start": "react-scripts start",
},
This may help you out https://www.npmjs.com/package/npm-run-all
most of the repositories on GitHub, don't have dependencies installed, after downloading a repository on your local machine, you need to run "npm install". The reason behind this is that npm will look for all the required dependencies of the specified project and install them on your machine, with node-modules, then run "npm start"

How to start verdaccio by pm2?

pm2 start verdaccio failed, the status is stopped.
$ npm i -g pm2 verdaccio
$ pm2 start verdaccio
I've have been running verdaccio in pm2 for 1 year by know and I use this after installing it globally via npm.
Start
pm2 start `which verdaccio`
Restart
pm2 restart verdaccio
And that works pretty well for me.
The problem is pm2 finding verdaccio in the first place. That's why using which solves it, but that is not always possible in Windows.
I've managed to run verdaccio in pm2 on Windows, without even installing it globally (so that I could commit my configs, plugin packages, etc), with this:
// ecosystem.config.js
module.exports = {
apps : [{
name: 'verdaccio',
script: './node_modules/verdaccio/build/lib/cli', // local package, that pm2 can run with "node <script>"
args : '--config ./config.yaml', // local confis
node_args: '-r esm', // verdaccio's package didn't contain "type": "module"
exec_mode: 'fork' // verdaccio does not currently support PM2's cluster mode
}]
}
PS.: these are my dependencies:
// package.json
"dependencies": {
"esm": "^3.2.25",
"verdaccio": "^5.10.2",
"verdaccio-activedirectory": "^1.0.2",
"verdaccio-simplegroup": "^1.0.2"
},
for someone still stumbling upon this problem. you can try deleting the process then starting it again. I solved it this way.

Hexo init fails on Windows

I am trying to initialize a Hexo site on my Windows 10 PC. I have Node 9.9.0 installed as well as git 2.16.2.windows.1.
I have run npm install hexo-cli -g and it installs fine. I then go about the steps listed to create and initialize a site (hexo init test-site) and get the following errors.
I've tried a fresh install of node and git, running npm install a-sync-waterfall -g, and adding a file to node_modules called .a-sync-waterfall.DELETE; but hexo init still fails. Manually running npm install also gives me the same error.
Any ideas? Thanks.
Windows and NPM are not the most stable together. I've found Yarn to much more stable.
Try one of these two things:
Use Yarn
Delete node_modules folder, clear the NPM global cache, attempt a hexo init
del node_modules
npm cache clear --force
hexo init test-site

gulp permission and local install without the json file

As it says on the gulp io to install gulp globally you need to run
npm i -g gulp
which will return an EACCES error so you will use sudo in front of the command. Now I go into my new project directory that I want to install gulp in. As they say on the page to install it into your project folder you can use this command
npm i --save-dev gulp
if I do so it returns an EACCES error, which means I will need to use sudo again which I cannot quite get as I only want to install it in a local directory. If I do so it installs node_modules folder with path
gulp#3.9.0 ../../node_modules/gulp
Which means it is installed in my root directory and not the directory I wanted it to install it in. To fix it I can run
npm init
saying yes to everything and then
sudo npm install --save-dev gulp
Which will put node_modules into my project folder
My question is what prevents gulp installation into my project folder be as simple as it is in the gulp instruction? And what can I do to prevent this from happening in the future? I have researched this and people seem to get gulp running without the package.json file which is not a big deal but one more step if you want to jump start on something.

NPM doesn't install module dependencies when deploying a Grunt app to heroku

I'v made a static single page site using grunt. I'm now trying to deploy it to heroku using the heroku-buildpack-nodejs-grunt for node grunt.
Below is a pic of my root directory:
Here's my Gruntfile package.json:
Procfile:
web: node index.html
When I run $ git push heroku master it gets to the Gruntfile and fails:
-----> Found Gruntfile, running grunt heroku:production task
>> Local Npm module "grunt-contrib-uglify" not found. Is it installed?
The above errors proceed to list all local NPM modules as not found. If I list all loadNpmTasks instead of using "load-grunt-tasks", I get the exact same error.
When I $ heroku logs I get:
Starting process with command `node web.js`
Error: Cannot find module '/app/web.js'
Can anyone see where I've gone wrong?
For anyone passing by here, I wasn't able to solve the problem. This is where I got to:
In my Gruntfile, I moved npm modules from devDependencies to dependencies. Heroku was then able to install these dependencies.
However, when Heroku ran the tasks, it stops at the haml task w/ error "You need to have Ruby and Haml installed and in your PATH for this task to work". Adding ruby & haml to the Gruntfile as engines did not work.
The only thing I can think of is that maybe Heroku installs your devDependencies first, tries to run Grunt, but since it didn't install load-grunt-tasks yet, you don't get the grunt.loadNpmTasks( 'grunt-contrib-uglify' ); line (which load-grunt-tasks does for you), and thus Grunt can't find the package.
Can you try changing your Gruntfile to explicitly list out all npm modules using the grunt.loadNpmTasks() method?
EDIT:
Just remembered another thing I had to do:
heroku labs:enable user-env-compile -a myapp
heroku config:set NODE_ENV=production
(Obviously replacing myapp with your Heroku app name.)
This makes Heroku allow user set environment variables and then sets your server to production. Try that, and set your dependencies and devDependencies as you had them originally (just to see if it works).
I am coming pretty late to the game here but I have used a couple methods and thought I would share.
Option 1: Get Heroku to Build
This is not my favorite method because it can take a long time but here it is anyway.
Heroku runs npm install --production when it receives your pushed changes. This only installs the production dependencies.
You don't have to change your environment variables to install your dev dependencies. npm install has a --dev switch to allow you to do that.
npm install --dev
Heroku provides an article on how you can customize your build. Essentially, you can run the above command as a postinstall script in your package.json.
"scripts": {
"start": "node index.js",
"postinstall": "npm install --dev && grunt build"
}
I think this is cleaner than putting dev dependencies in my production section or changing the environment variables back and forth to get my dependencies to build.
Also, I don't use a Procfile. Heroku can run your application by calling npm start (at least it can now almost two years after the OP). So as long as you provide that script (as seen above) Heroku should be able to start your app.
As far as your ruby dependency, I haven't attempted to install a ruby gem in my node apps on Heroku but this SO answer suggests that you use multi buildpack.
Option 2: Deploy Your Dependencies
Some argue that having Heroku build your application is bad form. They suggest that you should push up all of your dependencies. If you are like me and hate the idea of checking in your node_modules directory then you could create a new branch where you force add the node_modules directory and then deploy that branch. In git this looks like:
git checkout -b deploy
git add -f node_modules/
git commit -m "heroku deploy"
git push heroku --force deploy:master
git checkout master
git branch -D deploy
You could obviously make this into a script so that you don't have to type that every time.
Option 3: Do It All Yourself
This is my new favorite way to deploy. Heroku has added support for slug deploys. The previous link is a good read and I highly recommend it. I do this in my automated build from Travis-CI. I have some custom scripts to tar my app and push the slug to Heroku and its fast.
I faced a similar problem with Heroku not installing all of my dependencies, while I had no issue locally. I fixed it by running
heroku config:set USE_NPM_INSTALL=true
into the path, where I deployed my project from. This instructs Heroku to install your dependencies using npm install instead of npm ci, which is the default! From Heroku dev center:
"Heroku uses the lockfiles, either the package-lock.json or yarn.lock, to install the expected dependency tree, so be sure to check those files into git to ensure the same dependency versions across environments. If you are using npm, Heroku will use npm ci to set up the build environment."