Develop an NPM package

When developing the NPM package locally, we can use the NPM link command to link the NPM module to the corresponding running project, which is convenient for debugging and testing the module.

Create two folders:

  • Npm-link-module, the NPM package to develop
  • Npm-link-test, used to test the package we developed

NPM init-y on npm-link-module generates a default package configuration file package.json, creates a new directory named index.js as follows, and writes a simple date conversion function

function formateTime(date) {
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDay();
    month = month < 10 ? '0' + month : month;
    day = day < 10 ? '0' + day : day;
    return year + The '-' + month + The '-' + day;
}
module.exports = formateTime

Copy the code

After executing the command, npm-link-module will be linked globally according to the package.json configuration. The path is {prefix}/lib/node_modules/ , as described in the official documentation. We can use the NPM config get prefix command to obtain the value of prefix and find the corresponding folder. We can see that a shortcut to npm-link-Module has been generated under this directory

NPM -link-module: NPM -link-module: NPM -link-module: NPM -link-module: NPM -link-module: NPM -link-module: NPM -link-module: NPM -link-module: NPM -link-module

Now the nPm-link-test project can reference the nPm-link-module module

Create an index.js file under npm-link-test

let formateTime = require('npm-link-module');
let date = new Date(a);console.log(formateTime(date));

Copy the code

After executing node index.js, you can see that the command line has printed the result of the function’s execution



Then, we modify the date function in the index.js file in npm-link-module to return the date in slash format

return year + '/' + month + '/' + day;

Copy the code


Return to npm-link-test to execute index.js, and you can see that the result has been changed to a slash date



All changes to npm-link-module are mapped directly to nPm-link-test /node_modules/npm-link-module

NPM package release

registered

First go to the official website to register an account, note: registered email must be verified. Or register locally:

npm adduser
Username:
Password:
Email:
Copy the code

The login

Registration is successful, verify with the following command

npm whoami // Verify that your credentials are stored in the client, and your user name will be displayed after successful registration
Copy the code

If you already have an account, use NPM login to login locally. Enter your account and password as prompted.

Publish a package

Publish NPM: NPN publish

npm publish --access=public // If the package is public, if the package is not public, remove --access=public
Copy the code

Update package

npm version major/minor/patch // This command will automatically change your version number, or you can manually change it
npm publish
Copy the code

NPM version Parameter description

  • Patch: Minor changes, such as bug fixes, version number changed v1.0.0->v1.0.1
  • Minor: Added new features without affecting existing features. Version number changed to V1.0.0 -> V1.1.0
  • Major: Broke module backward compatibility, version number change v1.0.0->v2.0.0

Delete the package

1. Delete the specified version

NPM unpublish package name @version numberCopy the code

2. Delete the entire package

NPM unpublish package name --forceCopy the code