mac nodejs安装配置

来源:互联网 发布:淘宝总部投诉电话地址 编辑:程序博客网 时间:2024/05/22 04:53
mac nodejs安装配置


1、下载https://nodejs.org/en/#download,安装


2、更新
Chaim:~ Chaim$ npm -v
2.14.7
Chaim:.ssh Chaim$ sudo npm update npm -g
Password:
/usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js
npm@3.3.8 /usr/local/lib/node_modules/npm
Chaim:~ Chaim$ npm -v
3.3.8


3、同样的写个用例测试一下
var http = require('http');
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
将以上代码存为app.js,终端执行:node app.js,浏览器访问http://127.0.0.1:1337/,可以看到运行结果。




Express安装
Chaim:~ Chaim$ sudo npm install express -gf


flags安装
Chaim:~ Chaim$ sudo npm install flags -gf


其它
Chaim:~ Chaim$ sudo npm install redis -gf
Chaim:~ Chaim$ sudo npm install redis-commander -gf
Chaim:~ Chaim$ sudo npm install mysql-client -gf
Chaim:~ Chaim$ sudo npm install log4js -gf
Chaim:~ Chaim$ sudo npm install protobuf -gf
Chaim:src Chaim$ sudo npm install enum -gf
Chaim:src Chaim$ sudo npm install async -gf


安装protobuf时出错:
Chaim:~ Chaim$ sudo npm install protobuf -gf
npm WARN using --force I sure hope you know what you are doing.


> protobuf@0.11.0 install /usr/local/lib/node_modules/protobuf
> node-gyp rebuild


gyp WARN EACCES user "root" does not have permission to access the dev dir "/Users/Chaim/.node-gyp/4.2.1"
gyp WARN EACCES attempting to reinstall using temporary dev dir "/usr/local/lib/node_modules/protobuf/.node-gyp"


装上gyp、node-gyp也不行:
Chaim:~ Chaim$ sudo npm install gyp -gf
Chaim:~ Chaim$ sudo npm install node-gyp -gf


------------------------------------------------------------------------------------------------------------------------------------------------
Q:
运行程序出现问题:
module.js:339
    throw err;
    ^


Error: Cannot find module 'flags'


A:
为何发生Cannot find module问题


首先明确全局模块的默认安装位置:


$npm root -g
X:\Program Files\nodejs\prefix\node_modules
接着查看全局模块的默认搜索路径:


$node
> global.module.paths
[ 'X:\\Users\\Admin\\repl\\node_modules',
  'X:\\Users\\Admin\\node_modules',
  'X:\\Users\\node_modules',
  'X:\\node_modules' ]
发现两者并没有交集,难怪找不到。


那么解决方案就简单了,先npm install node-static -g,然后可以添加环境变量 NODE_PATH ,指向 npm root -g 给出的路径。


然后执行:
export NODE_PATH=/usr/local/lib/node_modules




------------------------------------------------------------------------------------------------------------------------------------------------
Q:
Chaim:data_svr Chaim$ node
> require("protobuf")
Error: Cannot find module 'protobuf'
    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 repl:1:2
    at REPLServer.self.eval (repl.js:110:21)
    at Interface.<anonymous> (repl.js:239:12)
    at Interface.emit (events.js:95:17)
    at Interface._onLine (readline.js:203:10)
    at Interface._line (readline.js:532:8)
装protobuf的库一直失败,把nodejs也降到0.10.36同样错误:
Chaim:.nvm Chaim$ sudo npm install protobuf -g
/
> protobuf@0.11.0 install /Users/Chaim/.nvm/v0.10.36/lib/node_modules/protobuf
> node-gyp rebuild


gyp WARN EACCES user "root" does not have permission to access the dev dir "/Users/Chaim/.node-gyp/0.10.36"
gyp WARN EACCES attempting to reinstall using temporary dev dir "/Users/Chaim/.nvm/v0.10.36/lib/node_modules/protobuf/.node-gyp"




A:
sudo npm install --unsafe-perm --verbose -g protobuf
来回无数次终于能下载完并安装了,可能是天朝的网络问题吧
> require("protobuf")
{ Schema: [Function: Schema] }
看起来是成功了!



0 0
原创粉丝点击