package.json for global modules - not dependencies - npm

I would like to list some modules that I want to install globally - they are not project related (things like bower, npm-check-updates, ... which I use all the time). Is it possible to manage this with a "global" package.json, or do I need to maintain a separate shell script to perform this installation? Currently I am doing:
# global-npm-pacakges.sh
npm install -g npm-check-updates#1.5.1
npm install -g bower#1.4.1
Any other way of doing this?

Your way is fine but that means you also need to maintain another script to
uninstall all of them globally
Using npm install -g with a global package.json, you still have to manage a clean up script.
There is no npm uninstall -g based on the package.json.
I think you need to
npm uninstall name<#version> or
npm -R name<#version>
Doing npm -R node_modules is not gracefully way of doing it.
It does not clean up the /tmp/xxx
There is some suggestions of using a symlink in this threads for your references:
How do I install a module globally using npm?

Related

is it neccessary to specify '-D' while using npm install?

for example, when I need to install webpack, usually we are supposed to use "npm i -D webpack", but if I use "npm i webpack" instead, there is no difference in my final bundle file, why we need to specify '-D'?
I think there is no differences between them, why not just use npm i?
npm i -D
npm i
As per the docs, npm install -D means you are installing the package as a devDependency and not a regular dependency.
How is this different?
When your app is deployed and your server runs the npm install step, it will only install the packages that are in dependency and not those in devDependency. Hence, we usually install things like eslint, typescript and such as devDependency and not regular depedency.
Reference
https://docs.npmjs.com/cli/v6/commands/npm-install

Installing npm globally

Is it possible to install npm globally and is this a good idea?
I installed npm with the npm install command and was able to run npm start. Then after publishing my project to github I wanted to make sure it would run if someone cloned it, so I cloned it to a different directory on my machine. I then had to run npm install again to install the dependencies. Is it necessary to do this for each project you build locally or is it better and possible to install it globally on your machine?
Thanks
Command line for install npm globally--
npm install -g <package>
For more read from here.
In general, the rule of thumb is:
If you’re installing something that you want to use in your program,
using require('whatever'), then install it locally, at the root of
your project.
If you’re installing something that you want to use in your shell, on
the command line or something, install it globally, so that its
binaries end up in your PATH environment variable.
Details you can read here.
To install a module from npm globally, you'll simply need to use the --global flag when running the install command to have the module install globally, rather than locally (to the current directory).
you can use command:
npm install <module> --globalor npm install <module> -g
note: <module> is the name of the module you want to install globally

Installing npm packages from multiple registries

Working on a project where I need to install npm packages from several registries - the default npm registry and several custom registries.
My existing solution is to use npm scripts to break the install into steps which use the --registry flag. Something like this:
"install-pkg1": "npm install pkg1 --registry https://pkg1.domain.com",
"install-pkg2": "npm install pkg2 --registry https://pkg2.domain.com",
"install-custom": "npm install && npm run install-pkg1 && npm run install-pkg2"
Then use npm run install-custom in place of npm install to install all the dependencies.
Is there a more preferred method for installing packages from multiple registries?
As noted in the comments below, there is a discussion on Github about this.
This is pretty much one of the reasons for npm ci. The registries and versions will be defined there so you shouldnt need to do anything crazy to get them all from their corresponding registries. Also, this can kind of be accomplished with scopes, where you associate a registry with a scope, but this requires you publishing the packages scoped as whatever. more info on associating registry with scopes

grunt js installing packages

I'm building a grunt javascript project with grunt, and I have a package.json file that looks something like:
{
... name, author, etc here ...
"dependencies": {
"grunt-html":"0.2.1"
}
}
I can run npm install to install grunt-html and this works just fine. But when I add new dependencies, all developers on the team must know to run npm install again. Is there a way to automatically install any packages that have not yet been installed? Should I just run npm install always to ensure I'm up to date?
Yes npm install is the easiest way IMO. Getting everyone familiar with the other npm commands makes managing deps easier as well. Such as:
npm ls to list out the currently installed modules.
Or the --save flag ie, npm install grunt-html --save to install and insert the package and version into your package.json.
npm prune to remove modules not included in your package.json.
Other ways to manage dependencies are to commit the node_modules folder in your repository to avoid other devs from having to run npm install. Or for more complex projects consider using npm shrinkwrap to lock down dependencies to specific versions: npm shrinkwrap docs.
I have not tried grunt-install-dependencies (https://github.com/ahutchings/grunt-install-dependencies), but it seems this may fullfill your needs. Just add the command install-dependencies as first task within your custom definfed grunt tasts, e.g.
grunt.registerTask('build', [ 'install-dependencies', 'useminPrepare', ... ]);

When installing locally (npm install .), can I have npm use my global packages?

When I run npm install . it takes a while to build packages that contain c code like expresso (which depends on node-jscoverage). I realized that I can copy expresso from my global package directory (~/Developer/lib/node_modules/expresso) to ./node_modules/expresso in my current directory before running npm install . and it won't bother compiling it. Is there a way to tell npm to try to install packages from my global npm directory before fetching and building them?
I guess this command might help: npm link
Check out this: npm to install packages from local position rather than from web?