Let’s talk about releasing a package to NPM normally

  • Publish the simplest NPM package:
  • First go to the official website to register an account, fill in the account, password, email
  • Then login to the NPM account NPM login
  • Then execute NPM publish in the written package directory to publish

The service we want to do now is to click a publish button to publish an automatically generated package

  • The.npMRc file is placed under our package delivery service. The NPMRC file is as follows
registry=http:.... / / /... /:_password= //.... /:username= //.... /:email= //.... /:always-auth=falseCopy the code

You can also run cat.npmrc from the command line (remember in the root directory)

  • Once the login permission is available, the program automatically creates the package contents (node uses the FS module), and then NPM publishes the package (see below)
const util = require('util');
const childProcess = require('child_process');
const exec = util.promisify(childProcess.exec); 
async publish() {
     // dir =.npmrc publishDir
    const dir = ' ';
    const publishDir = ' ';
    const cmdStr = `npm publish --userConfig=${dir}/.npmrc`;
    try {
      const content = await exec(cmdStr, { cwd: publishDir });
      console.log(content, 'Released successfully');
    } catch (err) {
      console.error(err, 'Publish failed'); }}Copy the code
  • The dispatch service will be fine