Giter Club home page Giter Club logo

smart-npm's Introduction

smart-npm

Greenkeeper badge NPM version GitHub version Build Status Build status Dependency Status Inline docs Coverage Status

智能的 npm,让你在**使用 npm 时,下载速度更快,使用更方便!

jsdoc 生成的文档

github 地址

背景

npm 时,默认它会访问国外资源,所以会非常卡,有时甚至会被墙。现在市面上一般有三种解决方案:

  1. .npmrc 上配置一个国内的 registry 镜像

  2. 使用 cnpm

  3. 使用 VPN

  • 第1个方案很粗暴,可以解决很多下载慢的问题,但是当你用 npm publish 时就会失败

  • 第2个方案不错,但这样你就又会遇到问题,到底哪些命令需要用 cnpm,哪些命令需要用 npm 呢?

  • VPN 方案有时也不能百分百解决问题,有时有些 VPN 也不稳定,但有个 VPN 很保险就是

其实 cnpm 的意图并不是简单给我们用来去下载 npm 资源的,它是为 cnpm 服务端(也可以理解成 npm 的私有仓库)服务的。所以你如果简单的把 cnpm 当作 npm 来用会有出现很多问题(见下)。

所以,我们就需要一个更智能的 npm 了,可以在我们使用 npm install 时自动从国内的镜像下载,而在我们使用 npm publish 又能发布到官方的 registry 上!

就让 smart-npm 来为你完成吧!

安装

npm install --global smart-npm --registry=https://registry.npm.taobao.org/

安装成功后默认会在你的 npm 用户配置文件 ~/.npmrc 中添加淘宝的 registry。

另外建议安装后替换原生的 npm 命令:

  • Linux 用户可以在 ~/.bashrc 文件中加一行
alias npm=smart-npm
  • Mac 用户可以在 ~/.bash_profile 文件中加一行
alias npm=smart-npm
  • Window 用户需要先定位到 npm.cmdsmart-npm.cmd 两个文件,然后用 smart-npm.cmd 替换 npm.cmd(注意备份原来的 npm.cmd),同时注意修改下 smart-npm.cmd 文件里的路径,否则运行 npm 会报找不到文件错误(如果不明白,建议不要替换,直接使用 snpm 或 smart-npm)

可以使用命令 where smart-npm 来定位到 smart-npm.cmd 文件所在的位置。如:在我的系统上执行 where smart-npm 的结果是:

C:\Users\Mora>where smart-npm
C:\Program Files\nodejs\smart-npm
C:\Program Files\nodejs\smart-npm.cmd

同理可以定位到 npm.cmd 的位置

使用指定版本的 npm

smart-npm 使用的是内部的 npm,和系统的 npm 没有任何关系

snpm install-npm          # 安装最新版的 npm
snpm install-npm 4        # 安装 4.x.x 版本的 npm

卸载

npm uninstall --global smart-npm

使用

安装后,你可以使用命令 snpmsmart-npm,如果你按上面的方法替换了原生的 npm,那么你也可以直接使用 npm

  • 新的 snpm 会自动根据你使用的命令切换 registry:当你使用 publish, config, adduser, star 等(click here to see more) 命令时,会强制使用官方的 registry https://registry.npmjs.org;当你使用其它命令时,都会使用淘宝的镜像 https://registry.npm.taobao.org/

    • 如果要强制使用某个 registry 时,只要在命令后面添加 registry 参数即可,比如, snpm install jquery --registry=https://r.cnpmjs.org 就会使用你指定的 registry 去拉取 jquery

    • 如果要强制使用官方的 registry, 只要在命令后面加上 --npm 即可, 比如, snpm install jquery --npm 就会使用官方的 registry 去拉取 jquery,(当镜像没有及时更新时,用此会选项很有效)

    • 如果你想修改默认的淘宝镜像或者官方的 registry,可以在你的环境变量中添加这两个参数: NPM_OFFICIAL_REGISTRYNPM_MIRROR_REGISTRY,以此来修改默认的官方 registry 和 淘宝镜像 registry。 更多环境变量的配置请点击这里

smart-npm 扩展的几个新命令

  • snpm check: 由 npm-check 提供,和 npm outdated 类似,但提示更人性化,同时它也支持自动更新依赖

  • snpm stats {package}: 打开一个网页,可以看到 package 的统计数据,包括被下载的次数、最近更新时间、被依赖的次数及排名等数据

  • snpm user {package}: 打开一个网页,跳到用户的 npm 上的个人主页,如果加了参数 -t,则是跳到淘宝镜像的个人主页

比较有用但很少被用的一些 npm 的命令

  • npm repo {package} : 用浏览器打开 package 的 github 地址(前提是此 package 的 package.json 文件中有设置 repository

  • npm home {package} : 用浏览器打开 package 的首页(前提是此 package 的 package.json 文件中有设置 homepage

  • npm user {package}cnpm的功能,用浏览器打开用户在淘宝镜像的主页,如 https://npm.taobao.org/~{user_name}

  • npm view {package} versions:查看 package 的所有版本号(只会显示版本号,不显示其它信息)

  • npm outdated:检查当前项目所依赖的 packages 是否有最新的版本可以更新

把 cnpm 当作 npm 来用时会出现的问题

  1. publish, adduser, loginClick here to see more) 等命令无法通过 cnpm 来执行,即运行 cnpm publish 来发布一个版本会失败的

  2. 某些命令在 cnpmnpm 上表现完全不一样,比如:

  • cnpm version:显示当前 cnpm 版本号; 而 npm version:是可以修改当前 package 的版本号的
  • cnpm -g root:会报错; 而 npm -g root 会显示 global 安装的目录
  1. 有很多 npm 包都集成了 npm install,比如 yeoman 的所有 generators ,在最后基本都会 调用 npm install,(可以看其源码) 这种情况下使用 cnpm 完全无效,必须中断操作,然后自己手动运行 cnpm install,或者在运行 yo [generator] 时就指定 --skip-install, 这体验就很不爽了

  2. 还有一种情况是,很多和 npm API 相关的 package,都会读取 ~/.npmrc 中的 registry,或者使用默认的 registry —— https://registry.npmjs.org/, 去查询 npm package 相关的信息,比如下面这些:

    • npm-latest: 查询某个 package 的最新版本号
    • npm-name: 查询某个 package name 是否被注册了
    • npm-dependents: 查找某个模块所依赖的其它所有模块
    • ...

如果你用的任何一个包或其所依赖的包中用了这些 package,那么在这些包请求网络时也得慢死了!

调试

调试 npm

--loglevel

  • silent: completely silent. Zero logging output.
  • win: Just the “npm ok” or “npm not ok” message at the end.
  • error: When something unexpected and bad happens.
  • warn: When something odd or potentially dangerous is happening.
  • info: Helpful information so you can track what’s happening.
  • verbose: Even more. Perhaps just a wee bit obnoxious, even.
  • silly: Completely fuckin crazy, man. Dump everything. Whole objects, you name it, whatever.

特定 loglevel 的缩写信息

  • s : ["--loglevel", "silent"]
  • d : ["--loglevel", "info"]
  • dd : ["--loglevel", "verbose"]
  • ddd : ["--loglevel", "silly"]
  • silent : ["--loglevel", "silent"]
  • verbose : ["--loglevel", "verbose"]
  • quiet: ["--loglevel", "warn"]
  • q: ["--loglevel", "warn"]

Todo List

  • 本地安装也会替代了全局的 npm
  • 添加更多有用的组件
  • 修改 stats 命令,把数据显示在命令行上

Reference

Release History

Changelog

License

Copyright (c) 2015 Zhonglei Qiu. Licensed under the MIT license.

smart-npm's People

Contributors

bitdeli-chef avatar greenkeeper[bot] avatar greenkeeperio-bot avatar qiu8310 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

smart-npm's Issues

An in-range update of snyk is breaking the build 🚨

Version 1.35.0 of snyk just got published.

Branch Build failing 🚨
Dependency snyk
Current Version 1.34.4
Type dependency

This version is covered by your current version range and after updating it in your project the build failed.

snyk is a direct dependency of this project this is very likely breaking your project right now. If other packages depend on you it’s very likely also breaking them.
I recommend you give this issue a very high priority. I’m sure you can resolve this 💪

Status Details
  • continuous-integration/travis-ci/push The Travis CI build is in progress Details
  • continuous-integration/appveyor/branch AppVeyor build failed Details

Release Notes v1.35.0

<a name"1.35.0">

1.35.0 (2017-06-20)

Bug Fixes

  • support --dev when running in maven projects (ff6e063e)
  • missing equal sign for the org parameter (387d537b)

Features

  • support SBT package manager (b461b84a)
Commits

The new version differs by 3 commits.

  • ff6e063 fix: support --dev when running in maven projects
  • b461b84 feat: support SBT package manager
  • 387d537 fix: missing equal sign for the org parameter

See the full diff

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

无法安装成功

请看下面的错误:
运行环境是 virtualbox, ubuntu 17.04 32bit, node v8.0.0, npm 5.0.3

edwin@edwin-VirtualBox:~$ sudo npm install --global smart-npm --registry=https://registry.npm.taobao.org/

> [email protected] preinstall /usr/local/lib/node_modules/smart-npm
> npm config set registry https://registry.npm.taobao.org/

npm ERR! path /usr/local/etc/npmrc
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall open
npm ERR! Error: EACCES: permission denied, open '/usr/local/etc/npmrc'
npm ERR!  { Error: EACCES: permission denied, open '/usr/local/etc/npmrc'
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'open',
npm ERR!   path: '/usr/local/etc/npmrc' }
npm ERR! 
npm ERR! Please try running this command again as root/Administrator.

┌─────────────────────────────────────────────────────────┐
│                 npm update check failed                 │
│           Try running with sudo or get access           │
│          to the local update config store via           │
│ sudo chown -R $USER:$(id -gn $USER) /home/edwin/.config │
└─────────────────────────────────────────────────────────┘

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/edwin/.npm/_logs/2017-06-14T12_57_16_538Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 243
npm ERR! [email protected] preinstall: `npm config set registry https://registry.npm.taobao.org/`
npm ERR! Exit status 243
npm ERR! 
npm ERR! Failed at the [email protected] preinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/edwin/.npm/_logs/2017-06-14T12_57_38_699Z-debug.log

读取缓存问题

snpm可以在本地缓存过的情况下,优先读取本地缓存吗?

关于更新了npm之后snpm -v的显示内容的问题

我更新了我的npm

~$ npm -v
5.0.2

不过snpm里的npm还是旧版本的

 ~$ snpm -v
 npm:  4.4.4
 smart-npm:  3.0.1

这样有什么影响么?我现在使用snpm的时候,用的是高版本的npm还是低版本的npm呀?

windows下安装后出错

windows下,生成的快捷方式直接指向的是smart-npm.js,如果windows的js默认不是由node.exe执行的话,那么就会出现安装后npm不能用的问题。

centos6.5安装失败

[root@iZ23oir9az1Z ]# npm install --global smart-npm --registry=https://registry.npm.taobao.org/
npm WARN lifecycle [email protected]
preinstall: cannot run in wd %s %s (wd=%s) [email protected] npm config set registry https://registry.npm.taobao.org/ /root/.nvm/versions/node/v6.0.0/lib/node_modules/.staging/smart-npm-ef71f992
/root/.nvm/versions/node/v6.0.0/bin/smart-npm -> /root/.nvm/versions/node/v6.0.0/lib/node_modules/smart-npm/bin/smart-npm.js

[email protected] postinstall /root/.nvm/versions/node/v6.0.0/lib/node_modules/smart-npm
node scripts/helper.js postinstall

fs.js:1083
return binding.unlink(pathModule._makeLong(path));
^

Error: EACCES: permission denied, unlink '/root/.nvm/versions/node/v6.0.0/bin/npm'
at Error (native)
at Object.fs.unlinkSync (fs.js:1083:18)
at postInstall (/root/.nvm/versions/node/v6.0.0/lib/node_modules/smart-npm/scripts/helper.js:89:34)
at Object. (/root/.nvm/versions/node/v6.0.0/lib/node_modules/smart-npm/scripts/helper.js:136:23)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:456:32)
at tryModuleLoad (module.js:415:12)
at Function.Module._load (module.js:407:3)
at Function.Module.runMain (module.js:575:10)
/root/.nvm/versions/node/v6.0.0/lib
└── (empty)

npm ERR! Linux 2.6.32-431.20.3.el6.x86_64
npm ERR! argv "/root/.nvm/versions/node/v6.0.0/bin/node" "/root/.nvm/versions/node/v6.0.0/bin/npm" "install" "--global" "smart-npm" "--registry=https://registry.npm.taobao.org/"
npm ERR! node v6.0.0
npm ERR! npm v3.8.6
npm ERR! code ELIFECYCLE

npm ERR! [email protected] postinstall: node scripts/helper.js postinstall
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] postinstall script 'node scripts/helper.js postinstall'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the smart-npm package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node scripts/helper.js postinstall
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs smart-npm
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls smart-npm
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR! /root/npm-debug.log
npm ERR! code 1

希望对 npm 命令路径的处理更智能些

z at ZsMBP in ~ 
$ yarn global bin
/usr/local/bin

z at ZsMBP in ~ 
$ snpm
events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: spawn /Users/z/.config/yarn/global/node_modules/.bin/npm ENOENT
    at exports._errnoException (util.js:1012:11)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:182:32)
    at onErrorNT (internal/child_process.js:348:16)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)
    at Module.runMain (module.js:577:11)
    at run (bootstrap_node.js:352:7)
    at startup (bootstrap_node.js:144:9)
    at bootstrap_node.js:467:3

cnpm registry  是不是可以去掉了?

使用snpm以后是不是可以去掉.npmrc中的registry呢
registry=https://registry.npm.taobao.org

以下环境变量是不是也可以去掉了

export NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/dist
export NVM_IOJS_ORG_MIRROR=https://npm.taobao.org/mirrors/iojs

奇怪的是,使用的淘宝的registry,npm search一用就报错,真不知道是为何。
正是由于这问题,才google到snpm。

windows下 snpm install-npm报错

>snpm install-npm
events.js:183
      throw er; // Unhandled 'error' event
      ^

Error: spawn npm ENOENT
    at _errnoException (util.js:992:11)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:190:19)
    at onErrorNT (internal/child_process.js:372:16)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)
    at Function.Module.runMain (module.js:695:11)
    at startup (bootstrap_node.js:191:16)
    at bootstrap_node.js:612:3

Centos 7 安装失败

npm ERR! Error: EACCES: permission denied, open '/usr/local/etc/npmrc'
npm ERR! at Error (native)
npm ERR! { [Error: EACCES: permission denied, open '/usr/local/etc/npmrc']
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'open',
npm ERR! path: '/usr/local/etc/npmrc' }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.

然,/usr/local/etc/下并没有npmrc文件。

OS:Centos 7.0
User:root
node version:v4.5.0
npm version:2.15.9

we must check into the path where npm locate

npm install express --save
...
npm WARN saveError ENOENT: no such file or directory, open 'd:\node\package.json'

we must check into the path where npm locate
cd node_modules\npm

其实我觉得npm慢是npm程序本身的问题

之前我把npm 的源换成淘宝的 也用了 smart-npm 速度确实要快很多 但是他会不定时的假死 就卡在那一直不动 等多久都不会动 挂vpn 都没用
我用 cnpm 嗖嗖的就装完了 但是cnpm 只能用来 自己install 包 像一些要使用npm的程序 还是要使用 npm 我现在3天了 像 create-react-app 这样的我就没有成功过

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.