Bring you in-depth understanding of NPM - NPM Beginner's Guide

Some time ago, we invited our "city" (grape City) Liu Tao, senior development engineer for everyone to share a full dry goods on the open class Electron line , there are many students in the curriculum process for some of the concepts and usage of NPM doubt, so this time we want you to answer questions students through this article. Based on the introduction of the next addition, we will introduce an appropriate depth, how to publish on their own first npm package. So, let's get started!

Thanks to the modular Node.js ecosystem, we believe NPM is basically the foundation of any part of the project consisting of Node. But in fact, we can even say that NPM is one of the most important tools in the developer community Node.js developers. After all, they use it every day to manage their projects using the package.

However, in addition to the installation package to use it, if we really understand it too NPM? So I will try to cover the basics of the tool, allowing you to better understand and use it, rather than just understanding npm install it.

 

Package Management

We all know that you can use NPM installation package, but what does it mean? Package basically contains the code you need a folder, you can install it locally or globally.

Local installation

Local installation means that you will actually download the files to the project folder. In which you'll find you have not created a directory called "node_modules". Because of this simple mechanism, the local folder may become very large.

So exactly how much this folder? A diagram to describe for you:

 

 

 

In fact, this is just a joke, you can usually ignore this folder, so that Node.js handle it for you.

To perform a local installation, you have to do:

$ npm install [package-name]

You can add —savea suffix, will save Node package name and version of your package.json file. Remember, this is important ( even critical ), because when you're part of a team at work, you will not distribute, nor will node_modules folder is added to the version control system (either GIT, SVN or any version management system you are using), and simply share the package.jsonfile, and then let your teammates execute $npm installcommand to complete the installation and update packages. This folder is faster than the entire file sharing, easier to maintain, entire folders can grow to contain gigabytes of data.

The following is a package.jsonconfiguration file:

{

 

  "name": "Project name",

 

  "version": "1.0.0",

 

  "description": "This is a basic description",

 

  "main": "index.js",

 

  "scripts": {

 

    "test": "echo \"Error: no test specified\" && exit 1"

 

  },

 

  "author": "Fernando Doglio",

 

  "license": "ISC",

 

  "dependencies": {

 

    "config": "^3.1.0",

 

    "express": "^4.17.1",

 

    "socket.io": "^2.2.0",

 

    "winston": "^3.2.1"

 

  }

 }

 

 

You may change some, depending on the package you have installed, or which fields of the file you need (the above example is not used in many other fields).

Global Installation

You can also install a global package, which means that Node.js will be able to access them from any of the items you may need. What's wrong with doing this? Global Installed packages will not be added to the package.jsonfile, since it will not add to package.json, then why should install a global install it?

One a great reason to use Node.js and NPM can do is to build people commonly known as "binary" things, they just can global script installed, it can be accessed from anywhere in the box. This means you can create a command-line tool and use the NPM to install them!

For example: ExpressJS (one of the most popular Web framework Node.js) or Mocha (very popular test library) also comes with a software package such as executable binary files can be used. For example, mocha requires that you install it in the global and local, in order to have a CLI tool called "mocha" and be able to run tests on a local project.

Global package create symbolic links (or shortcuts) you need to add to the PATH environment variable conventional path.

NPM classic command

installCommand just one of many commands you can use with the NPM. In fact, aside nearly 60 different commands (yes, so much!), I will briefly describe, NPM also allows you to create your own custom commands to prevent the built-in command to you is not enough.

The following is a list of the most common commands, taken from official documentation :

  • Access : Set published access level package, restrict or enable access to other people other than the author. example:$ npm access public
  • adduser : add user accounts to the registry (by default, the registry is the npm registry, but you can specify a custom account). Example: $ npm addUserWhen prompted, enter user credentials (username and password) and email.
  • Audit : Security Audit to run dependencies installed, make sure that no known vulnerabilities affect them (and by extension, your project). You can even use this flag fixautomatically fix any problems that may be found during the audit.
  • bin : Displays the current project NPM bin folder.
  • bugs : Open the error list new browser window. Command interesting about this is that it tries to guess the current bug tracker package, once you have found it, it will launch a new browser window.
  • Cache : While the developers do not usually use this command, but it allows them to remove, or add content to verify NPM cache. In the buffer, the stored HTTP request information and additional data packets. Usually this is handled directly by NPM and transparently work for developers, but if you see some strange behavior, especially when switching between different packages and their different versions, try clearing the cache may be a good idea ( just to open) safe side).
  • CI : almost npm installfor automation environment (such as continuous integration process) the same. This command is more stringent than this command, installand to ensure that the installation is always clean (if there is, it will automatically delete node_modules folder).
  • Completion : Enable Tab Completion is npm its sub-commands. Read the full documentation for more details.
  • config : Allows you to set, edit NPM access and configuration options.
  • as dedupe : try to repeat by traversing the dependency tree and the hierarchy as far as possible duplicate entries in moving to reduce dependencies. When your application starts to grow and include more and more of the module, which is particularly useful. Use this command is absolutely optional, but if you have a lot of dependencies, it will provide a considerable reduction in the period (most useful CI / CD environment) installed.
  • deprecate : Add deprecated warning for a specific version (or range) in the registry repository.
  • Tag-dist : to help manage specific package label. Label version can serve as an alias to help identify the version without having to remember numbers. For example, by default, the latest mark for the final versions of all libraries, you just run npm install library-name@latest, NPM will know which version to download library.
  • docs : Like bug , this one command trying to guess where official documents in the package, and open the URL in the local browser.
  • Doctor : perform a set of predefined checks to ensure that the system is performing NPM is ready minimum requirements: nodeand gitcommands are accessible and executable node_modulesfolder (local and global) by the NPM, its registry write or any custom versions are accessible, finally, NPM cache exists and it is working.
  • Search-Help / Help :  Help will be displayed to the document page set of terms, if the result is not found, Help-Search will NPM's markdown help file and displays full-text search results list.
  • Hook : Allows you to configure a new NPM hook , when packets are interested in making changes, it will notify the custom URL. For example, you can receive notifications when a new version ExpressJS by typing the following: Conversely, you can use this information to perform whatever you like operations (such as automatic update dependencies). $npm hook add express http://your-url.com/new-express-version-endpoint
  • the init : to help initialize the project by asking a series of questions, such as name, version, author, etc. Finally, package.json use this information to create a new file. You can also provide custom initialization procedure to customize treatment to your specific stack.
  • install : Install the new package. You can specify the location and format packages (that is, you can only provide a name to look it up in the main registry, or the path tarball file in the download package to be installed in). If you do not want to install the latest version every time you run this command, you can also specify the version to be installed (especially useful for automatic environment (such as CI / CD)).
  • LS : Lists the current project of all installed packages. You can list the global package or locally installed packages. In either case, it will not only list the package.jsonfiles visible name and version, also lists their dependencies and their versions.
  • outdated : Check the project obsolete packages. It will provide a report for your installed packages, its current version, package.jsonthe latest version of the desired file version and master registry published.
  • owner : allows you to manage the package owner. If you are the owner or maintainer library, which is very important, but if you are limited to using the package, is not the case.
  • the ping :  the ping of the current configuration of the main npm registry and test authentication. It is only when you have problems downloading or installing any software packages. It will only help you solve part of the problem, but it is important to remember it.
  • prefix : Displays the current prefix words, the display contains the most recent folder package.jsonpath to the file. You can use this -gflag, you will get the actual location of the installation of the global package.
  • publish : allows developers using the group and the public or private organizations sharing module with others.

These are the most common or the most useful NPM commands you can use, but there are still more than 10 additional commands for you to view, so I recommend that you add a document to a bookmark, and make a note to go back and double check it!

 

How do I publish my own bag

The last point I want to share to share your work with others about how easy NPM knowledge Yes. On a list, the last command is issued command, which basically allows you to do this, but here I want to give you more details.

Metadata preparation of project

Registry is the essence of a great package NPM search engine can simultaneously host all content, so you do not index every bit of metadata that can get in your work at the same time to help other people find your module as soon as possible.

In other words, make sure your package.jsonsettings are correct. As these are interesting to you (and others) began to study and share the main factors in your package.

  • The Name : This is a list of the most obvious and most common, is that you create a package.jsonfile name may have been set up to keep track of the time dependencies. Note that it's unique.
  • The Description : Describe your bag, so that others can quickly understand what they will get when you install. And be sure to add as many important keywords in the description, so that search engines know how to quickly find you. This is an attempt to find the demand and engine developer your first attempt to package the correct balance between the needs of the index.
  • Tags : This is a comma-separated list of keywords. That being said, once you start publishing package, these tags is very important, because in the NPM's main site, they can be used as categories you can easily navigate. Therefore, if you do not improve your package.jsontag properties, other developers will be difficult to find your bag by category navigation.
  • Private : Unless you just for personal use, or you need to set the property as soon as possible false, otherwise no one can find you through a keyword search module.
  • Bugs : This ensures that if you host your content in one place, for example, there Github public issue tracking, then set this property to the correct URL. This will help NPM displays links and displays the number of currently open problem on the package page.
  • Repository : not strictly required attributes, but if you add it, NPM will be able to display additional information, such as its links to events, a list of collaborators, to name a few.
  • Homepage : Like the previous one, it will help NPM show a link to this URL alone (if present). When you put the code in a URL (such as Github repository) and another dedicated to the URL of a specific website modules This is particularly important.
  • License : used to display the actual license you have set in the project. If you add it as package.jsonpart of the file, it will be displayed in a different and more prominent way. You can also mention it on readme.md, but in this case it will be added to provide additional information about your items for the NPM.

By providing metadata I mentioned above, NPM able to display the data and highlight it for developers to see. The following example is Winston package page:

 

 

 

Because of its team added the meta data, add the number of additional links and details and location.

A good documentation

This step is optional, but if your package is a module available for other developers to use, then I suggest you'd better provide a good documentation for others to reference.

Because you really can not count on your tools, "started simple, easy to understand." NPM

The purpose is to provide a set of presets to help them solve the problem of tools for others. Therefore, try to enrich your document, to attract more people to use your tool bar.

The official release package

After the coding is completed, will effectively package.jsonplaced in your directory and write the best possible readme.mdfile, you can publish up.

To perform publishing operations, you must do two things:

  1. Use npm CLI NPM log in to your account (if you already have an account).
  2. Post your code.

That is just two steps, you can simply complete the publication. Online:

$ npm login

You will be prompted for credentials, once you have successfully logged in, you can enter:
$ npm publish

Do this in a folder of the project, otherwise the second command will fail.

Also, keep in mind that the package name will be your package.json file name given property, not from the name of the folder (usually both are the same, but that does not mean They have to do ). Therefore, if you experience duplicate name error (taking into account the NPM already has the same name as the package), then you have to change it up a publish again.

in conclusion

Thanks for reading, I hope that until now, you have to understand the complexity and beauty of NPM. It is not just a simple tool package is installed, but if you take the time to check the document, you can use it to do more things.

Guess you like

Origin www.cnblogs.com/powertoolsteam/p/11504262.html
NPM