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

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

Jun 26, 2017 pm 03:26 PM
delete use release Install renew

## 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 of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is it true that you can be blocked and deleted on WeChat and permanently unable to be added? Is it true that you can be blocked and deleted on WeChat and permanently unable to be added? Apr 08, 2024 am 11:41 AM

1. First of all, it is false to block and delete someone permanently and not add them permanently. If you want to add the other party after you have blocked them and deleted them, you only need the other party's consent. 2. If a user blocks someone, the other party will not be able to send messages to the user, view the user's circle of friends, or make calls with the user. 3. Blocking does not mean deleting the other party from the user's WeChat contact list. 4. If the user deletes the other party from the user's WeChat contact list after blocking them, there is no way to recover after deletion. 5. If the user wants to add the other party as a friend again, the other party needs to agree and add the user again.

How to completely delete TikTok chat history How to completely delete TikTok chat history May 07, 2024 am 11:14 AM

1. Open the Douyin app, click [Message] at the bottom of the interface, and click the chat conversation entry that needs to be deleted. 2. Long press any chat record, click [Multiple Select], and check the chat records you want to delete. 3. Click the [Delete] button in the lower right corner and select [Confirm deletion] in the pop-up window to permanently delete these records.

PHP Practical Tip: Remove the last semicolon in your code PHP Practical Tip: Remove the last semicolon in your code Mar 27, 2024 pm 02:24 PM

Practical PHP Tips: Delete the Last Semicolon in the Code When writing PHP code, you often encounter situations where you need to delete the last semicolon in the code. This may be because copy-pasting introduces extra semicolons, or to optimize code style and structure. In this article, we will introduce some methods to remove the last semicolon in PHP code and provide specific code examples. Method 1: Use the substr function The substr function can return a substring of a specified length from a string. we can

Windows cannot access the specified device, path, or file Windows cannot access the specified device, path, or file Jun 18, 2024 pm 04:49 PM

A friend's computer has such a fault. When opening "This PC" and the C drive file, it will prompt "Explorer.EXE Windows cannot access the specified device, path or file. You may not have the appropriate permissions to access the project." Including folders, files, This computer, Recycle Bin, etc., double-clicking will pop up such a window, and right-clicking to open it is normal. This is caused by a system update. If you also encounter this situation, the editor below will teach you how to solve it. 1. Open the registry editor Win+R and enter regedit, or right-click the start menu to run and enter regedit; 2. Locate the registry "Computer\HKEY_CLASSES_ROOT\PackagedCom\ClassInd"

Detailed steps to install Go language on Win7 computer Detailed steps to install Go language on Win7 computer Mar 27, 2024 pm 02:00 PM

Detailed steps to install Go language on Win7 computer Go (also known as Golang) is an open source programming language developed by Google. It is simple, efficient and has excellent concurrency performance. It is suitable for the development of cloud services, network applications and back-end systems. . Installing the Go language on a Win7 computer allows you to quickly get started with the language and start writing Go programs. The following will introduce in detail the steps to install the Go language on a Win7 computer, and attach specific code examples. Step 1: Download the Go language installation package and visit the Go official website

How to delete WeChat Moments How to delete WeChat Moments Apr 08, 2024 pm 03:25 PM

1. Open the WeChat app, click [Me] in the lower right corner, find and click the [Moments] option. 2. Click [My Moments] in the upper right corner and find the content in the Moments you want to delete on the My Moments interface. 3. Click to enter the details page of this circle of friends, and click the [small trash can] icon to the right of the content release time. 4. Select [OK] in the pop-up window, thus completing the operation of deleting the content in the circle of friends.

How to install Go language under Win7 system? How to install Go language under Win7 system? Mar 27, 2024 pm 01:42 PM

Installing Go language under Win7 system is a relatively simple operation. Just follow the following steps to successfully install it. The following will introduce in detail how to install Go language under Win7 system. Step 1: Download the Go language installation package. First, open the Go language official website (https://golang.org/) and enter the download page. On the download page, select the installation package version compatible with Win7 system to download. Click the Download button and wait for the installation package to download. Step 2: Install Go language

Windows permanently pauses updates, Windows turns off automatic updates Windows permanently pauses updates, Windows turns off automatic updates Jun 18, 2024 pm 07:04 PM

Windows updates may cause some of the following problems: 1. Compatibility issues: Some applications, drivers, or hardware devices may be incompatible with new Windows updates, causing them to not work properly or crash. 2. Performance issues: Sometimes, Windows updates may cause the system to become slower or experience performance degradation. This may be due to new features or improvements requiring more resources to run. 3. System stability issues: Some users reported that after installing Windows updates, the system may experience unexpected crashes or blue screen errors. 4. Data loss: In rare cases, Windows updates may cause data loss or file corruption. This is why before making any important updates, back up your

See all articles