npm and node slow to exit on Windows - node.js

I have a build process using npm with several tasks registered in a chain.
package.json looks like this (the http/https stuff is because of a proxy server certificate that upsets npm):
"scripts": {
"git-config": "git config --global --replace-all url.\"https://\".insteadOf git://",
"bower-install": "node_modules/.bin/bower install",
"bower": "npm run git-config && npm run bower-install",
"build": "npm config set registry http://registry.npmjs.org/ && npm config set strict-ssl false && npm install && npm run bower && node build-bundles"
},
Every time npm or node is launched, there seems to be about a .5 second hang to launch, and another 1.5 second hang AFTER the command has finished but before the npm/node process exits. In the above script, there are many calls to npm/node, and this ends up adding 15-20 seconds onto our build time (even if there are no changes).
Is there any way to:
Eliminate at least the 2 second hang before npm/node exits
Make the above script(s) only execute when one of the project files changes (I do not want to use watchify)
Otherwise improve the speed of these scripts
Please note that this question doesn't have anything to do with the speed of npm install, package caching, package download speed or the like.
Thanks.

Related

npm run client & npm run server not running properly (won't run at the same time)

In the image you can see the scripts I am using, and on the left side is the folders hierarchy.
According to the defined script when I am running npm run watch, it should run server script and client script as well. But it's only running one script server. It never actually runs the client script.
Same thing happens if I put it like npm run client & npm run server in watch script. It runs only client script and never reaches server script.
One more: If I run npm install it doesn't install as defined in the script. Throws error EISDIR (error shown in image).
Please explain why is this happening and how I can fix this.
Thank you
there is a npm package called concurrently, you can install it as dev dependency and use it to run your watch script.
first install concurrently: "npm isntall concurrently --save"
then on your script: "watch": "concurrently "npm run server" "npm run client""
you can now run both server and client from one script.
source: https://www.npmjs.com/package/concurrently
Try running this script:
"watch":"npm-run-all --parallel server client"

npm run watch fails to run, but gives no error

I am working on a project with a couple of others using laravel and react/material ui.
Something happened to my npm (someone tried to fix it and ran npm audit fix --force) and therefore I needed to reinstall it. I deleted npm, deleted node js, reinstalled node js and ran npm install.
I have tried countless things so far, including things like remove node modules and package-lock.json and using rm node_modules and rm package-lock.json followed by npm cache clear --force.
When I run something like npm run watch or npm run dev this is the output I get.
> watch
> npm run development -- --watch
> development
> cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js "--watch"
After that I expect it to compile and stuff but instead I see the line
SP C:\xampp\htdocs\projectName
I have tried it with xampp running and without xampp, I also tried it from the phpstorm terminal and the cmd (with admin rights) but sadly nothing seems to work.
The only solution I could find in the end was reïnstalling Windows. There probably was something like a node or npm folder left somewhere that was interfering with the rest.
In my case, another npm run watch was running in background from another launch
killing it solved

Is there good way to run a script only when the user install my npm package manually?(not installed by dependency)

I'm under developing my npm package and I want to run a specific script only when user installed my package manually like npm install my-package --save-dev or something npm command.
I'd not like to run the command when the package was installed via package dependency.
For example, My package is dependency of the other package other-package.
Even if the user installed other-package manually and npm should install my-package as dependency, I'd not like to run the script.
Is there good way to handle this?
npm has a set of scripts that will automatically run when npm is launched a particular way. The scripts you might be interested in are:
prepublish: Run BEFORE the package is published. (Also run on local npm install without any arguments.)
publish, postpublish: Run AFTER the package is published.
preinstall: Run BEFORE the package is installed
install, postinstall: Run AFTER the package is installed.
There is no event that exactly matches your criteria but you could use one of the install events and then have an intermediate script that detects the npm command line options before your actual script.
Due to the (imo horrible) way prepublish works, a number of people have written modules to do a similar task and these could easily be adapted to your requirements.
iarna/inpublish is a good example. It check's process.env['npm_config_argv'] for the existence of /^i(n(s(t(a(ll?)?)?)?)?)?$/
Using the following package.json setup:
"scripts": {
"postinstall": "my-install && install-manual-tasks || not-my-install"
}
If my-install uses process.exit(0) then install-manual-tasks will run. If you process.exit(1), not-my-install will clean up so the npm task doesn't fail.
I think this setup actually has an issue. If your install-manual-tasks fails, the exit status is silenced and the npm task won't fail but it's a start at least. You could work around this by doing all your checks in the install-manual-tasks script, then you don't need use the shell tricks to run multiple scripts.
You can take a look at https://superuser.com/a/105389/627275 to see how to create a shell function that acts as an alias to a command. This way, npm install would act as an alias to whatever you want to run in reality. It could also be an alias to bash something.sh & npm install.
Hope that answers your question!

npm installing dev dependencies on production

I set NODE_ENV to production and tried to install dependencies using a Capfile which contains this:
run "cd #{latest_release} && npm config set production=true && npm install --production"
or this:
run "cd #{latest_release} && npm install --production"
but I always get also the dev dependencies, which is annoying because after a few releases all the inodes are taken and I cannot create any other files on the deploy machine.
I set the environment variable like this in the Capfile:
set :default_environment, {
'NODE_ENV' => 'production'
}
run "echo $NODE_ENV"
and it echoes the correct value.
If I execute
npm install --production
from within a shell, it works correctly. The user that makes the capistrano deploy and this shell user are the same, so I'm quite lost. Any hints?
The problem was:
sudo npm link
which I ran after the install command and which installs all dependencies. The fix is:
sudo npm link --production

Jenkins script quitting prematurely when using npm install on Windows

In my Jenkins job I want to build a JavaScript app using Grunt. The Jenkins build scripts creates a build directory (if it doesn't already exist), changes to that directory and runs:
npm install grunt
npm install grunt-zip
grunt --gruntfile=[something]
(Of course grunt-cli is installed globally.) When I build the job, the first statement causes Grunt and dependencies to be pulled down as expected. However, the job then terminates successfully:
Archiving artifacts
No emails were triggered.
Finished: SUCCESS
The second npm install is not run. Any idea why the script is terminating after running npm install instead of continuing to the subsequent statements?
So it turns out that npm is a batch file, not an executable, so it needs to be invoked using call from the Jenkins script:
call npm install grunt
i would recommend not using the local grunt / nodejs install, but instead getting jenkins to do this for you!
it's much easier and means there's less coupling to system specific installs and variables.
steps:
a) use nodejs jenkins plugin + get it to install nodejs on machine/grunt-cli -> Jenkins integration with Grunt
b) populate your package.json with any nodejs dependances required, eg grunt/grunt-zip etc
c) when running grunt just do a "npm update" before "grunt" command
that way your not doing explicit npm install, it's all configured from your package.json, and your build scripts will be less brittle, and your developers can use the same steps as the build server, eg "npm update;grunt" locally same as build server
For future googlers:
use command chaining for this.
This works:
npm install && npm install install grunt-zip
This wont work:
npm install
npm install grunt-zip

Resources