search
HomeWeb Front-endJS TutorialShare using npm to install/delete/publish/update/unpublish packages

## 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.
First, get the path to the directory where npm is located through npm config get prefix, for example, like this:
##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
Javascript Data Types : Is there any difference between Browser and NodeJs?Javascript Data Types : Is there any difference between Browser and NodeJs?May 14, 2025 am 12:15 AM

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScript Comments: A Guide to Using // and /* */JavaScript Comments: A Guide to Using // and /* */May 13, 2025 pm 03:49 PM

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools