What is the NPM

NPM is the module manager for Node and is extremely powerful. It’s one of the big reasons for Node’s success. Thanks to NPM, we can install modules written by others with a single command.

npm install

Using the NPM install command, you can install modules to the node_modules directory.

$NPM install <packageName> --save-dev -d local --save -s production -g global $NPM uninstall <packageName>Copy the code

Before installing, NPM install checks whether the specified module already exists in the node_modules directory. If it exists, it is not reinstalled, even if a new version of the remote repository is available.

If you want NPM to force a module to be reinstalled regardless of whether it has been installed (nodemodules do not need to be deleted), use the -for –force arguments

$NPM install <packageName> --force // Force updateCopy the code

npm update

If you want to update an installed module, use the NPM update command.

$ npm update <packageName>
Copy the code

It queries the remote repository for the latest version and then the local version. If the local version does not exist, or the remote version is newer, it will be installed.

Module installation process

  1. anpm installThe command
  2. NPM queries Registry for the url of the module zip package
  3. Download the zip package and save it~/.npmdirectory
  4. Unzip the zip package to the current projectnode_modulesdirectory

NPM common directives

NPM init --yes(initial configuration) -y install package dependencies according to the key dependencies in package.json. This package is in the node_modules folder and will be updated in your package.json file3.0. 0NPM I package --save-dev(the package required to install a development environment, -s NPM uninstall package -s NPM uninstall package -s NPM uninstall package -s NPM uninstall package -s NPM uninstall package -s NPM uninstall package Json file) NPM update package (update this package to the latest version, which will be updated in your package.json file) NPM run script key (which will be updated according to package.json"scripts"NPM publish --force NPM unpublish --force NPM unpublish --forceCopy the code

PackageJson file configuration

{
	"name": "axios".// The package name to publish. The default is the name of the parent folder. Must not be the same as the package name in the current NPM. Package names must not have uppercase letters/Spaces/slippage!
  "version": 2.2.2 "".// The default version of your package is 1.0.0. There are a series of rules for the version number of the NPM package. The version number of the module is in the format of X.Y.Z, which is embodied as follows:1. Fix bugs, make small changes and add Z. 2, add new features, backward compatibility, add Y 3, there is a big change, not backward compatibility, add X"description": "To request an interface, you get it, asshole."./ / is introduced
  "main": "index.js".// The entry file, which defaults to index.js, can be modified to its own file. This is very important, when you use it in the actual project, let a = require(" package name "), it will find the corresponding file path.
  "scripts": {                  // Shortcut command. Enter the command NPM run in the package.json directory to execute the corresponding command
    "bulid": "npx webpack --config myConfig.js"."start": "xxx".// For example, enter NPM run bulid to run the NPX webpack --config myconfig.js command.
  	// NPM start omits run
  },
  "keywords": []."author": "Two Eggs of the King"."license": "ISC".// Open source file protocol (MIT)
  "dependencies": {             // The package that the production environment depends on
    "jquery": "^ 3.4.1 track"."sea": "^ 1.0.2"
  },
  "devDependencies": {          // Packages that the development environment depends on
    "webpack": "^ 4.41.6"}}Copy the code

Change the default NPM download path and cache path

The NPM config ls command displays the NPM installation information and the default download path.

D:\software\cmder\cmder>npm config ls
; cli configs
metrics-registry = "https://registry.npmjs.org/"
scope = ""
user-agent = "NPM / 6.13.4 node/v12.14.0 win32 x64"

; builtin config undefined
prefix = "C:\\Users\\DELL\\AppData\\Roaming\\npm"

; node bin location = D:\SoftWareInstall\nodejsIns\node.exe
; cwd = D:\software\cmder\cmder
; HOME = C:\Users\DELL
; "npm config ls -l" to show all defaults.
Copy the code

Note, the default download path of NPM is IN C:\Users\DELL\AppData\Roaming\ NPM, where AppData is a hidden folder, check the “hidden project” in the “View” menu to see the AppData folder.

?????

// Change the default NPM cache path
npm config set cache "D:\TempDate\nodedata\cache"

// Change the default download path for NPM
npm config set prefix "D:\TempDate\nodedata\download"
Copy the code

After changing the cache path and download path, check again

D:\software\cmder\cmder>npm config ls

; userconfig C:\Users\DELL\.npmrc
cache = "D:\\TempDate\\nodedata\\cache"
prefix = "D:\\TempDate\\nodedata\\download"
Copy the code

How to create your first Node module and upload it to NPM so that others can use it

Create the first Node module

Node.js modules are packages of code published to NPM,

The first step in creating a new module is to create a package.json file. You can create package.json files with NPM init. During this process, you will be prompted to enter the module information step by step. The module name and version number are mandatory

You will also need an entry file, which will be index.js if you use the default. Json file, you need to write the contents of the package. The simplest example is to export a function in the default index.js file that can be imported or required from someone else’s code.

exports.showMsg = function () {
  console.log("This is my first module");
};
Copy the code

In this case, your Node module will have been created

How do I publish to the NPM server

Register an NPM account
You need to log in for the first time,npm loginStore the certificate to the local, then you do not need to log in every time

To log in, you need to enter your user name, password, and email address, which are filled in when you first register

Began to publish

NPM publish Publish package

CNPM error: no_perms Private mode enable, NPM config set registry registry.npmjs.org ** After publishing, if you want to go back to the previous CNPM, use the command NPM config set Registry registry.npm.taobao.org

Create a case that references the package you just uploaded

If the package name is toniqian-test-module, create an empty directory, CD it, and run NPM install toniqian-test-module. Then a folder called node_modules will appear in this directory, where the packages you wrote will appear.

Then write an index.js as follows

var test = require('toniqian-test-module');
test.showMsg();
Copy the code

Running index. Js

node index.js
Copy the code

So the package you just uploaded is now available, and so far you have successfully created an NPM package

How do I update the NPM package

When the contents of your package are modified, for example

exports.showMsg = function () {
  console.log("This is my second module");
};
Copy the code

NPM

update_type automatically updates package.json and then re-npm publish and update will be done

The job implements a relative time conversion function

moment(time){
  // Relative time conversion
}
Copy the code

How do I delete the NPM package

npm unpublish kk-a-test --force
Copy the code

View the current NPM account

npm whoami
Copy the code

Output the username

Release package error collection

1. You need to increase the version number

NPM publish failed
sh-neverleave:z-tool neverleave$ npm publish
npm ERR! publish Failed PUT 400
npm ERR! code E400
npm ERR! deprecations must be strings : z-tool

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/neverleave/.npm/_logs/2018-11-23T10_52_01_742Z-debug.log
sh-neverleave:z-tool neverleave$ npm publish


#2, NPM publish failed
sh-neverleave:z-tool neverleave$ npm publish
npm ERR! publish Failed PUT 403
npm ERR! code E403
npm ERR! You cannot publish over the previously published versions: 1.0.3. : z-tool

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/neverleave/.npm/_logs/2018-11-23T11_24_57_662Z-debug.log
sh-neverleave:z-tool neverleave$ 
Copy the code

NPM publish –access public: NPM publish –access public

Reference: stackoverflow.com/questions/5…

NPM publish failed
sh-neverleave:npm neverleave$ npm publish
npm ERR! publish Failed PUT 400
npm ERR! code E400
npm ERR! unscoped packages cannot be private : z-tool

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/neverleave/.npm/_logs/2018-11-23T08_44_21_310Z-debug.log
sh-neverleave:npm neverleave$ 

NPM publish --access public
sh-neverleave:npm neverleave$ npm publish --access public
+ z-tool@ 1.0.0
sh-neverleave:npm neverleave$ 
Copy the code

3. Ensure that the login user account is correct

sh-neverleave:npm neverleave$ npm publish
npm ERR! publish Failed PUT 404
npm ERR! code E404
npm ERR! 404 User not found : z-tool
npm ERR! 404 
npm ERR! 404  'z-tool' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404 
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/neverleave/.npm/_logs/2018-11-23T07_32_28_518Z-debug.log
Copy the code

4. Add ‘~’ before username for login

sh-neverleave:npm neverleave$ npm login
Username: (~neverleave) neverleave
Password: (<default hidden>) 
Email: (this IS public) (1063588359@qq.com) 
npm ERR! code EAUTHIP
npm ERR! Unable to authenticate, need: Basic

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/neverleave/.npm/_logs/2018-11-23T07_27_50_877Z-debug.log
sh-neverleave:npm neverleave$ 
Copy the code

5, do not have permission to delete the package online (there is a time limit, 24 hours) solution: add –force

sh-neverleave:z-tool neverleave$ npm unpublish z-tool
npm ERR! Refusing to delete entire project.
npm ERR! Run with --force to do this.
npm ERR! npm unpublish [<@scope>/]<pkg>[@<version>]
sh-neverleave:z-tool neverleave$ 

😄 I sure hope you know what you are doing.
sh-neverleave:z-tool neverleave$ npm unpublish z-tool --force
npm WARN using --force I sure hope you know what you are doing.
- z-tool
sh-neverleave:z-tool neverleave$ 
Copy the code

6, remove NPM market package with the same name 24 hours after re-release

sh-neverleave:z-tool neverleave$ npm publish
npm ERR! publish Failed PUT 403
npm ERR! code E403
npm ERR! z-tool cannot be republished until 24 hours have passed. : z-tool

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/neverleave/.npm/_logs/2018-11-23T11_41_24_086Z-debug.log
sh-neverleave:z-tool neverleave$ 
Copy the code