I'm having the following code in my package.json to start the script while developing:
....
"scripts": {
"start": "nodemon src/index.js --exec babel-node --presets es2015,stage-2"
},
....
Now I want to deploy it to production. When I run npm start everything works fine. However, it will shut down when I close the terminal. So how can I use it with PM2?
This is what I've tried:
pm2 start src/index.js -x babel-node -p es2015,stage-2
You can actually use npm start if you like:
$ pm2 start npm -- start
That said, for production deployment I would strongly recommend a) using a config file for your pm2 startup stuff (so you can bundle environment variables and such) and b) precompiling your assets as a build step rather than on startup.
Related
I've always used just npm and never yarn/webpack explicitly. I need to run the code from this repo:
https://github.com/looker-open-source/custom_visualizations_v2
Like a dev server or something to ensure it's serving the files properly but I don't see a "run" like npm run start. Does this just not exist with yarn? It feels like this code should work as is and I shouldn't have to add anything.
EDIT: I've now tried yarn run watch but it just seems to build the code again and not actually host anywhere
npm run somecommand just looks up in the "scripts" field of package.json for the key
somecommand and executes the value in the terminal.
So npm run start basically runs the start script from package.json
The same thing is done using yarn via simply yarn start
In the linked repo, there isn't a start script in the package.json, rather a watch script, so you should be able to run it with the below steps:
yarn to install dependencies after cloning the repo to local (similar to npm install)
yarn watch to start the webpack server (analogous to npm run watch)
Edit:
Turns out the watch command is just setting up webpack to watch for changes and recompile the project every time there is a change.
To run a development server, you will need to add another script preferably with name start and use webpack-dev-server
So the package.json has entries like:
...
"watch": "webpack --config webpack.config.js --watch --progress",
"start": "webpack-dev-server --config webpack.config.js",
...
Then running yarn start should open a dev server at localhost:8080
I want to run these pm2 tasks:
"pm2-frontend": "pm2 start --name frontend npm -- start",
"pm2-storybook": "pm2 start --name storybook npm -- storybook"
Which should run these two package.json scripts:
"storybook": "start-storybook -p 6006",
"start": "next start"
But, this only seems to start the start script. Is there a way to target scripts with another name than start?
Use the word run before you run scripts other than start.
Start is a default script name so you can run it with npm start but storybook is not a default script so you gotta use npm run storybook
I am using npm start to start my MEAN stack application, but I would like to use the node-inspector to debug some Mongoose. I know I can start the node inspector with node-inspector, but what can I substitute node --debug app.js with to make npm start work in my case?
This is my MEAN stack directory structure:
HTML views/
Angular.js public/javascript/
Express.js routes/
Node.js app.js
Mongoose js models/, connected in app.js
Mongo db connected in app.js
For more information, this is my related question.
You may want to add a seperate debug script to package.json. That way you won't have to remember to revert npm start when you're finished debugging.
"scripts": {
"start": "node ./bin/www",
"debug": "node --debug ./bin/www"
}
Start with npm run:
$ npm run debug
In package.json modify the start run command:
"scripts": {
"start": "node --debug app.js"
}
I use it like this, I also set a variable and run the inspector in one command:
npm run debug
"scripts": {
"start": "set SOAPAPI=https://example.com/&&nodemon",
"debug": "start node-inspector --web-port=8081&&set SOAPAPI=https://example.com/&&nodemon --debug"
}
*nodemon is an utility wrapper for node, you can use node instead
I'm experimenting with setting up a dev environment to use NPM only, without the use of grunt.js or bower.js.
I followed this tutorial: http://beletsky.net/2015/04/npm-for-everything.html
I'm using nodemon to watch my .js and .scss files for changes which restarts the node server. So in my package.json file, under scripts I have
scripts:
"watch-js": "nodemon -e js --watch public/js -x \"npm run build-js\"",
"watch-sass": "nodemon -e scss --watch public/sass -x \"npm run build-sass\"",
"watch": "npm run watch-js & npm run watch-sass"
But when I run npm run watch it only watches for the public/js files to change. And it triggers a build accordingly.
But it won't watch for the sass files.
Versions:
node v0.10.36 nodemon v1.4.1
I also include a build script which if I run compiles the sass to css, so my build-sass script should be ok
"build": "npm run build-js & npm run build-sass",
"watch": "npm run watch-js & npm run watch-sass"
If you are using windows, you might be encountering the windows problem
Because npm is reliant on the operating systems shell to run scripts commands, they can quickly become unportable. While Linux, Solaris, BSD and Mac OSX come preinstalled with Bash as the default shell, Windows does not. On Windows, npm will resort to using Windows command prompt for these things
If so, you can try using parallelshell or npm-run-all for better cross platform support.
This works for me in Mac OS
Install nodemon
npm install --save-dev nodemon
Install npm-run-all:
npm install --save-dev npm-run-all
In package.json add two scripts:
"startn": "nodemon ./app.js",
"watch-jscss": "gulp watch"
Where 'app.js' is your server file
"gulp watch" can be replace by whatever task is doing a sass watch
In the console:
npm-run-all --parallel watch-jscss startn
pm2 is a great tool to manage node apps. How does it work with grunt/glup ?
I didn't find any useful clues after Googling for 20 minutes.
If I understand your question well, it seems you want to deploy your app.
Since pm2 0.9 deployment can be done with pm2 deploy see README.
In the case of grunt/gulp, I see two options:
You've your node_modules comitted. Using pm2 deploy run your gulp process from the post-deploy section:
"post-deploy" : "node ./node_modules/gulp/bin/gulp.js ./GulpFile.js && pm2 startOrRestart ecosystem.json --env production"
Using a basic script that will launch npm install for you, you could use the package.json to grunt/gulp:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js",
"postinstall": "./node_modules/bower/bin/bower -q -s -f install && ./node_modules/gulp/bin/gulp.js"
},
My gulp generally needs bower to minify scripts so I left it only for example purpose.
You may combine the two options to let pm2 deploy install your npm scripts and have a postinstall script in the package.json.
Note that I'm using the relative path to the gulp module binary! It's just to avoid an issue if the global module is not installed.
Now, in my opinion to deploy an application in production it's better to simply have a git branch where everything is pre-gulped so that you only clone that branch and you're good to go. It also improves the deploy time, especially if you're running tests with gulp or grunt...
Hope that's clear enough!
The Reply may be late it must be usefull to others
On the command line do:
$ export NODE_ENV=production
will setup production environmental
$ grunt build
will create necessary min.js and min.css
$ pm2 start server.js
will load the server with pm2 , that its a package thats makes sure the node server will restart if an error and will log.