Contract awarding

I’m going to create a project called Add-function-demo and write a simple summation method

// dist/index.js
function add(a, b) {
  return a + b;
}

module.exports = add;
Copy the code

Create an NPM account

You must create an NPM account for sending packets, and enter the password and email address for login

Approximate directory structure of NPM package

└── ├─ dist/index.js ├─.nPMIgnore ├─ CHANGELOG.md ├── download.json

Configuration of package.json files

When we initialize an NPM package with NPM init-y, we get a package.json file:

{
  "name": "add-function-demo"."version": "1.0.0"."description": ""."main": "index.js"."scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": []."author": ""."license": "ISC"
}
Copy the code

Note: In order to send the package, we need to modify or add some things

  1. “name”: “@zerozhang/add-function-demo”

    ◆ Make sure the package name is unique

    ◆ It is recommended to place the package name under the current account, i.e. “@user/package-name”.

  2. “main”: “dist/index.js”

    ◆ When downloading NPM package, users will find the entry file of the package

    The index.js file will be imported by default

  3. “private”: false

    Indicates that the current package is public (private packages cannot be uploaded)

Package. json after modification:

{
  "name": "@zerozhang/add-function-demo"."version": "1.0.0"."description": ""."main": "dist/index.js"."scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": []."author": "zerozhang"."license": "ISC"
}
Copy the code

. Npmignore file

Similar to the.gitignore file, the directory in this file is ignored by NPM and will not be uploaded. Such as:

# omit examples/ packages/ public/ # omit the specified file vue.config.js babel.config.js *.mapCopy the code

The README file

Documentation of the current project

CHANGELOGO file

Update documentation for the current project, which is attached each time a version is upgraded

NPM mirror

If the NRM image source management tool has been installed, ensure that the current source is the NPM source; otherwise, the upload fails

nrm ls

* npm -------- https://registry.npmjs.org/       
  yarn ------- https://registry.yarnpkg.com/     
  cnpm ------- http://r.cnpmjs.org/
  taobao ----- https://registry.npm.taobao.org/  
  nj --------- https://registry.nodejitsu.com/   
  npmMirror -- https://skimdb.npmjs.com/registry/
  edunpm ----- http://registry.enpmjs.org/   

Use NRM use to switch back to the NPM source
nrm use npm
Copy the code

Contract awarding command

  • NPM login login
  • npm publishrelease
    # If package.json name is in the format of '@user/package-name', you can add the following options to publish the package
    npm publish --access public
    Copy the code

Successful release:

$ npm publish --access public npm notice npm notice package: @zerozhang/[email protected] NPM notice === Tarball Contents === NPM notice 65B dist/index.js NPM notice 256B package.json npm notice 0 CHANGELOGO.md npm notice 104B README.md npm notice === Tarball Details === npm notice name: @zerozhang/add-function-demo NPM notice Version: 1.0.0 NPM notice Package size: 514 B NPM notice Unpacked size: 425 B npm notice shasum: 037b3a7be38ff049956d4e22f9d6efd39e68a5b6 npm notice integrity: sha512-4JAzO/5vVcUXS[...] 1vLBUGtAgOv2A== NPM notice Total Files: 4 NPM notice + @zerozhang/[email protected]Copy the code

upgrade

  1. Update the CHANGELOGO file (e.g. we add a multiplication): // dist/index.js:
    function add(a, b) {
      return a + b;
    }
    function multiply(a, b) {
      return a * b;
    }
    
    module.exports = {
      add,
      multiply
    };
    Copy the code

    // CHANGELOGO:

    # update log
    # # [1.0.0] - 2021-10-13
    New in # # #
    -[index] Adds the multiplication calculationCopy the code
  2. Make sure you submit your changes,Keep your Git working directory cleanOtherwise, the upgrade fails:
    $ npm version patch
    npm ERR! Git working directory not clean.
    npm ERR! M CHANGELOGO.md
    npm ERR! M dist/index.js
    Copy the code

The upgrade command

  • npm version patchUpgrade pudding At this point, the version in the package will be automatically upgraded to"Version" : "" 1.0.1,
  • Git push commits the current version update
  • NPM publish publishes updates

The release succeeded, and you can see that the version number has changed ~

To upgrade the difference

There are three ways to upgrade: pudding/minor version/major version

  • Patchnpm version patch 1.0.0 - > 1.0.1
  • Minor (minor)npm version minor 1.0.0 - > 1.1.0
  • Major (Major version)npm version major 1.0.0 - > 2.0.0

Well today share here ~!