node.js, question

When I installed cheerio, the system reported the following error.

taolinweideMacBook-Pro:http Vagor$ npm install cheerio
 └── cheerio@0.19.0
 npm WARN ENOENT ENOENT: no such file or directory, open '/Users/Vagor/package.json'
 npm WARN EPACKAGEJSON /Users/Vagor No description
 npm WARN EPACKAGEJSON /Users/Vagor No repository field.
 npm WARN EPACKAGEJSON /Users/Vagor No README data
 npm WARN EPACKAGEJSON /Users/Vagor No license field.
 taolinweideMacBook-Pro:http Vagor$

I would like to ask you a few questions:

  1. How to solve the above problems

  2. Can you explain to me the principle of in the meantime

I found information aboutpackage.jsonThe content of the attachedLinkFor your reference


We already know that the basic unit of JS module is a single JS file, but more complex modules are often composed of multiple sub-modules. In order to facilitate management and use, we can call a large module composed of multiple sub-modules packages and put all sub-modules in the same directory inside.

In all sub-modules that make up a package, there needs to be an entrance module, and the export object of the entrance module is taken as the export object of the package. For example, there is the following directory structure.


The cat directory defines a package that contains 3 sub-modules. Main.js is the entry module, and its contents are as follows:

var head = require('./head');
 var body = require('./body');
 exports.create = function (name) {
 return {
 name: name,
 head: head.create(),
 body: body.create()
 };     };

When other modules inside use packages, they need to load the package’s entry module. Next to the example above, userequire('/home/user/lib/cat/main')The goal can be achieved, but it seems not a good idea for the name of the entry module to appear in the path inside. So we need to do some extra work to make the package more like a single module.


When the file name of the module is index.js, the path of the directory where the module is located can be used instead of the Module file path when loading the module, so following the example above, the following two statements are equivalent.

var cat = require('/home/user/lib/cat');
 var cat = require('/home/user/lib/cat/index');

After such processing, only the path of the package directory needs to be passed to the require function, which makes the whole directory feel more integrated as a single module.


If you want to customize the file name and storage location of the portal module, you need to include a package.json file in the package directory and specify the path of the portal module. The cat module in the above example can be reconstructed as follows.


Json in which package.json is as follows.

"name": "cat",
 "main": "./lib/main.js"

In this way, modules can also be loaded using the required (‘/home/user/lib/cat’). NodeJS will find the location of the entry module according to the package.json in the package directory.

You can use it first.npm initJson file, and then proceednpm i cheerio --save-devTo install the module


npm install -g npm-autoinit
 npm config set onload-script npm-autoinit/autoinit

Similar problems