NPM package (publish, run, debug) experience summary
Objective:
Create an NPM package from 0 to 1 to experience the process of NPM package release, running and debugging.
Steps:
1 what do you do
Make a mobile phone number, user name plus * function. Example: 13012345678 –> 130****5678 This function implementation is very simple, the purpose is to experience the whole NPM package process.
2. Build the package framework to achieve the functions
2.1 Overall Framework
|bin
--index.js # command line
|script
--index.js # NPM script
|src
--index.js # Related code
package.json
.gitignore
README.md
.npmignore # NPM ignore file
Copy the code
Knowledge npmignore
.npmignore files similar to. Gitignore files are filtered out when NPM packages are uploaded.
2.2 the SRC/index. Js
SRC /index.js is used to implement the function
/** * @param {*} STR 11 bits */
function mobileAsterisk(str) {
if(str.length ! = =11) {
const err = new Error("String length is not 11 digits.");
throw err;
}
const start = str.slice(0.3);
const end = str.slice(7);
const result = `${start}* * * *${end}`;
return result;
}
module.exports = {
mobileAsterisk
};
Copy the code
2.3 bin/index. Js
Bin /index.js Is used to run scripts related to command line tools
#! /usr/bin/env node
const { mobileAsterisk } = require(".. /src/index")
const num = process.argv[2];
console.log(mobileAsterisk(num))
Copy the code
The bin folder is used to execute command-line scripts.
Knowledge shebang:
By marking #! The /usr/bin/env node lets the machine know that the file is being run through node
Shebang related information
Command line parameters:
Argv is used directly to parse the arguments. The first argument is the node location, the second argument is the script location, and the subsequent arguments are input. See the Node documentation for details. 2) Use commander. Js to parse the commander
2.4 package. Json
{
"name": "addasterisk"."version": "1.0.3"."description": "My mobile phone, email and account to add an asterisk (*) | is still in perfect"."main": "src/index.js"."repository": "https://gitee.com/yatsov/AddAsterisk.git"."author": "yatsov <[email protected]>"."license": "MIT"."private": false."bin": {
"asterisk":"bin/index.js"
},
"scripts": {}}Copy the code
1 bin:
The files that are configured to call the command line, sometimes we want to write packages that can be run like the command line and that’s where the bin configuration item is needed. The bin configuration item creates a mapping between the file and the command line. In simple terms, it establishes a soft link in./node_modules/.bin/ or node_global_path(which can be viewed using the NPM root command)\bin.
1 docs.npmjs.com/files/packa bin configuration related document… 2 NPM root command docs.npmjs.com/cli/root.ht… Don’t forget to add shebang to your linux-like system for 2.3
In addition, if you use the bin configuration to write a file name directly (as the following file configuration does), the command line name will be the same as the project name.
{" name ":" my - program ", "version" : "0.0.1", "bin" : ". / path/to/CMD}Copy the code
3 Test Procedure
After the package is written, you need to test it. You can use the NPM link command to debug, avoid the pain of frequently sending packages, and test the installation locally. The step is to first map your own modules to the global
Go to the module directory and link it to the global directory
cd my-package-path
npm link
Copy the code
Then connect the test package through the NPM link in the prepared test package
# Go to the project directory via the package name to link
cd my-test-project-path
npm link my-package-name
Copy the code
If you want to delete link’s package then
npm unlink my-package-name
Copy the code
Related literatures: 1 github.com/atian25/blo… 2 NPM link docs.npmjs.com/cli/link.ht…
4 How does NPM send packets
Once the test is complete, you are ready to ship
4.1 Preparation Procedure
Prepare a Git repository for related packages, and prepare an NPM account.
4.2 NRM Manages related sources
NRM is a great tool for managing your own NPM sources. 1 Run the NRM ls command to list existing NPM sources.
4.3 NPM login
Use the NPM login command to login.
4.4 NPM contract
Publish using NPM at the project location.
Done!
Then you can push the package to NPM. www.npmjs.com/package/add…
🐈 Criticism is welcome!!!! 🐈