preface

The NPM repo mentioned earlier is a quick way to open the Github source page for every NPM package.

There is a portal to view its source code analysis section.

Today we will also cover an NPM command that helps us quickly understand each NPM package, NPM Docs, which also has an alias of NPM Home. As the name suggests, this command looks at the documentation for each NPM package.

The specific implementation

Use the react document as an example. You can enter NPM home React directly on the terminal to open the official document of react. You will find the official link of Reactjs.org/.

Let’s take a look at the code logic of NPM Docs. Again, if you don’t know how NPM starts and registers commands, you can open this portal and see the implementation logic of NPM Docs very clearly.

function docs (args, cb) { if (! args || ! args.length) args = ['.'] var pending = args.length log.silly('docs', args) args.forEach(function (proj) { getDoc(proj, function (err) { if (err) { return cb(err) } --pending || cb() }) })}Copy the code

The NPM docs command can open multiple document pages of NPM packages at the same time.

The core logic is all in the getDoc method

function getDoc (project, cb) { log.silly('getDoc', project) fetchPackageMetadata(project, '.', {fullMetadata: true}, function (er, d) { if (er) return cb(er) var url = d.homepage if (! url) url = 'https://www.npmjs.org/package/' + d.name return openUrl(url, 'docs available at the following URL', cb) })}Copy the code

Here we see fetchPackageMetadata again

We also saw this method in the implementation analysis of the NPM repo. I won’t go over it here, but you can take a look through the portal.

FetchPackageMetadata: fetchPackageMetadata: fetchPackageMetadata: fetchPackageMetadata: fetchPackageMetadata: fetchPackageMetadata: fetchPackageMetadata

In the react manifest file, pacote manifest file –fullMetadata=true

We found that the react package’s source information contains the same homepage link as reactjs.org/. The react command opens the react website.

By the way, when developing NPM packages in the future, you can configure your homepage. If there is no official document of your own, you can also configure the Github page directly.

It is also possible that the NPM package does not have a homepage field configured

url = 'https://www.npmjs.org/package/' + d.name
Copy the code

Open a NPMJS website corresponding package information page, such as www.npmjs.com/package/rea.

conclusion

NPM this package management tool, we must make good use of it, because in our daily front-end development process, are dealing with many NPM package dependence, through the NPM docs command we can quickly check the corresponding NPM package document information, improve the development efficiency, but also can further understand the tools we are using.

Also, don’t forget that NPM Docs is also known as NPM Home.

Ha ha, eagle-eyed students will find another trick is to directly www.npmjs.com/package/xxx…

As a programmer, if you can use terminal commands, you can probably use CLI to solve the problem.