DWQA QuestionsCategory: ProgramLocal mode NPM install installs express repeatedly in subdirectory
lvii asked 3 weeks ago

Brothers and sisters, I just learned how to use node.js recently.npm install Local mode installationDuplicate package installation encountered:
Directory structure:

  1. nodejs home directory~/code/nodejs/
  2. nodejs Sub item~/code/nodejs/test

home directoryInstall Express

gentoo ~/code/nodejs $ npm install express --save
[email protected] node_modules/express
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── content-t[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])
├── [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])

Subdirectoryinstallexpress-generator

testexpress-generatorOfSubdirectory~/code/nodejs/test

gentoo ~/code/nodejs $ mkdir test && cd test

gentoo ~/code/nodejs/test $ npm install express-generator --save
npm WARN prefer global [email protected] should be installed with -g
[email protected] ../node_modules/express-generator
├── [email protected]
├── [email protected]
└── [email protected] ([email protected])

express-generatorIs installed in nodejs developmenthome directoryLower~/code/nodejs/node_modulesCatalog:

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

But found inhome directoryandSubdirectoryLowernode_modulesThere are installations.express

home directoryExpress tree for:
home directoryUnder the express tree, compareSubdirectoryMore installeddebugBag:

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

SubdirectoryExpress tree for:

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

WHY
Why?SubdirectoryCan not be reusedhome directoryAnd expressRepeat installationWhat about it?

Evian replied 3 weeks ago

I think it’s because NPM people are so stupid. Similarly, PIP does not cache package files and git repositories.I have a spectacular screenshot of NPM installation here:http://lilydjwg.is-programmer.com/2015/4/7/crazy_npm.86849.htmlI remember my colleagues had a problem with too long a path when they installed nodejs on the MAC 233

Evian replied 3 weeks ago

In fact, this is the most powerful part of NPM. It becomes the first and only package management system to solve the problem of dependency conflict by repeatedly installing subdirectories and the class free and commonjs file scopes of JS / / try the NPM DeDupe command to remove some duplicates under the premise of compatibility.

Evian replied 3 weeks ago

NPM (and virtualenv + PIP) and I really have a deep hatred. Every time NPM install takes tens of minutes or even tens of minutes, Linux is not willing to package nodejs library. Recently, it was found that this cargo would toss the server half to death every time it ran on the server.

2 Answers
Best Answer
Yu an answered 3 weeks ago

Two questions
1, aboutexpress-generator, which is an express project generator of yeoman, as in the error alertnpm WARN prefer global [email protected] should be installed with -g, to use it, in addition to
npm install -g yo grunt-cli bowerAll generators (packages ending in generator) must also be installed globally, except for yeoman three-piece set.npm install -g express-generator, and then execute under the project you want to create Expressyo expressTo create a seed project for an opportunity generator. The two express packages you see, one is the native of express, and the other is the generator.
2. Why can’t nodejs packages be reused?
Nodejs modularization uses the commonjs specification. When you require, by default, you will search for modules under the node modules in the current directory. If not, you will search for modules under the node modules in the parent directory until the root directory. If you do not install in the node directory, all projects will be available. It needs to be pointed out that – G’s global installation is not globally available. It just generates an executable program under path, such asnpm install -g bowerYou can then call the bower program on the command line, such as any directorybower install jquery
Pay more attention to the basic things and get a good understanding of node’s module mechanism. I hope my answer will help you.

huguangju answered 3 weeks ago

I still think the version management of Maven is better than that of NPM.
clipboard.png