各位兄台,最近刚学 node.js 使用 npm install
local 方式安装 遇到软件包重复安装问题:
~/code/nodejs/
~/code/nodejs/test
gentoo ~/code/nodejs $ npm install express --save
express@4.12.3 node_modules/express
├── merge-descriptors@1.0.0
├── utils-merge@1.0.0
├── cookie-signature@1.0.6
├── methods@1.1.1
├── cookie@0.1.2
├── fresh@0.2.4
├── escape-html@1.0.1
├── range-parser@1.0.2
├── content-type@1.0.1
├── finalhandler@0.3.4
├── vary@1.0.0
├── parseurl@1.3.0
├── serve-static@1.9.2
├── content-disposition@0.5.0
├── path-to-regexp@0.1.3
├── depd@1.0.0
├── qs@2.4.1
├── on-finished@2.2.0 (ee-first@1.1.0)
├── debug@2.1.3 (ms@0.7.0)
├── etag@1.5.1 (crc@3.2.1)
├── send@0.12.2 (destroy@1.0.3, ms@0.7.0, mime@1.3.4)
├── type-is@1.6.1 (media-typer@0.3.0, mime-types@2.0.10)
├── accepts@1.2.5 (negotiator@0.5.1, mime-types@2.0.10)
└── proxy-addr@1.0.7 (forwarded@0.1.0, ipaddr.js@0.1.9)
express-generator
测试 express-generator
的 子目录 : ~/code/nodejs/test
gentoo ~/code/nodejs $ mkdir test && cd test
gentoo ~/code/nodejs/test $ npm install express-generator --save
npm WARN prefer global express-generator@4.12.1 should be installed with -g
express-generator@4.12.1 ../node_modules/express-generator
├── sorted-object@1.0.0
├── commander@2.6.0
└── mkdirp@0.5.0 (minimist@0.0.8)
express-generator
是安装在 nodejs 开发 主目录 下的 ~/code/nodejs/node_modules
目录:
gentoo /home/i/code/nodejs
$ tree -L 2 ~/code/nodejs/node_modules/
/home/i/code/nodejs/node_modules/
├── express
│ ├── History.md
│ ├── index.js
│ ├── lib
│ ├── LICENSE
│ ├── node_modules
│ ├── package.json
│ └── Readme.md
└── express-generator
├── bin
├── LICENSE
├── node_modules
├── package.json
├── README.md
└── templates
但发现在 主目录 和 子目录 下的 node_modules
都有安装 express
主目录 下的 express 目录树,比 子目录 多安装了 debug
包:
gentoo /home/i/code/nodejs
$ tree -L 2 node_modules/express
node_modules/express
├── History.md
├── index.js
├── lib
│ ├── application.js
│ ├── express.js
│ ├── middleware
│ ├── request.js
│ ├── response.js
│ ├── router
│ ├── utils.js
│ └── view.js
├── LICENSE
├── node_modules
│ ├── accepts
│ ├── content-disposition
│ ├── content-type
│ ├── cookie
│ ├── cookie-signature
│ ├── debug <--
│ ├── depd
│ ├── escape-html
│ ├── etag
│ ├── finalhandler
│ ├── fresh
│ ├── merge-descriptors
│ ├── methods
│ ├── on-finished
│ ├── parseurl
│ ├── path-to-regexp
│ ├── proxy-addr
│ ├── qs
│ ├── range-parser
│ ├── send
│ ├── serve-static
│ ├── type-is
│ ├── utils-merge
│ └── vary
├── package.json
└── Readme.md
28 directories, 11 files
gentoo /home/i/code/nodejs/test
$ tree -L 2 node_modules/express/
node_modules/express/
├── History.md
├── index.js
├── lib
│ ├── application.js
│ ├── express.js
│ ├── middleware
│ ├── request.js
│ ├── response.js
│ ├── router
│ ├── utils.js
│ └── view.js
├── LICENSE
├── node_modules
│ ├── accepts
│ ├── content-disposition
│ ├── content-type
│ ├── cookie
│ ├── cookie-signature
│ ├── depd
│ ├── escape-html
│ ├── etag
│ ├── finalhandler
│ ├── fresh
│ ├── merge-descriptors
│ ├── methods
│ ├── on-finished
│ ├── parseurl
│ ├── path-to-regexp
│ ├── proxy-addr
│ ├── qs
│ ├── range-parser
│ ├── send
│ ├── serve-static
│ ├── type-is
│ ├── utils-merge
│ └── vary
├── package.json
└── Readme.md
27 directories, 11 files
为什么 子目录 不能复用 主目录 的 express 还要 重复安装 呢?
黄舟2017-04-17 11:39:53
Two questions
1. Regarding express-generator
, it is an express project generator of yeoman, as shown in the error reminder npm WARN prefer global express-generator@4.12.1 should be installed with -g
. To use it, in addition to npm install -g yo grunt-cli bower
installing the yeoman three-piece set globally, all generated generator (package ending with generator), you must also install npm install -g express-generator
globally, and then execute yo express
in the directory where you want to create an express project to create a seed project for the opportunity generator. The two express packages you see, one is native to express, and the other is a generator
2. Regarding why nodejs packages cannot be reused.
The modularization of nodejs uses the commonjs specification. When require, it will search for the module under node_modules in the current directory by default. If not, it will search for the node_modules in the parent directory until the root directory. If it does not exist, all projects will be installed in the node directory. Used. What needs to be pointed out here is that the global installation of -g is not globally available. It just generates an executable program placed under the path, such as npm install -g bower
, and then you can call the bower program on the command line, such as Any directory bower install jquery
Read more basic things and have a good understanding of node’s module mechanism. I hope my answer will be helpful to you