mac添加环境变量

两个问题:

1.nodejs的安装

使用Mac OS X Binaries (.tar.gz)(64位)来安装:

http://www.nodejs.org/download/

安装参照:http://blog.snowinmay.net/?p=41

安装要点:

1.1把之前的nodejs文件都清理掉:

rm -r bin/node bin/node-waf include/node lib/node lib/pkgconfig/nodejs.pc share/man/man1/node.1

1.2解压文件到自己喜欢的目录(最好是系统默认的程序安装目录,这样不用写环境变量)

我写的环境变量:export PATH="~/work/node/bin:$PATH"

我这次安装nodejs出现问题是因为在复制文件的时候把bin文件夹中的文件多复制了一层:

本来应该是这样的:

 

Prince-2:node snowinmay$ ls
ChangeLog README.md include share LICENSE bin lib

Prince-2:node snowinmay$ cd bin/
Prince-2:bin snowinmay$ ls
node npm

node和npm在bin中,我复制的时候把node和npm放在了bin/bin/下面,所以和环境变量对应不起来了。

 

2.npm的安装

npm的出错分两次,第一次是因为:curl https://npmjs.org/install.sh | sh 写错了,提示错误一直是:

directory sh: line 2: syntax error near unexpected token<'
'h: line 2: `<head><title>301 Moved Permanently</title></head>
Prince-2:node snowinmay$ sudo npm install -g npmlog

正确的写法应该是:

curl -0 -L http://npmjs.org/install.sh | sudo sh

运行之后提示:

npm cannot be installed without nodejs.
Install node first, and then try again.

Maybe node is installed, but not in the PATH?
Note that running as sudo can change envs.

PATH=~/work/node/bin:/Users/snowinmay/.rvm/gems/ruby-2.0.0-p451/bin:/Users/snowinmay/.rvm/gems/ruby-2.0.0-p451@global/bin:/Users/snowinmay/.rvm/rubies/ruby-2.0.0-p451/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Users/snowinmay/.rvm/bin

提示环境变量写错了,怪不得之前用:which node 和which npm都无效,看了一下环境变量文件:

export PATH="~/work/node/bin:$PATH" 改成了:

export PATH="$HOME/work/node/bin:$PATH"

Prince-2:bin snowinmay$ which node
/Users/snowinmay/work/node/bin/node
Prince-2:bin snowinmay$ which npm
/Users/snowinmay/work/node/bin/npm

Prince-2:bin snowinmay$ $PATH
-bash: /Users/snowinmay/work/node/bin:/Users/snowinmay/work/node/bin:~/work/node/bin:/Users/snowinmay/.rvm/gems/ruby-2.0.0-p451/bin:/Users/snowinmay/.rvm/gems/ruby-2.0.0-p451@global/bin:/Users/snowinmay/.rvm/rubies/ruby-2.0.0-p451/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Users/snowinmay/.rvm/bin:/Users/snowinmay/.rvm/bin:/Users/snowinmay/.rvm/bin: No such file or directory

运行:

Prince-2:~ snowinmay$ curl -0 -L http://npmjs.org/install.sh | sh
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 193 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 6711 100 6711 0 0 2324 0 0:00:02 0:00:02 --:--:-- 5386
tar=/usr/bin/tar
version:
bsdtar 2.8.3 - libarchive 2.8.3
install npm@latest
fetching: http://registry.npmjs.org/npm/-/npm-1.4.21.tgz
0.10.29
1.4.21
cleanup prefix=/Users/snowinmay/work/node
find: /Users/snowinmay/work/node/share/man: No such file or directory
find: /Users/snowinmay/work/node/share/man: No such file or directory
find: /Users/snowinmay/work/node/lib/node: No such file or directory
find: /Users/snowinmay/work/node/lib/node: No such file or directory

All clean!

> npm@1.4.21 prepublish .
> node bin/npm-cli.js prune --prefix=. --no-global && rm -rf test/*/*/node_modules && make -j8 doc

make: Nothing to be done for `doc'.
/Users/snowinmay/work/node/bin/npm -> /Users/snowinmay/work/node/lib/node_modules/npm/bin/npm-cli.js
npm@1.4.21 /Users/snowinmay/work/node/lib/node_modules/npm
It worked

问题结束

===================================

/etc/profile;/etc/bashrc 是针对系统所有用户的全局变量,只有root用户才能修改这两个文件,对一般用户来说是他们是只读的。一般用户要想修改它们,可以在命令前加sudo,意思是以Root身份执行,比如:sudo vi /etc/profile  ,然后按照提示输入密码即可。

因此,对于一般用户而言,通常我们建议去修改~/.bash_profile来设置环境变量,它是用户级的设置,只对当前用户有效。
操作步骤:1、打开Terminal(终端)
            2、输入:vi ~/.bash_profile
            3、设置PATH:export PATH=/usr/local/mysql/bin:$PATH
            4、输入::wq    //保存并退出vi
            5、修改立即生效:source ~/.bash_profile
            6、查看环境变量的值:echo $PATH
注意:1、~/.bash_profile中有个点
        2、如果是新增环境变量或者是修改环境变量的值,都需要source一下才能立即生效。如果是删除一个环境变量,必须输入exit以logout当前shell,然后再重新打开一个新的shell并login才能生效。
 

Prince-2:~ snowinmay$ vim .bash_profile 

Prince-2:~ snowinmay$ source ~/.bash_profile
Prince-2:~ snowinmay$ $PATH
-bash: ~/work/node/bin:/Users/snowinmay/.rvm/gems/ruby-2.0.0-p451/bin:/Users/snowinmay/.rvm/gems/ruby-2.0.0-p451@global/bin:/Users/snowinmay/.rvm/rubies/ruby-2.0.0-p451/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Users/snowinmay/.rvm/bin:/Users/snowinmay/.rvm/bin: No such file or directory

但是npm还是不能用:

Prince-2:node snowinmay$ node -v
v0.10.29
Prince-2:node snowinmay$ npm -v

module.js:340
throw err;
^
Error: Cannot find module 'npmlog'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at /Users/snowinmay/work/node/bin/npm:19:11
at Object.<anonymous> (/Users/snowinmay/work/node/bin/npm:87:3)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
Prince-2:node snowinmay$
Prince-2:node snowinmay$
Prince-2:node snowinmay$ make install
make: * No rule to make target install'. Stop. Prince-2:node snowinmay$ curl https://npmjs.org/install.sh | sh % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 193 100 193 0 0 119 0 0:00:01 0:00:01 --:--:-- 153 sh: line 1: html: No such file or directory sh: line 2: syntax error near unexpected token<'
'h: line 2: `<head><title>301 Moved Permanently</title></head>
Prince-2:node snowinmay$ sudo npm install -g npmlog
Password:
sudo: npm: command not found
Prince-2:node snowinmay$
Prince-2:node snowinmay$ which node
Prince-2:node snowinmay$ which npm

posted @ 2014-07-17 22:57  snowinmay  阅读(18758)  评论(4编辑  收藏  举报