How do I update npm in linux? - linux

I've tried the following in order to update npm. But no commands seem to work.
What should I do so I can update npm?

The npm update command updates all packages (no arguments), a specific package (with a package name as argument); in the local directory or in the global modules directory (-g argument).
As stated in the npm documentation, sometimes the npm update npm -g command doesn't work, especially if you run an old version of npm, which you are. To really update npm, run this:
$ curl https://npmjs.org/install.sh | sh

Related

my own NPM package not working command not found

If I don't install my own CLI globally, I can't run it with my keyword command. Only if I install the NPM package globally, the start works with my keyword. I would like to install it with the command suggested by NPM and then run it with keyword.
Visit my CLI: https://www.npmjs.com/package/sweetstack
Installation as globally it works, started with sweetstack.
npm i -g sweetstack
Installation as local not working.
npm i sweetstack
This behavior is expected, if your inspect the $PATH environment variables of your shell, you'll see that only the directory containing the globally installed packages is present.
If you want to install your tool locally (which is a good practice), you then have to run it through npx.
The associated documentation page details what it does more clearly and it's relation to npm exec : https://docs.npmjs.com/cli/v9/commands/npx
cd ./your-project
npm i sweetstack
npx sweetstack

The preferred way of installing and using global npm packages?

To install a package globally, one has to use sudo, but then again npm does not recommend using sudo. So the official recommendation is this. I followed all the steps from 1 to 5 in Option 2. But when I want to use an NPM package from terminal, I have to define the full path. For instance I installed npm-check-updates, but I can't run ncu or npm-check-updates command from terminal, because it says bash: ncu: command not found. I have to type the full path like this: ~/.npm-global/bin/ncu.
How do I fix this?
Hi there I noticed this hasn't been answered so I'm responding just in case I or anybody else runs into the same problem in future.
Here's what worked for me:
npm install -g npm-check-updates
ncu -u
after running the second command, you should see a list of all your dependencies and their latest versions then run
npm install
to install new versions.

Switch versions of node.js packages

I installed two different versions of a node package:
npm install sails -g
npm install sails#beta -g
What could I use to use these versions according what is specified in package.json in a directory basis?
First of all, when you are doing
npm install sails -g
npm install sails#beta -g
it installs the packages globally, and the second command will override the first. One of the main purposes of global installs is having the executable command (sails in our case) available in the PATH. And this command, basically, defines for which version of Sails you will generate the new application when you type sails new ....
Long story short, if you really need to be able to use two different versions of the package, you can install one of them locally and then provide the full path to the executable. Something like:
npm install sails -g
mkdir -p ~/tmp
cd ~/tmp
npm install sails#beta
cd ~/Sites
sails new thisWillBeAStableApp
../tmp/node_modules/.bin/sails new thisWillBeABetaApp
The generators are supposed to configure package.json files accordingly.

NPM appears to do nothing on Linux Mint 15

searched, and did not see this specific problem.
Trying to get a MEAN stack built on my Linux Mint machine, and bumping into a bit of an unusual issue.
Got MongoDB installed, and finally got it running correctly (none of the instructions ANYWHERE mentioned having to create the /data/db/ directory and set permissions, go figure).... it works now.
Got NodeJS installed, and it appears to work correctly.
I had been told (apparently incorrectly) that NPM installs right alongside Node, with:
sudo apt-get install nodejs
but:
$ npm
bash: /usr/bin/npm: No such file or directory
So I go ahead and install NPM separately.
$ sudo apt-get install npm
Seems to work, so far, no errors, and it looks like it is pulling down the NPM package and installing it...
$ nodejs -v
v0.10.21
$ npm -v
$
?? It simply fails to respond without any error... so I try:
$ npm install grunt -g --save-dev
$
Same completely silent failure... in fact, NOTHING I could do gets a response out of NPM.
Looked all over the web, and saw nothing similar anywhere... found out that NPM holds its cache files in ~/.npm and noticed that this folder didn't exist (kinda like the mongo issue above), so I created it, and set permissions to 7777... still nothing.
Purged and re-installed both node and npm, tried installing them both together and separately (yes, desperation)... still no love.
WTF am I doing wrong?
I would love, eventually, to have a nice development environment setup, hopefully with Cloud9 as a local IDE.... but already pulling my hair out.
=========================================================================================
OK, after a few more headaches, this is up and built now... thank you all.
Would love to mark both as answers, but it won't let me.
npm does come with node. Where is apt-get pulling it from? I install the Mac OS X packages on my Mac for development and npm does in fact come with it. I compile from source on my CentOS server and npm comes with it.
Your package provider may be providing them separately as a (in)convenience to you.
As for why your npm command does not work after installation, I can't say for sure, but I am suspicious of your use of --save-dev and -g together.
-g means to install globally, which means 2 things:
It will be installed outside of your npm package's structure into a system location like /usr/bin or /usr/local/bin
It requires root access to install. Did you use sudo to run it with root access?
Both of those requirements conflict with --save-dev which records the package as a dependency in your package.json file so that future npm install commands will install that package within the project space.
That said, I happen to know a lot about grunt. It has 2 parts, a globally installed tool and the package-specific tool. The correct way to install it is:
$ [sudo] npm install -g grunt-cli
$ npm install grunt --save-dev
This will install the grunt-cli package into a system location guaranteed to be in the $PATH, which turns around and looks for a package-specific grunt installed which is not system-wide.
When installing nodejs with npm, this one-liner worked for me.
sudo apt-get install nodejs nodejs-dev npm
I don't believe npm comes with the nodejs installation in the apt repo. Try to install npm separately using the following tutorial http://www.giantflyingsaucer.com/blog/?p=1688 'To install NPM ....'
====== Edit ====
node and npm IS separate. Follow the official wiki and everything should be fine

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.

Resources