1.使用一个proxy来代理访问,但是这个方法速度可能比较慢;
2.可以通过修改npm的配置文件让npm到另外的pacakge mirror站点去找package,通过如下命令
$npm config set registry https://registry.npm.taobao.org
$ npm config set registry http://r.cnpmjs.org
或者:npm config set registry http://registry.npmjs.eu
npm install
随后再执行
或者直接在命令行中指定某些参数,比如phantomjs是一个无图形界面的浏览器,在自动化测试中应用广泛,可能的安装方式:
npm install phantomjs --phantomjs_cdnurl=http://cnpmjs.org/downloads
如果上述方法都不奏效,那么可能需要 配置自己网卡的dns为国外的dns
npm config set proxy http://proxy.company.com:8080 npm config set https-proxy http://proxy.company.com:8080
$ npm install -g gulp npm ERR! tar.unpack untar error /home/cabox/.npm/gulp/3.9.0/package.tgz npm ERR! Linux 2.6.32-042stab104.1 npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "-g" "gulp" npm ERR! node v0.12.3 npm ERR! npm v2.9.1 npm ERR! path /usr/local/lib/node_modules/gulp npm ERR! code EACCES npm ERR! errno -13 npm ERR! Error: EACCES, mkdir '/usr/local/lib/node_modules/gulp' npm ERR! at Error (native) npm ERR! { [Error: EACCES, mkdir '/usr/local/lib/node_modules/gulp'] npm ERR! errno: -13, npm ERR! code: 'EACCES', npm ERR! path: '/usr/local/lib/node_modules/gulp', npm ERR! fstream_type: 'Directory', npm ERR! fstream_path: '/usr/local/lib/node_modules/gulp', npm ERR! fstream_class: 'DirWriter', npm ERR! fstream_stack: npm ERR! [ '/usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23', npm ERR! '/usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:46:53', npm ERR! 'FSReqWrap.oncomplete (fs.js:95:15)' ] } npm ERR! npm ERR! Please try running this command again as root/Administrator. npm ERR! Please include the following file with any support request: npm ERR! /home/cabox/npm-debug.log
可能的解决方案是修改npm将安装的目标目录的ownershipi:
$ npm config get prefix /usr/local $ whoami cabox
上面的命令可以查到你是以cabox用户来运行命令的,npm将全局package安装package到/usr/local下面的lib/node_modules目录下面,比如gulp,bower,grunt等需要全局安装的node module都将存放到这里,而如果你对该目录没有写的权限,则会出现问题,因此你可以做的是chown -R /usr/local your_username
但是这个方案也是有缺点的,特别是当一个系统中有多个用户使用时,你把这些公共目录都搞成你自己的ownership,可能会存在问题。
另外一种可能的解决方案修改上述prefix,指定npm的全局package安装目录为自己的Home目录下面的子目录,同时需要将上述子目录放到path中去,这样就能够将npm的全局package安装到这个我们有权限控制的目录中了
$ npm config set prefix /home/cabox/npm-global/ $ npm config get prefix /home/cabox/npm-global $ gulp -bash: gulp: command not found $ npm install -g gulp /home/cabox/npm-global/bin/gulp -> /home/cabox/npm-global/lib/node_modules/gulp/bin/gulp.js gulp@3.9.0 /home/cabox/npm-global/lib/node_modules/gulp ├── pretty-hrtime@1.0.0 ├── interpret@0.6.2 ├── deprecated@0.0.1 ├── archy@1.0.0 ├── minimist@1.1.1 ├── tildify@1.0.0 (user-home@1.1.1) ├── v8flags@2.0.5 (user-home@1.1.1) ├── chalk@1.0.0 (escape-string-regexp@1.0.3, ansi-styles@2.0.1, supports-color@1.3.1, strip-ansi@2.0.1, has-ansi@1.0.3) ├── semver@4.3.6 ├── orchestrator@0.3.7 (stream-consume@0.1.0, sequencify@0.0.7, end-of-stream@0.1.5) ├── liftoff@2.1.0 (extend@2.0.1, rechoir@0.6.1, flagged-respawn@0.3.1, resolve@1.1.6, findup-sync@0.2.1) ├── gulp-util@3.0.5 (array-differ@1.0.0, array-uniq@1.0.2, lodash._reescape@3.0.0, lodash._reevaluate@3.0.0, beeper@1.1.0, lodash._reinterpolate@3.0.0, object-assign@2.1.1 , replace-ext@0.0.1, vinyl@0.4.6, lodash.template@3.6.1, through2@0.6.5, multipipe@0.1.2, dateformat@1.0.11) └── vinyl-fs@0.3.13 (graceful-fs@3.0.8, strip-bom@1.0.0, defaults@1.0.2, vinyl@0.4.6, mkdirp@0.5.1, through2@0.6.5, glob-stream@3.1.18, glob-watcher@0.0.6)
上面使用cabox用户安装gulp到/home/cabox/npm-global目录中去了。
详细参照下面的两篇文章:
http://www.johnpapa.net/how-to-use-npm-global-without-sudo-on-osx/
https://github.com/sindresorhus/guides/blob/master/npm-global-without-sudo.md
但是现在还有一个问题,那就是系统path中并没有包含该目录,因此直接运行gulp还是无法找到的
在.bash_profile中添加以下:
npm install -g npm@latest或npm install npm -g npm cache clear && rm -rf node_modules && npm install
[cabox@box-codeanywhere npmtest]$ npm install --save lodash npm WARN package.json kidsit@1.0.0 No repository field. npm WARN package.json kidsit@1.0.0 No README data lodash@3.9.3 node_modules/lodash [cabox@box-codeanywhere npmtest]$ ls node_modules package.json [cabox@box-codeanywhere npmtest]$ vi package.json [cabox@box-codeanywhere npmtest]$ ls node_modules package.json [cabox@box-codeanywhere npmtest]$ npm ls kidsit@1.0.0 /home/cabox/workspace/npmtest └── lodash@3.9.3 [cabox@box-codeanywhere npmtest]$ npm uninstall lodash --save unbuild lodash@3.9.3
在这里如果node系统的模块安装有问题,这里会主动列出来:
├── [email protected]
├─┬ [email protected]
│ ├─┬ [email protected]
│ │ └─┬ [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ └── [email protected]
│ ├─┬ [email protected]
│ │ └─┬ [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ └── [email protected]
│ └── [email protected]
├── [email protected]
└── [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: content-type@~1.0.1, required by [email protected]
npm ERR! missing: depd@~1.0.1, required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: on-finished@~2.2.1, required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: raw-body@~2.0.1, required by [email protected]
npm ERR! missing: type-is@~1.6.2, required by [email protected]
[cabox@box-codeanywhere npmtest]$ npm install lodash npm WARN package.json kidsit@1.0.0 No repository field. npm WARN package.json kidsit@1.0.0 No README data lodash@3.9.3 node_modules/lodash [cabox@box-codeanywhere npmtest]$ ls node_modules/ lodash [cabox@box-codeanywhere npmtest]$ npm ls kidsit@1.0.0 /home/cabox/workspace/npmtest └── lodash@3.9.3 extraneous npm ERR! extraneous: lodash@3.9.3 /home/cabox/workspace/npmtest/node_modules/lodash [cabox@box-codeanywhere npmtest]$ npm prune npm WARN package.json kidsit@1.0.0 No repository field. npm WARN package.json kidsit@1.0.0 No README data unbuild lodash@3.9.3 [cabox@box-codeanywhere npmtest]$ npm ls kidsit@1.0.0 /home/cabox/workspace/npmtest └── (empty)
[cabox@box-codeanywhere npmtest]$ npm ls -g --depth=0 /home/cabox/npm-global/lib ├── gulp@3.9.0 └── npm@2.11.1 [cabox@box-codeanywhere npmtest]$ npm uninstall -g gulp unbuild gulp@3.9.0 [cabox@box-codeanywhere npmtest]$ npm ls -g --depth=0 /home/cabox/npm-global/lib └── npm@2.11.1
[cabox@box-codeanywhere npmtest]$ npm adduser Username: kidsit Password: Email: (this IS public) 1372921435@qq.com [cabox@box-codeanywhere npmtest]$ npm config ls ; cli configs user-agent = "npm/2.11.1 node/v0.10.28 linux x64" ; userconfig /home/cabox/.npmrc prefix = "/home/cabox/npm-global" ; node bin location = /usr/bin/node ; cwd = /home/cabox/workspace/npmtest ; HOME = /home/cabox ; 'npm config ls -l' to show all defaults. [cabox@box-codeanywhere npmtest]$ npm publish + kidsit@0.0.1 [cabox@box-codeanywhere npmtest]$
http://blog.izs.me/post/1675072029/10-cool-things-you-probably-didnt-realize-npm
var utils = require( "utils" ); 上面的require命令将导致node按照以下顺序查找文件,注意不一定是index.js,这个由package.json中的main定义决定的 ./node_modules/utils.js ./node_modules/utils/index.js ./node_modules/utils/package.json
具体参考http://www.bennadel.com/blog/2169-where-does-node-js-and-require-look-for-modules.htm
info unbuild /home/nicholas/share/node_modules/yuitest verbose link symlinking /usr/local/lib/node_modules/yuitest to /home/nicholas/share/node_modules/yuitest ERR! Error: EPROTO, Protocol error '/usr/local/lib/node_modules/yuitest'
Error: EPERM, operation not permitted 'C:\...\noam\node_modules\phantomjs\tmp\phantomjs-1.7.0-windows' at Object.fs.renameSync (fs.js:439:18)
npm config set msvs_version 2012 --global
有时我们希望看清楚一个模块到底是因为谁被安装进来的,也就是说它是哪个package的依赖,你可以使用npm ll命令:
比如下面的命令npm -- gulp-less命令你就可以看出,原来gulp-less这个模块在laravel-elixir模块中包含而安装的,也就是说laravel-elixir已经包含了less预处理功能!
$ npm ll gulp-less laravel-elixir@5.0.0 │ C:\Users\Administrator\devenvironment\Code\newkidsitfromscratch\node_modules\laravel-elixir │ Laravel Elixir Core │ git+https://github.com/laravel/elixir.git │ https://github.com/laravel/elixir └── gulp-less@3.1.0 Less for Gulp git://github.com/plus3network/gulp-less.git https://github.com/plus3network/gulp-less#readme
https://github.com/substack/stream-handbook
参考这篇 https://github.com/felixrieseberg/npm-windows-upgrade 文章,写的非常详细,强烈建议升级到npm3,一些怪异的npm模块安装问题都会没有了!
fsevents: AttributeError: 'MSVSProject' object has no attribute 'iteritems'
上述錯誤的原因是windows机器上没有安装build tooling依赖,解决办法是:
npm install --global --production windows-build-tools
参考: https://github.com/nodejs/node-gyp
version=1 npm run build -- homepage
上面我们的build脚本就可以得到version和homepage这个参数
https://jurosh.com/blog/npm-pass-parameters-into-script
https://github.com/Silviu-Marian/node-codein