npm package.json install globally? - node.js

Is there any way to set an NPM dependency to be installed globally in the package.json file?
Whenever I run npm update all the dependencies are installed locally.
Thanks.

I believe that the -g option causes things to be installed globally.

Is your reason for installing globally in order to make new scripts available on the command line? If so, I might have a workaround for you.
Just install your packages as usual (without the -g):
npm install -S my_module_name
Including the -S flag or --save will help keep your package.json file up to date.
As usual, your project's npm install step will install locally (as you have described). However, it will also produces a local folder containing symlinks to each of the project's npm-supplied command-line executables (located inside the node_modules/.bin/ folder).
Add that folder to your system path to enable command-line access to npm modules without requiring installation via -g, or root access to a machine:
export PATH=/path/to/your/project/source/node_modules/.bin/:$PATH

Related

How do I install all the requirements with npm?

I would like to clone https://github.com/tstringer/create-react-app-with-redux and start a new project. I ran npm start and then ran npm install for each module not present, but there are many of them. Is there a way to install all the requirements? Something like pip install -r requirements.txt in Python.
Thanks,
Uri.
Just run npm install without arguments. It will resolve the required dependencies from the package.json file.
It's simple.
If you want to install all the node_modules from the package.json file you simply put: npm install in terminal (on the same directory where the package.json exists) and it would install all the node modules in the folder called node_modules.
Generally, the node_modules folder is not uploaded in a git (by putting restriction at .gitignore) because it is essentially the same folders or packages that one would have to install, *hence installing it from package.json is simpler and it saves the internet bandwidth and time.
Even you want to save something in the package.json while you are installing any npm package you can simply put npm install --save your-package-name and it would automatically save your package in the .package.json file and you can install the same file, even after you delete the node_modules folder using the same command.
Better yet, if you want to save yourself a lot of time use yarn install instead of npm install (https://yarnpkg.com/en/). It is much faster because it caches everything and operates in parallel (see https://www.sitepoint.com/yarn-vs-npm/ for a good comparison).
npm install githubname/reponame -- Repository Name you can try

Using npm, how do I globally install and use a "binary" package from a private repo?

I have a command line utility that I've written in node.js which has a "binary" it exposes via package.json's bin property under the key gogogo.
I have it hosted on a private git repo. It is not registered on the npm registry. I realize we could do a private registry there, but I would prefer not to.
I'd like to be able to have others in my organization do:
npm install -g git+ssh://git#git.mycompany.com:utils/setup.git
and then once installed access it as one would with other global binaries by just typing:
gogogo into their shell (taking advantage of it being in the PATH).
The npm install above downloads and installs it into the global modules folder just fine, but I don't have a gogogo binary or symlink in my global modules folder. Do I have to npm link? I would really like to not have to do that.
Is there a way in the npm install to specify its global binary "alias"?
This does in fact work properly by just doing the normal:
npm install -g git+ssh://git#git.mycompany.com:utils/setup.git
I wasn't seeing my gogogo command in my path because I had previously done a npm link inside of my development folder which created a symlink in my /usr/local/lib/node_modules folder to my dev folder. Once I removed the symlink /usr/local/lib/node_modules/setup, and reran the npm install -g... I was able to run gogogo from any path.

node packages are loaded from wrong path

I have this issue on global packages, that if I run npm install -g <package> that packages are installed into /Users/myUsername/.nvm/versions/node/v5.0.0/node_modules which seems correct to me.
But it runs packages from:
/usr/local/lib/node_modules
Thus these packages never get updated/ if I delete a package from there and install it globally it is not available afterwards. If I copy the package after installing from the nvm to the local folder it works.
1) Are the packages supposed to be in the nvm path or in the local one?
2) How to use the correct path?
You should install all dependencies locally if you plan to use them in your node application and make sure it is listed under dependencies or devDependencies in your package.json file.
If you intend to use them only from cmd line then you can install globally so that it'll be available from any path in your terminal.
You don't need to mention from where node_modules will be loaded in your node application.

npm installs all modules in /usr/local/lib/node_modules/

I have node.js 0.8.14 installed on Ubuntu 12.10. I created a directory in my home directory with a sub directory node_modules. I want to install some local node modules there but running
npm install myModule
in this directory installs this module in /usr/local/lib/node_modules/ (same behavior as installing the module with the -g flag
There is no node path in .bashrc.
Any idea how I can install local node modules?
After some further research I found the solution.
Running the command npm config ls revealed that the default config global=false (you see the default config with npm config ls -l) was overwritten by global=true in /home/vsdev/.npmrc and /usr/local/etc/npmrc.
Reverting this to global=false solved the issue.
That is odd.
FYI you don't need to create the node_modules directory, npm will do that for you
npm normally just installs to the current directory. Even if the package you are installing is configured to prefer global installation, npm will install it locally unless you explicitly pass the -g parameter.
can you run the following shell commands and confirm npm is really the real npm?
which npm
alias | grep npm
npm install load all in node_modules then it might be version 3 behaviour http://blog.npmjs.org/post/110924823920/npm-weekly-5 or as mentioned by #vsdev so once you make sure it version 3 behaviour and u want to go with it then its fine else follow below
1- uninstall all modules.. into the node_modules folder in your project then execute: npm uninstall *
2- Tell npm to install with legacy bundling for this one install:
npm install --legacy-bundling
A "permanent" alternative:
Set your npm config to always use legacy bundling...
npm set legacy-bundling=true
.. and run as usual:
npm install
*fetching dependencies with legacy bundling will take a lot more time because many several different versions of the same dependencies will be installed.

Node Version Manager (NVM) npm installing modules to common folder

I've installed NVM for node.js using the instructions from this post:
http://www.backdrifter.com/2011/02/18/using-nvm-and-npm-to-manage-node-js/
When I switch between node versions and then use npm to install a module, all the modules are placed in the same 'node_modules' folder (~/node_modules/) instead of in the 'node_modules' directory specific to that version of node?
Any idea on how to remedy this?
Based on the comments from https://github.com/creationix/nvm/pull/97:
When installing packages with npm using the global switch -g the
package ends up in the proper directory (i.e.
.nvm/$VERSION/lib/node_modules), however node is unable to require it
since it somehow isn't searching on it's prefix.
So using npm install -g xxxxx will put the modules in the correct location for NVM but if you try to require one of them node can't find the module. I am still playing around with this and will update if I find a solution.
Update
Where does NPM put node_modules? (see https://docs.npmjs.com/files/folders)
Local install (default): puts stuff in ./node_modules of the current package root.
Global install (with -g): puts stuff in /usr/local or wherever node is installed.
Install it locally if you're going to require() it.
Install it globally if you're going to run it on the command line.
If you need both, then install it in both places, or use npm link.
So what I did was run npm init (see http://npmjs.org/doc/init.html) in my projects root dir which generated package.json. Now when I run npm install xxxxx it creates a node_modules dir in my project folder (which I add to my .gitignore). This works for modules that I require in my code.
For commands such as CoffeeScript I install with npm install -g coffee-script which puts it in the correct directory (.nvm/$VERSION/lib/node_modules). While I can't require these modules (npm link should solve this problem) I can run the commands - i.e. coffee.
I just installed express globally (-g) and was having problem when require("express"). Just like Jesse Vogt said I just reinstalled express but this time without the -g just like this: "sudo npm install express" and now is working perfectly!
For latest nvm window version 1.1.7.
Package was installed and placed into the respective nodejs version.
nvm use 16.8.0
npm install truffle
nvm use 16.7.0
npm install mysql

Resources