npm 1.0: Global vs Local installation
来源:互联网 发布:优化营商环境心得体会 编辑:程序博客网 时间:2024/04/29 12:17
from: http://blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation/
npm 1.0: Global vs Local installation
npm 1.0 is in release candidate mode. Go get it!
More than anything else, the driving force behind the npm 1.0 rearchitecture was the desire to simplify what a package installation directory structure looks like.
In npm 0.x, there was a command called bundle
that a lot of people liked. bundle
let you install your dependencies locally in your project, but even still, it was basically a hack that never really worked very reliably.
Also, there was that activation/deactivation thing. That’s confusing.
Two paths
In npm 1.0, there are two ways to install things:
- globally —- This drops modules in
{prefix}/lib/node_modules
, and puts executable files in{prefix}/bin
, where{prefix}
is usually something like/usr/local
. It also installs man pages in{prefix}/share/man
, if they’re supplied. - locally —- This installs your package in the current working directory. Node modules go in
./node_modules
, executables go in./node_modules/.bin/
, and man pages aren’t installed at all.
Which to choose
Whether to install a package globally or locally depends on the global
config, which is aliased to the -g
command line switch.
Just like how global variables are kind of gross, but also necessary in some cases, global packages are important, but best avoided if not needed.
In general, the rule of thumb is:
- If you’re installing something that you want to use in your program, using
require('whatever')
, then install it locally, at the root of your project. - If you’re installing something that you want to use in your shell, on the command line or something, install it globally, so that its binaries end up in your
PATH
environment variable.
When you can’t choose
Of course, there are some cases where you want to do both. Coffee-script andExpress both are good examples of apps that have a command line interface, as well as a library. In those cases, you can do one of the following:
- Install it in both places. Seriously, are you that short on disk space? It’s fine, really. They’re tiny JavaScript programs.
- Install it globally, and then
npm link coffee-script
ornpm link express
(if you’re on a platform that supports symbolic links.) Then you only need to update the global copy to update all the symlinks as well.
The first option is the best in my opinion. Simple, clear, explicit. The second is really handy if you are going to re-use the same library in a bunch of different projects. (More on npm link
in a future installment.)
You can probably think of other ways to do it by messing with environment variables. But I don’t recommend those ways. Go with the grain.
Slight exception: It’s not always the cwd.
Let’s say you do something like this:
cd ~/projects/foo # go into my projectnpm install express # ./node_modules/expresscd lib/utils # move around in therevim some-thing.js # edit some stuff, work work worknpm install redis # ./lib/utils/node_modules/redis!? ew.
In this case, npm will install redis
into ~/projects/foo/node_modules/redis
. Sort of like how git will work anywhere within a git repository, npm will work anywhere within a package, defined by having a node_modules
folder.
Test runners and stuff
If your package’s scripts.test
command uses a command-line program installed by one of your dependencies, not to worry. npm makes ./node_modules/.bin
the first entry in the PATH
environment variable when running any lifecycle scripts, so this will work fine, even if your program is not globally installed:
{ "name" : "my-program", "version" : "1.2.3", "dependencies": { "express": "*", "coffee-script": "*" }, "devDependencies": { "vows": "*" }, "scripts": { "test": "vows test/*.js" , "preinstall": "cake build" } }
- npm 1.0: Global vs Local installation
- Python—global vs local variables
- npm installation
- Local & Global
- global and local
- local|global index
- Global and Local Variables
- Local and Global References
- Global 和 Local 索引。
- Global and Local Variables
- Global 和 Local 函数
- PHP 全局变量(Global Scope Variable) vs 局部变量(Local Scope Variable)
- Local and Global References区别
- global variable and local variable
- Global and Local Variables python
- Use Local Or Global Index?
- unity3d中的local和global
- Local Reference and Global Reference
- 数据仓库的基本架构
- PHP框架高级编程——应用Symfony、CakePHP和Zend
- prototype中文参数乱码解决方案
- HEAP-DELETE(A,i)操作将节点i中的项从堆中删去
- [转]ssh无密码登入设置
- npm 1.0: Global vs Local installation
- 设计模式学习笔记——组合模式
- asp.net中url地址传送中文参数时的两种解决方案
- GAE做IP上报
- 嵌入式面试题-经典
- Linux通知链技术(jjyy)
- 痛苦和盐
- 笔记本电脑双机无线互联
- 完备度量空间