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
From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

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 Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software