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.
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 have a Node.js app using TypeScript and now I want Jasmine to run tests automatically each time I make changes in .ts files. So I'm just trying to find an appropriate command to be run as npm test in command line or a package that can watch my .ts files compile them on changes and run jasmine. Does anybody know a solution for it?
The easiest way I found is
installing dependencies: npm install --save-dev jasmine-ts nodemon
initializing jasmine: node_modules/.bin/jasmine-ts init
In the package.json:
"scripts": {
"test": "nodemon --ext ts --exec 'jasmine-ts \"src/**/*.spec.ts\"'"
}
Edit: the above solution doesn't work as of the 11th of Apr, 2019. I published a modified working example at https://github.com/erosb/ts-node-jasmine-example
This may be done with two commands launched in separate terminals. Assuming packages are installed in global mode.
First command launches TypeScript compiler in watch mode:
tsc --watch
The second starts nodemon that watches .js files and restarts on changes. Each time it executes jasmine test runner:
nodemon --ext js --exec 'jasmine JASMINE_CONFIG_PATH=jasmine.json'
This solution is fast enough though it also has a drawback of running in two terminals. So it is not ideal but the best I've found so far.
As a result scripts section in package.json looks like:
"scripts": {
/* ... */
"watch": "tsc --watch",
"test": "nodemon --ext js --exec 'jasmine JASMINE_CONFIG_PATH=jasmine.json'",
"devstart": "nodemon ./bin/www"
},
devstart also works in couple with watch restarting server each time .ts files are changed (after they are compiled to .js).
You might consider using jasmine-node. I don't think that jasmine itself has a watch option.
npm i -g jasmine-node
Assuming that your test command in your package.json scripts block is something like this:
"scripts": {
...
"test": "jasmine some-directory-or-glob-pattern"
...
}
Use jasmine-node and add the --autotest and --watch flags to that command:
"scripts": {
...
"test": "jasmine-node --autotest --watch some-directory-or-glob-pattern"
...
}
Previously described methods either did not work, or were slow to compile code. Here is my attempt to solve this, both fast and convenient, works great for me. The only downside is that jasmine would not know which tests are affected by TS recompilation and would run all the tests.
yarn add tsc-watch --dev
yarn run tsc-watch --onSuccess "yarn run jasmine --config=jasmine.json"
NPM version:
npm -i tsc-watch
npm run tsc-watch --onSuccess "npm run jasmine --config=jasmine.json"
In my case I needed to correctly map TS paths. The full command looks like this:
yarn run tsc-watch --onSuccess \
"node -r tsconfig-paths/register node_modules/jasmine/bin/jasmine \
--config=jest/jasmine.json --require=dist/jest/setup.js $targetFile"
jasmine.json
{
"spec_dir": "dist/src",
"spec_files": ["**/*.e2e.js", "**/*.unit.js", "**/*.spec.js", "**/*.test.js"],
"env": {
"random": false
}
}
Just an example, please adjust to your needs.
tsc-watch starts a TypeScript compiler with --watch parameter, with the ability to react to successful compilation and start tests.
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.
I have a simple NPM build script which is run using "npm run build". It will listen for file changes, then transpile using babel
"scripts": {
"build": "babel -w js_uncompiled --out-dir js --source-maps"
}
This issue i'm having is regular crashes probably due to having not the best network (We work off the server). I've considered using "forever" to restart the node server once it crashes but i've had no luck with it using the following script
"scripts": {
"build": "forever start --minUptime 1000 --spinSleepTime 1000 babel -w js_uncompiled --out-dir js --source-maps"
}
The error I get is
A:\PortalTom>npm run build
> babeltest#1.0.0 build A:\PortalTom
> forever --minUptime 10000 --spinSleepTime 10000 babel -w js_uncompiled --out-dir js --source-maps
error: Cannot start forever
error: script A:\PortalTom\babel does not exist.
Any help would be much appreciated!
e/
Here's my current package.json : pastebin.com/iRUpAxLm, It all works perfectly (exscuse the many babel modules had to do some hacky stuff to get rid of strict mode to not flag all our old codebase)
I've tried installing forever globally also using "npm install forever -g"
I am trying to build my package.json file, and I am having a difficult time when it comes to writing scripts.
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build-css":"node-sass --output-style compressed -o build/styles src/styles",
"pugtohtml": "npm pug -D index.pug"
},
this doesn't work
I've installed the pug package and now I want to automate the task using just npm can you please help me with that and I would appreciate it if you give me tips and resources of how to learn writing scripts and automating tasks using just npm, thank you!
You must write the task like this
"pugtohtml": "pug --output-style compressed -o dist/html/ assets/pug/*.pug"
It looks like the command line client is required to use this with NPM scripts.
Per the Pug NPM page:
Package
npm install pug
Command Line
After installing the latest version of Node.js, install with:
npm install pug-cli -g
I'm using npm install pug-cli --save-dev instead because I prefer packages to be installed local to the project I'm working on, but YMMV.
If you're into the global (-g) thing, you might not need the pug-cli package for command line handling, and you could possibly use the other solutions mentioned here.