前言
最近搭建Hexo
,需要安装npm
和NodeJS
. 遇到一点幺蛾子, 开帖记录下.
NodeJS
内包括npm
, 所以直接安装npm
即可.
NodeJS官网http://nodejs.cn/download/
dmg安装 (不推荐)
- 安装
使用dmg安装. 下载安装包直接安装即可. 没有什么技术含量. - 需要注意的是 安装的位置在何处?
我们前往cd /usr/local/bin/
sh-3.2# cd /usr/local/bin/sh-3.2# ls -lrt
total 82576
-rwxr-xr-x 1 root wheel 42276320 10 21 15:53 node
lrwxr-xr-x 1 root admin 38 11 2 15:56 npm -> ../lib/node_modules/npm/bin/npm-cli.js
lrwxr-xr-x 1 root admin 38 11 2 15:56 npx -> ../lib/node_modules/npm/bin/npx-cli.js
lrwxr-xr-x 1 sean admin 33 11 6 12:06 hexo -> ../lib/node_modules/hexo/bin/hexo
可以看到node
/npm
/npx
. 其中node
是二进制文件, npm
和npx
是软连接.
- 卸载
删除node
/npx
/npm
和lib/node_modules
内的内容即可.
Mac安装NodeJS的默认路径
- Other Tips
dmg
安装会默认把/usr/local/bin/lib/node_modules
当作默认的npm仓库
地址. 有时候会导致没有权限的问题. 我在安装hexo
的时候就遇到. 没有办法, 只能重装. - 操作1
node install -g hexo
SeandeMacBook-Pro:~ sean$ npm install -g hexo
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /usr/local/lib/node_modules
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR! [Error: EACCES: permission denied, access '/usr/local/lib/node_modules'] {
npm ERR! stack: "Error: EACCES: permission denied, access '/usr/local/lib/node_modules'",
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'access',
npm ERR! path: '/usr/local/lib/node_modules'
npm ERR! }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/sean/.npm/_logs/2019-11-06T03_46_31_834Z-debug.log
解决办法: 没有目录权限. 加上`sudo`即可.
【问题与解决】Mac OS通过 npm 安装 React Native 报错(checkPermissions Missing write access to /usr/local/lib/node_modules)
- 操作2
sudo npm install -g hexo
SeandeMacBook-Pro:~ sean$ sudo npm install -g hexo
Password:
/usr/local/bin/hexo -> /usr/local/lib/node_modules/hexo/bin/hexo
> fsevents@1.2.9 install /usr/local/lib/node_modules/hexo/node_modules/nunjucks/node_modules/fsevents
> node install
node-pre-gyp WARN Using needle for node-pre-gyp https download
node-pre-gyp WARN Pre-built binaries not installable for fsevents@1.2.9 and node@12.13.0 (node-v72 ABI, unknown) (falling back to source compile with node-gyp)
node-pre-gyp WARN Hit error EACCES: permission denied, mkdir '/usr/local/lib/node_modules/hexo/node_modules/nunjucks/node_modules/fsevents/lib'
gyp WARN EACCES current user ("nobody") does not have permission to access the dev dir "/Users/sean/Library/Caches/node-gyp/12.13.0"
gyp WARN EACCES attempting to reinstall using temporary dev dir "/usr/local/lib/node_modules/hexo/node_modules/nunjucks/node_modules/fsevents/.node-gyp"
gyp WARN install got an error, rolling back install
gyp WARN install got an error, rolling back install
gyp ERR! configure error
gyp ERR! stack Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/hexo/node_modules/nunjucks/node_modules/fsevents/.node-gyp'
gyp ERR! System Darwin 18.7.0
gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "configure" "--fallback-to-build" "--module=/usr/local/lib/node_modules/hexo/node_modules/nunjucks/node_modules/fsevents/lib/binding/Release/node-v72-darwin-x64/fse.node" "--module_name=fse" "--module_path=/usr/local/lib/node_modules/hexo/node_modules/nunjucks/node_modules/fsevents/lib/binding/Release/node-v72-darwin-x64" "--napi_version=5" "--node_abi_napi=napi" "--napi_build_version=0" "--node_napi_label=node-v72"
gyp ERR! cwd /usr/local/lib/node_modules/hexo/node_modules/nunjucks/node_modules/fsevents
gyp ERR! node -v v12.13.0
gyp ERR! node-gyp -v v5.0.5
gyp ERR! not ok
node-pre-gyp ERR! build error
node-pre-gyp ERR! stack Error: Failed to execute '/usr/local/bin/node /usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --module=/usr/local/lib/node_modules/hexo/node_modules/nunjucks/node_modules/fsevents/lib/binding/Release/node-v72-darwin-x64/fse.node --module_name=fse --module_path=/usr/local/lib/node_modules/hexo/node_modules/nunjucks/node_modules/fsevents/lib/binding/Release/node-v72-darwin-x64 --napi_version=5 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v72' (1)
node-pre-gyp ERR! stack at ChildProcess.<anonymous> (/usr/local/lib/node_modules/hexo/node_modules/nunjucks/node_modules/fsevents/node_modules/node-pre-gyp/lib/util/compile.js:83:29)
node-pre-gyp ERR! stack at ChildProcess.emit (events.js:210:5)
node-pre-gyp ERR! stack at maybeClose (internal/child_process.js:1021:16)
node-pre-gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:283:5)
node-pre-gyp ERR! System Darwin 18.7.0
node-pre-gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/hexo/node_modules/nunjucks/node_modules/fsevents/node_modules/node-pre-gyp/bin/node-pre-gyp" "install" "--fallback-to-build"
node-pre-gyp ERR! cwd /usr/local/lib/node_modules/hexo/node_modules/nunjucks/node_modules/fsevents
node-pre-gyp ERR! node -v v12.13.0
node-pre-gyp ERR! node-pre-gyp -v v0.12.0
node-pre-gyp ERR! not ok
Failed to execute '/usr/local/bin/node /usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --module=/usr/local/lib/node_modules/hexo/node_modules/nunjucks/node_modules/fsevents/lib/binding/Release/node-v72-darwin-x64/fse.node --module_name=fse --module_path=/usr/local/lib/node_modules/hexo/node_modules/nunjucks/node_modules/fsevents/lib/binding/Release/node-v72-darwin-x64 --napi_version=5 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v72' (1)
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/hexo/node_modules/nunjucks/node_modules/fsevents):
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 install: `node install`
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: Exit status 1
+ hexo@4.0.0
added 288 packages from 442 contributors in 100.34s
解决办法: 还是因为没有目录权限导致. 更改npm
的仓库地址. npm
应该是默认在安装目录的. 我这里采用重新安装的办法. 当然也可以把lib/node_modules
文件夹放到本地其他地方, 然后更改软连接的方法.
这就是我为什么不推荐使用.dmg
安装的原因.
tar.gz安装 (推荐)
- 安装
下载tar.gz
包. 解压tar.gz
包. 创建软连接. 配置/etc/profile
文件.
具体操作流程如下:
# 1. 前往安装目录
SeandeMacBook-Pro:Software sean$ cd Software/NodeJS/
# 把下载的tar.gz包
# 2. 解压安装包(可以手动解压 / 也可以使用tar.gz命令解压)
# 解压完成
SeandeMacBook-Pro:NodeJS sean$ ls
node-v12.13.0-darwin-x64 node-v12.13.0-darwin-x64.tar.gz
SeandeMacBook-Pro:NodeJS sean$ pwd
/Users/sean/Software/NodeJS
# 创建软连接
SeandeMacBook-Pro:NodeJS sean$ ln -s /Users/sean/Software/NodeJS/node-v12.13.0-darwin-x64 /Users/sean/Software/NodeJS/current
# 在/etc/profile文件末尾加入如下
# NodeJS 20191104
export NodeJS_HOME=/Users/sean/Software/NodeJS/current
export PATH=.:$PATH::$NodeJS_HOME/bin
- 验证
# npm
SeandeMacBook-Pro:bin sean$ npm
Usage: npm <command>
where <command> is one of:
access, adduser, audit, bin, bugs, c, cache, ci, cit,
clean-install, clean-install-test, completion, config,
create, ddp, dedupe, deprecate, dist-tag, docs, doctor,
edit, explore, get, help, help-search, hook, i, init,
install, install-ci-test, install-test, it, link, list, ln,
login, logout, ls, org, outdated, owner, pack, ping, prefix,
profile, prune, publish, rb, rebuild, repo, restart, root,
run, run-script, s, se, search, set, shrinkwrap, star,
stars, start, stop, t, team, test, token, tst, un,
uninstall, unpublish, unstar, up, update, v, version, view,
whoami
npm <command> -h quick help on <command>
npm -l display full usage info
npm help <term> search for help on <term>
npm help npm involved overview
Specify configs in the ini-formatted file:
/Users/sean/.npmrc
or on the command line via: npm <command> --key value
Config info can be viewed via: npm help config
npm@6.12.0 /Users/sean/Software/NodeJS/node-v12.13.0-darwin-x64/lib/node_modules/npm
# node
SeandeMacBook-Pro:bin sean$ node
Welcome to Node.js v12.13.0.
Type ".help" for more information.
> .exit
- Other Tips
我们可以查看下NodeJS
提供了哪些命令?通过./bin
目录下查看可以知道, 和dmg
安装一致. (npm/npx/node
)
SeandeMacBook-Pro:bin sean$ pwd
/Users/sean/Software/NodeJS/current/bin
SeandeMacBook-Pro:bin sean$ ls
hexo node npm npx
OK. 我们重新安装hexo
测试测试.
SeandeMacBook-Pro:bin sean$ npm install -g hexo
/Users/sean/Software/NodeJS/node-v12.13.0-darwin-x64/bin/hexo -> /Users/sean/Software/NodeJS/node-v12.13.0-darwin-x64/lib/node_modules/hexo/bin/hexo
> fsevents@1.2.9 install /Users/sean/Software/NodeJS/node-v12.13.0-darwin-x64/lib/node_modules/hexo/node_modules/nunjucks/node_modules/fsevents
> node install
sh: node: command not found
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/hexo/node_modules/nunjucks/node_modules/fsevents):
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 install: `node install`
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: spawn ENOENT
+ hexo@4.0.0
added 288 packages from 442 contributors in 148.64s
安装成功.
brew 安装(不推荐)
brew install -g node
brew uninstall node
- brew 安装目录 /usr/local/Cellar
- brew 配置目录 /usr/local/etc
- brew 命令目录 /usr/local/bin
brew安装的文件有一个缺陷. 那就是基本新版的brew
无法指定版本, 默认认为最新版.
Reference: brew 下 安装node 和 npm
Others - npm相关命令 / 更改npm仓库源
npm
相关命令.
npm -v
: 查看版本.npm install XXX
: 安装某些框架. Ex.npm install jquery
.npm info jquery
: 查看包的详细信息.npm view jquery versions
: 查看一个包存在的所有版本号.npm view jquery version
: 查看指定包当前的最新版本.npm install jquery@3.4.1
: 下载指定版本的包.npm list
: 查看项目安装了哪些包.npm install jquery --save
/npm i jquery -S
:–save 表示 在 package.json 文件中(dependencies)记录 下载包的版本信息.npm install jquery --save-dev
/npm i jquery -D
:下载开发依赖包,上一条命令是下载生产依赖包(这里就不讲开发依赖和生产依赖的区别).npm root -g
: 查看全局安装包的存放位置npm audit fix
: 包的修复 ,一般是包的更新npm ls jquery
: 查看当前安装包的版本- 更改
npm
仓库地址
因为国区有时候无法下载某些.js
包, 所以需要更改js
仓库地址.
//设置淘宝源
npm config set registry https://registry.npm.taobao.org
//设置本地源
npm config set registry http://127.0.0.1:4873
//查看源,可以看到设置过的所有的源
npm config get registry
此外, 还可以安装cnpm
替代npm
. 但是cnpm
不会更改仓库源. 需要手动更改.
// 安装cnpm命令,不会改变npm的源
npm install -g cnpm --registry=https://registry.npm.taobao.org
//使用
cnpm install
同时, 你还可以通过更改~/.npmrc
来更改npm仓库地址
.(没有文件创建)
registry = https://registry.npm.taobao.org