简体   繁体   中英

Issues in installing node and npm

I have to install node and npm. Before installing I did a bit of search for the best way to install these things. What I found is:

  1. Install nvm
  2. Install node using nvm
  3. Set the nvm alias default so that the node will be available by default.
  4. You will also get npm while installing node .
  5. Change the global modules directory to some path inside your HOME so that while installing global scoped packages using npm you don't have to use sudo.
  6. Install npm again using npm as a global module.
  7. Add new global module directory's bin to our $PATH environment variable

But after doing this npm is not working. Its giving following error:

$ npm install -g yo

> spawn-sync@1.0.13 postinstall /Users/aapa/.node_modules_global/lib/node_modules/yo/node_modules/cross-spawn/node_modules/spawn-sync
> node postinstall

/Users/aapa/.node_modules_global/bin/yo -> /Users/aapa/.node_modules_global/lib/node_modules/yo/lib/cli.js

> yo@1.4.7 postinstall /Users/aapa/.node_modules_global/lib/node_modules/yo
> yodoctor

sh: yodoctor: command not found
npm ERR! Darwin 14.3.0
npm ERR! argv "node" "/Users/aapa/.node_modules_global/bin/npm" "install" "-g" "yo"
npm ERR! node v0.12.7
npm ERR! npm  v2.14.1
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn

npm ERR! yo@1.4.7 postinstall: `yodoctor`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the yo@1.4.7 postinstall script 'yodoctor'.
npm ERR! This is most likely a problem with the yo package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     yodoctor
npm ERR! You can get their info via:
npm ERR!     npm owner ls yo
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /Users/aapa/npm-debug.log

Here are all the actual commands I ran:

#1. Install nvm
$curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.26.1/install.sh | bash

#2. Install node using nvm
$nvm install node
# This will install the latest stable version of node. Here `node` is an alias to stable 
#node -> stable (-> v0.12.7) (default)
#stable -> 0.12 (-> v0.12.7) (default)

#3. Set the nvm alias default so that the node will be available by default.
$nvm alias default node

#4. You will also get npm while installing node.
$ which npm
/Users/aapa/.nvm/versions/node/v0.12.7/bin/npm

#5. Change the global modules directory to some path inside your HOME so that while installing global scoped packages using npm you don't have to use sudo.
$ npm config get prefix
/Users/aapa/.nvm/versions/node/v0.12.7
$ cd && mkdir .node_modules_global
$ npm config set prefix=$HOME/.node_modules_global
$ npm config get prefix
/Users/aapa/.node_modules_global
$ cat .npmrc
prefix=/Users/aapa/.node_modules_global

#6. Install npm again using npm as a global module.
$ npm install npm --global
/Users/aapa/.node_modules_global/bin/npm -> /Users/aapa/.node_modules_global/lib/node_modules/npm/bin/npm-cli.js
npm@2.14.1 /Users/aapa/.node_modules_global/lib/node_modules/npm

#7. Add new global module directory's `bin` to our `$PATH` environment variable
$ echo -e '\n\n#adding npm global module location to path\nexport PATH="$HOME/.node_modules_global/bin:$PATH"' >> ~/.bash_profile
$ source ~/.bash_profile

# check
$ which npm
/Users/aapa/.node_modules_global/bin/npm

There are also some other related resources on web:

  1. SO- NPM permission error while installing - Here the answers suggested to use sudo which I am trying to avoid.
  2. Yeoman issues - can not install yeoman on my mac running OS X Yosemite 10.10.1 - I think I ahve done everything according the solution here except the solution here uses .bashrc and I have used .bash_profile .

I am not sure where I am doing it wrong. Any ideas?

i think the best and easiest way would be to simply download node from the website: nodejs.org and install it.

after that open your cmd, go to the download nodejs folder from there and type

npm install package-name

if you want to install it globally then use -g like

npm install package-name -g

The easiest solution is to simply not do steps 5, 6, and 7.

The downside is you have to install your globals separately for each version of node you are using, but it sounds like you aren't switching versions very often.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM