Before you start, sign up for an NPM account, if you don’t have one

◈ Publish the NPM public package

♢ Create Item

  1. To create a project folder, create a project folder or pull an existing project from remote Git

    mkdir my-test-project
    Copy the code
  2. Initialize Git Go to this folder and initialize Git first (if you haven’t already) for version control. Create a new repository on Github (or any other repository) with the same name as your project and add the readme. md file to copy the repository address as follows:

    Git git init git remote add origin [email protected]:usename//my-test-project.git git push -u origin masterCopy the code

    This associates the local repository with the remote repository

  3. Initialize NPM in the root directory of the project (my-test-project), NPM init, and then fill in the relevant information as prompted, some of which can be skipped later. Note that the name field has a few points to note:

    • This is unique, cannot be the same as all existing NPM package name, otherwise, publish error will be reported
      npm ERR! You do not have permission to publish "my-test-project". 
      Are you logged in as the correct user? : my-test-project
      Copy the code
    • In addition, there are norms for naming, cannot appear underscore, uppercase letters, Spaces and other characters, can have a hyphen (hyphen)
    • First createindex.jsFile, as a test, with a few simple lines of code, and export a default variable

♢ release

  1. Log in to user NPM and run

    npm adduser // or npm login
    Username: npm-user-name
    Password:
    Email: your-email
    Copy the code

    Enter the correct NPM user name, password, and email address as prompted. After the NPM user name is added, you will log in by default

  2. perform

    npm publish
    Copy the code

    If there are no problems, the release should be successful. You can go to NPM to search for the published package: my-test-project, or go to your NPM account to view the package

♢ About Testing

  1. After NPM init is complete, the pre-publish test can be referenced as a dependency package by other modules. Following the example above, put the my-test-project folder under the node_modules folder of the other test project, so that you can import the test variables previously exported in index.js, as in other modules

    // index.js const a = under my-test-project'this is a test'
    exportDefault a // or module.exports = a // import a from'my-test-project' // or const a = require('my-test-project')
    Copy the code

    You are advised to publish after testing

  2. Publish test to see if there is a problem. Publish is a better way to do it, just like any other module with NPM I, and then reference it

◈ Updates published packages

The command for updating a package is the same as that for releasing a package. You only need to modify the version field in package.json or use the version control command provided by NPM to change the version number.

  1. Changing the Version number
  2. npm publish

♢ npm version

NPM provides NPM version for version control. The effect is the same as manually changing the version field in package.json. The advantage is that the NPM version command can be used automatically during build process. And it has Semantic versioning.

npm version [<newversion> | major | minor | patch | premajor | preminor | 
prepatch | prerelease | from-git]
Copy the code

Its semantics are:

Major: major version (major version) Minor: minor version (minor update) Patch: patch number (patch) PreMajor: pre-major version PreMinor: pre-patch version prerelease: pre-release versionCopy the code

For example, if the initial version is 1.0.0, the corresponding semantics are as follows:

NPM Version Patch // 1.0.1 indicates minor bug fixes. NPM Version Minor // 1.1.0 indicates minor new features. NPM Version mMajor // 2.0.0 indicates major version or major upgrade NPM Version Preminor // 1.1.0-0 is followed by 0, indicating a pre-releaseCopy the code

You can see the corresponding version change in the package.json of the current module

◈ Revoke publication

Because the undo conference does not allow packages that depend on the package to be undone to work properly, the NPM’s official revoking of packages is limited:

  1. It is not allowed to undo packages that have been published for more than 24 hours (unpublish is only allowed with versions published in the last 24 hours)
  2. If you do want to undo within 24 hours, add the –force parameter
  3. Even if the released package is revoked, it cannot be re-released with the same name/version as the previously revoked package, because the uniqueness of the two has been occupied and is not officially deleted with the cancellation

♢ npm unpublish

The unpublish command is NPM unpublish

NPM unpublish my-test-project NPM ERR! Refusing to delete entire project. npm ERR! Run with --force todothis. npm ERR! NPM unpublish my-test-project --force NPM WARN using <@scope>/]< PKG >[@<version> --force I sure hope you know what you are doing. - my-test-projectCopy the code

♢ npm deprecate

Recommended alternative to NPM unpublish:

npm deprecate <pkg>[@<version>] <message>
Copy the code

This command does not undo existing packages on NPM, but will give deprecated warnings when anyone tries to install the package, such as:

npm deprecate my-test-project 'this package is no longer maintained'
Copy the code

Reference:

  • Creating and publishing unscoped public packages
  • [NPM] Use NPM to install/delete/publish/update/cancel distributions
  • Version number management policy && uses NPM to manage project version numbers