How to change npm globally installation path - node.js

Is it possible to change where you install your dependencies when doing npm install -g module? I know it installed in your C:/../{name}/Appdata..etc but I want to change the path on mine due to limited disk space.
I've installed node.js on an external disk which is fine and can do npm commands, but now I want the global dependencies to be installed on this disk as well.
Is there a way to do it?

You can configure it to new PATH by the following command -
npm config set prefix '~/.npm-new-global'

You can also use the environment variable NPM_CONFIG_USERCONFIG to set a new local for your config file, that is, npmrc. For instance, you can add to you login config file (~/.profile) the following line
export NPM_CONFIG_USERCONFIG="$HOME/.config/npm/npmrc"
Then, on the file ~/.config/npm/npmrc you can write
cache=~/.cache/npm
prefix=$~/.local/share/npm
Here, the advantage is that you have set both the ~/.npm and its cache to match with the XDG Base Directory Specification
reference

Related

Is it possible to make the node command see a node_modules folder on a different location than the specified one?

I know I can install Node.js libraries with the command npm from the command line... So, if I type:
npm install mylibrary
It'll create a node_modules folder on my current location and it'll install mylibrary (if it exists on npm)... Let's say that I create a Node.js code using it as the following:
const mylibrary = require('mylibrary')
mylibrary.doSomething()
Since I've installed this library on my current folder with the last command, this node.js script will work only if I save it on a file on my terminal current location as well. If I name this file as file.js, I can execute it with:
node file.js
Well, my problem is that I have a very specific case where the node_modules folder cannot exist in the same location that my file.js. Also, I'd like to avoid having to install mylibrary globally. Is there any way of defining as a parameter the path that the command node will search for the node_modules folder? I've checked node --help and it seems there's a lot of options, but I didn't manage to make it work. Is it possible to do? Can I use a node_modules folder that's neither the one of my file.js path or a global installed library?
You can have a link for the node_modules folder. For example, to create the symbolink link in Linux do this:
cd /path/to/script/folder
ln -s /path/to/where/you/can/have/node_modules node_modules
If you can't have the link, use the full path to the node_modules folder in the require, for example:
const mylibrary = require('/path/to/node_modules/mylibrary')
Also, you can solve the problem by using the NODE_PATH environment variable, set it like this
export NODE_PATH=/path/to/node_modules

How to change the npm prefix without config?

I accidentally changed the npm prefix to a place that doesn't exist. Is there a configuration file I can access that would allow me to change this back?
The only options I can think of are:
Completely Uninstall Node (npm not responding after changing the prefix)
I guess I could create the directories that don't exist and move the npm files there.
But it seems like there should be a config file I can change somewhere, right?
prefix can be defined per-install and other commands using --prefix, but as a global setting it is in ~/.npmrc (C:\Users\<your user name>\.npmrc). You can remove / edit it directly in that file.
You can also use npm config set prefix $value, or npm config delete prefix if you prefer.
A Simple ln -s /usr/local/bin/n /usr/bin/n has fixed the issue.

NPM - Conditional additions to global path

In a Node package.json file, you can map multiple executables to the PATH environmental variable on a global NPM install (npm install -g):
"bin": {
"foo": "./bin/foo.js",
"bar": "./bin/bar.js"
},
I have a unique project that requires mapping existing PATH variables on Operating Systems that do not have it. For example, I want to add a command named grep to PATH, if and only if it is being installed on a Windows computer. If the computer is running any other OS, the NPM installation will obviously fail.
Is there any way to run logic that pre-determines what bin options are available in the installation?
Oh snap - I just had an idea!
Would this work:
Parent module has npm (programmatic version) as a dependency.
On global installation, run a post-install script as declared in the package.json of parent module.
Post-install script does a check on the system to see which commands exist. This would be more mature than "Windows or not Windows" - it would try to exec a list of commands and see which ones fail.
For every command that doesn't exist, post-install script programmatically runs npm install -g on all sub-modules (one for each command, such as grep).
This would take a while and the npm module is huge, but it seems like it would work. No?
There doesn't seem to be a way to do this directly through package.json, but it might be possible (and desirable) to do something like:
Make a separate npm module for each executable you want to register (eg my-win-grep).
In the my-win-grep module, implement the executable code you want to run, and register the PATH/executable value in this module.
In the package.json for my-win-grep, include an os field that limits it to installing on windows.
In your original module, list my-win-grep as an optionalDependency.
In this way, if someone installs your original module on Windows, it would install my-win-grep, which would register an executable to run under the grep command.
For users on other systems, the my-win-grep module would not install because of the os requirement, but the main module would continue to install because it ignores failures under optionalDependencies. So the original grep executable would remain untouched.
Updated question
This approach does sound like it should work - as you say, the npm dependency is pretty large, but it does avoid having to preform additional symlinking, and still has the benefit outlined above of having each piece of OS specific functionality in a separate module.
The only thing to watch for, possibly, in the programmatic npm docs:
You cannot set configs individually for any single npm function at
this time. Since npm is a singleton, any call to npm.config.set will
change the value for all npm commands in that process
So this might just mean that you can't specify -g on your installs, but instead would have to set it globally before the first install. This shouldn't be a problem, but you'll probably need to test it out to find out exactly.
Lastly...
You might also want to have a look at https://github.com/lastboy/package-script - even if you don't use it, it might give you some inspiration for your implementation.

NPM how to configure location of global repository

Does anyone know how to configure location of global repository?
My global repo is somewhere under $HOMEDRIVE/$HOMEPATH/blahblahblah
and all my packages are being installed under that place fopr global reference
but I want to park it somewhere specific and secret like the docroot of my appserver ? so I can operate demos and proof-of-concepts and prototypes ands show them off
can you tell me how I can configure the path to my global repository? I am on windows7 which is thoroughly supported and chmod chown issues are not as prevalent on linux
is this directory anchor controlled by a designated variable within NPM?
is this variable ever referenced by javascript modules indiscriminantly? i would hope not
I assume this variable is within the NPM tool itself.
what about bower... would bower operate the same configurable? or is bower a different animal and place.
is bower a subset of npm? anmd of so does it operate the same configuration as npm?
thank you
See the npm docs about the folders. It states that the global modules are installed under a configured prefix. You can get it from the npm config comand:
npm config get prefix
And you may change it with a similar command:
npm config set prefix /path/to/my/global/folder
However, modules are usually installed globally if want to use some command line command they provide. For using in some node.js application, prefer to install them locally. If you still want to use the globally installed modules inside the application, you should use the link command (though I'm not sure if it works in a Windows environment).
Bower is another thing completely. Looking at the api documentation, you will see that there is no option to install modules globally (which makes sense, as Bower is intended for front-end dependencies).
You could change the default folder using the directory parameter of your .bowerrc file (see the documentation). This way you would be able to set all projects to use the same folder, but notice that's not the way it's intended to use and you would need to set it in all projects.
npm config set registry <registry url>
once this command is run, check in ~/.npmrc, it must show your changes.

Making an express work with the express installed globally on windows

I have installed expressjs and nodemon i.e npm install -g express but i want the app i create express the_app to work with the modules that are installed globally.I can access npm on the command line so that part works.What should i do to use this globally installed modules?.
This should work, but you likely need to set
ENVIRONMENT VARIABLES
NODE_PATH
':'-separated list of directories prefixed to the module search path.
set that variable to where npm installed modules globally. This is needed at run time if not all of the modules (including your source) are installed in the same source tree.
If you are on windows,the globally installed modules are in this location C:\Documents and Settings\Josef Doe\Application Data\npm\node_modules by default.
Please copy that path(note that you have to enable view hidden folders) if you want to copy that path or if you know your username,just replace the Josef Doe username with yours.
After copying the path,right click on the computer after clicking the start button and head over to enviromental variables. While there identify the section called System variables and click the New button.
You shall be asked to add variable name and there you shall enter NODE_PATH and on the variable value section copy the global modules path you copied earlier.
Everything should work now.

Resources