Home  >  Article  >  Web Front-end  >  Share using npm to install/delete/publish/update/unpublish packages

Share using npm to install/delete/publish/update/unpublish packages

零下一度
零下一度Original
2017-06-26 15:26:271637browse

## 1. What is npm?
npm is a package management tool for javascript, and is an iconic product under front-end modularity
To put it simply, download modules through npm, reuse existing code, and improve work efficiency
1.From the perspective of the community: Publish the module for a specific problem to the npm server for other people in the community to download and use. At the same time, you can also use it in the community Find resources for specific modules and solve problems
2.From a team perspective: With npm, the package management tool, you can reuse the team’s existing code It has also become more convenient
## 2. Use npm to install the package
npm installation methods - local installation and global installation
When to use local/global installation?
1. When you try to install command line tools, such as grunt CLI, use global installation
Global installation method: npm install -g module name
2. When you try to install a module through npm and introduce it through require('XXX'), Use local installation
Local installation method: npm install module name
Problems you are likely to encounter
When you try to install locally, you will usually encounter the permission deny problem
For example, this is my first attempt to install express globally. Enter npm install -g express

[Tucao] What’s more, what makes you speechless is that after installing many dependencies, you are reminded that you have insufficient permissions...
Solution:
##1.
sudo npm install -g XXX, install as administratorEvaluation:
You have to enter your account and password every time, which is very cumbersome and not officially recommended
(You could also try using sudo, but this should be avoided)
2.
sudo chown -R The path to the directory where your account name npm is located/{lib/node_modules,bin,share}Evaluation:
Officially recommended practice
, chown stands for change owner, which means specifying the owner of the npm directory as your name (granting permissions), -R means to change all files in the specified directory The same operation is also performed on subdirectories and files.
<1>First, get the path to the directory where npm is located through npm config get prefix, for example, like this:
##<2>Enter sudo chown -R on the command line. The path to the directory where your account name npm is located/{lib/node_modules, bin,share}, for example:
[Note] The braces in {lib/node_modules,bin,share} must be written
Install express globally again: enter npm install -g express

Installation Success
3.sudo chmod 777 npm directory (not recommended)
Comment: This is a solution that can often be seen online, but there is no mention of it in the official tutorial. chmod represents change mode to change the read and write mode. Grant the highest permissions to the directory. Anyone can read and write. This is very dangerous.
When installing locally, write the dependent package information into package.json
Pay attention to a problem. In team collaboration, a common scenario is that others clone your project from github, and then install the necessary dependencies through npm install. ( There is no node_modules just cloned from github. Need to install ) So what information does use to install dependencies? It is the dependencies and devDepencies in your package.json. Therefore, while installing locally, it is important to write the information of the dependent package (required name and version) into package.json!
npm install module: Do not write it into package.json after installation
npm install module --save After installation, write it into the dependencies of package.json (production environment dependencies)
npm install module --save-dev After installation, write it into the devDepencies of package.json (development environment dependency)
Example:
I installed webpack under the project: enter the project terminal and enter npm install
After the installation is completed, my package.json
Uninstall webpack and then reinstall: After entering npm install webpack --save:
Uninstall webpack and then reinstall it. After reinstalling: npm install webpack --save-dev:
##三.Use npm to delete the package
Deleting a module is actually very simple:
Delete the global module
npm uninstall -g Use npm
to delete local modules
npm uninstall module
Questions you should think about when deleting local modules: Will the corresponding dependency information on package.json also be eliminated?
npm uninstall module: Delete the module, but does not delete the corresponding information left by the module in package.json
npm uninstall module --save Delete the module, Also delete the corresponding information left by the module under dependencies in package.json
npm uninstall module --save-dev Delete the module, Also delete the corresponding information left by the module under devDependencies in package.json
4. Use npm to publish the package
Before publishing the package, you must first There is an npm account
Publishing a package for the first time:
Enter npm adduser in the terminal and you will be prompted to enter the account , password and email, and then you will be prompted to successfully create
##Non-first release package:
Enter in the terminal npm login, then enter the account and password you created, and email, log in
[Note] When npm adduser is successful, you will be logged in by default, so there is no need to continue npm login.
Example:
(because I have already created account, so log in directly)
1. Enter the project directory, and then log in:
2. Send the package through npm publish
The name and version of the package are the package in your project The name and version in .json!
3 Then you can find the published APP in npm search!

[Note 1] It cannot have the same name as an existing package!
For example I tried changing the package name to 'react' which obviously already exists:
Then when the package is sent...
(Translation: You do not have permission to publish react packages. Are you logged in as the react owner?)
[Tip] Before sending the package, you can use npm's search engine to check whether a package with the same name already exists
[Note 2] Also One thing to note is npm’s restrictions on package names: no capital letters/spaces/underscores!
(In fact, in the above example, I originally planned to write it as penghuwanAPP , reported an error... changed to penghuwan_app, reported an error again, and finally had to change to penghuwanapp.

##[Note 3] There are some private codes in your project that you don’t want to publish to npm?

Write it into .gitignore or .npmignore, and the upload will be ignored

5. Use npm to unpublish the package
One thing to say here is that unpublishing the package may not be as easy as you think. This operation is subject to many restrictions. Undoing a published package is considered a bad behavior
(Imagine you revoked a published package [assuming it is already in There is a certain degree of influence in the community], what a collapse this is for the teams that have deeply used and relied on the packages you released!)
##Example:
I will now revoke the previously released package penghuwanapp: enter npm unpublish package name
[Tucao] Pay attention to the words in the red box, and you will know npm’s official attitude towards this behavior when it revokes the released package....
[Note] If you report There was an error in permissions, and after adding --force

, I couldn’t find it after searching on npm

1According to the specification, unpublish is only allowed within
24 hours of sending the package. with versions published in the last 24 hours)
##2

Even if

you revoke the published package,
issued the package It can no longer duplicate the name and version of the withdrawn package (that is, it cannot have the same name and the same version, because the unique identifier composed of the two has been "occupied")
For example, after withdrawing the package, I try to publish a package with the same name + the same version: # #Report an error and suggest me to modify the package version
Recommended alternative command to npm unpublish: npm deprecate [@]
Use this The command, will not revoke your existing package in the community, but will get a warning when anyone tries to install the package
For example: npm deprecate penghuwanapp 'I no longer maintain this package~'
6. Package after npm update release:
In fact, the commands for npm update package and publish package are the same, both are npm publish.The difference is that you need to modify the version of the package
So the steps are:
1. Modify the version of the package (version field in package.json)
2.npm publish
For details about the modified version, please see below:
Seven.npm version control - Semantic versioning
There is a version field in our package.json. So, how to adjust the version while the project is constantly being built?
npm has its own version control standard - Semantic versioning
The specific embodiment is:
For "version":"x.y.z"
1. Fix bugs, make minor changes, add z
#2. Add new features, but still be backward compatible, add y
3. There are big changes, which are not backward compatible. Add x
For example: If my original project is version 1.0.0
If it is in 1, it will become 1.0.1
##If it is in 2 In the case of 3, it becomes 1.1.0
If it is the case in 3, it becomes 2.0.0
Automatically change the version through npm version
##update_type is one of patch, minor, or major, which respectively means patch, minor change, Major changes
For example, I change the project version in the shell

Let’s take a look at my package.json again, it has become v1.0.0
【End】

The above is the detailed content of Share using npm to install/delete/publish/update/unpublish packages. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn