NPM plays an indispensable role in front-end engineering. Front-end engineers install project dependencies through NPM every day, release their packages and update packages through NPM, run the development environment through NPM scripts, package and so on.

Usually, I focus on business implementation, but I have not understood the release process of a package, so I take a break from busy work. Today, I will experience the process of writing and releasing a package, and record and share it with you. The process of a package birth is nothing more than: write – test – release – iteration – test – release… .

Initialize the project

Mkdir math-tool_demo Create a folder

NPM init initializes the package.json file and enters the project information as prompted:

  • Package Name Project name
  • Version version
  • The description described
  • Entry Point Entry point file
  • Test command Tests a command
  • Git repository Git repository address
  • Keywords Indicates the keyword information
  • The author the author
  • Agreement etc.

NPM init -y: add -y flag, directly generated by default.

Write the code in the package

Included features: we implement a small tool for addition, subtraction, multiplication and division.

Create the index.js entry file


module.exports = {
    add:add,
    minus:minus
}
function add(num1,num2) {
    if(typeof num1 === 'number' && typeof num2 === 'number') {return num1 + num2;
    }else{
        throw('arguments must be number')}}function minus(left,right){
    if(typeof left === 'number' && typeof right === 'number') {return left - right;
    }else{
        throw('arguments must be number')}}Copy the code

test

Testing is nothing more than importing written modules and running test cases.

release

NPM login Login. Enter the account password email, if you do not have an account, first request: link.

npm publish

Failure: duplicate name or similar: change package name.

Re-publish: NPM publish

How do I iterate over published packages

Once the package is released, bugs are inevitable, and of course the authors also consider extending functionality, fixing bugs or iterating functionality requires synchronizing code changes to remote repositories.


Modify the code

For example, the following changes have been made:

module.exports = {
    add:add,
    minus:minus
}
function add() {
    var left = arguments[0];
    var right = arguments[1];
    if(typeof left === 'number' && typeof right === 'number') {return left + right;
    }else{
        throw('arguments must be number')}}function minus(){
    var left = arguments[0];
    var right = arguments[1];
    if(typeof left === 'number' && typeof right === 'number') {return left - right;
    }else{
        throw('arguments must be number')}}Copy the code
Changing the Version number

We can change the version field in package.json directly.

However, it is better to use the command provided by NPM to change the version number (for example, v1.0.0):

  • npm version patch–>1.0.1: this is a minor change and does not change the function
  • npm version minor–>1.1.0: Some functions may be added, which does not affect the previous use.
  • npm version major–>2.0.0: probably changed API, input wide range of changes.

Details: About the NPM semantic version

release

npm publish

How do I use published packages

  • Search the NPM repository for package names
  • And then, typenpm install packageName -S [-D ]As a dependency or development dependency, respectively

Here’s an example of the package we just released:


Use this package for the first time

NPM install -s math-tool_demo installs the package just released

Import and use this in index.js:

var math = require('math-tool_demo')
var addRes = math.add(1.2);
var minusRes = math.minus(1.2);

console.log(
    addRes,
    minusRes,
)// Output: 3,-1
Copy the code
In use, the package may have been updated iteratively

NPM outdated checks whether each package defined in the project package.json has a new version in the remote repository

Found a new version, currently using 1.0.0, the latest is 2.0.0.

Update [packageName] NPM update [packageName]

See package dependency definitions in package.json

  • Write the exact version number directly:1.0.0
  • The big version does not move, there are functional updates to use the new version:^ 1.0.0
  • More conservative, only minor updates like bug fixes, etc. :~ 1.0.0

^1.0.0 means >=1.0.0 < 1.2.0

~1.0.0 means >=1.0.0 < 1.1.0

So updates to 2.0.0 are not allowed and the version number in package.json has to be manually changed and updated.

Why did this happen? Because the first number means a major change, a hasty update could have unpredictable consequences

Portal: NPM-outdated, NPm-update


The end of this article, if there is a mistake welcome readers to correct