Publish package

  1. Initialize the basic structure of the package

    1. The folder my-package of the new key package, as the root directory of the package


    2. In the my-package folder, new key the following three files:

      1. package.json package management configuration file

      2. The entry file of the index.js package

      3. README.md package description file


    3. Initialize package.json

      1
      2
      3
      4
      5
      6
      7
      8
      9
      {
      'name':'my-package', //name attribute and package folder name can be different. But the package name is ultimately determined by the name attribute.
      //The package name is unique. You should go to npm to retrieve the package name before naming it.
      'version':'1.0.0', //Default initial version number
      'main':'index.js', //When the outside world imports a folder through require(), it will be automatically searched through the main attribute
      'description':'Time and HTMLEscape formatting function', //Users can see it in npm search
      'keywords':['my-package', 'dateFormat', 'escape'], //Search keywords
      'license':'ISC' //Open source protocol, the default ISC protocol.
      }


  1. Develop features in your own package: escape HTML, restore HTML

    Used to escape user input. If the user input has html related characters, it will cause a display error.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    function htmlEscape(htmlStr){
    return htmlStr.replace(/<|>|*|&/g, (match) => { //Regular, g is a global match
    switch(match){
    case '<': return '&lt;'
    case '>': return '&gt;'
    case '*': return '&quot;'
    case '&': return '&amp;'
    }
    })
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    function htmlUnescape(str){
    return htmlStr.replace(/&lt;|&gt;|&quot;|&amp;/g, (match) => {
    switch(match){
    case '&lt;': return '<'
    case '&gt;': return '>'
    case '&quot;': return '*'
    case '&amp;': return '&'
    }
    })
    }

    There is also a formatted time function in the example, which is very simple not to record.


  2. Function modular split in the package

    1. Split the corresponding functions into src -> function1.js. For example, split the function of formatting time into src->dataFormat.js, and split the function of processing HTML strings into src->htmlEscape.js.

    2. In index.js, import two modules to get the methods that need to be shared externally.

    3. In index.js, use module.exports to share the corresponding method. Because some modules have multiple functions, the spread operator ...function1 should be used directly to expose them all (even if there is only one function in the module).


  3. Write a README document

    Written in markdown format. There is no mandatory requirement for the content, as long as the description of the function, usage, and precautions of the clear package can be written. For example, the README document includes:

    1. Installation method
    2. Import method
    3. Format time function
    4. Escape HTML function
    5. Restore HTML function
    6. Open Source Protocol

    When writing, you can paste the code, start with a newline ```js, and end with ``` after a newline. The title should start with ##. Open Source Protocol ISC.


  4. Release Package

    1. Register an npm account (on the npm official website)

    2. Log in to your npm account (in the terminal) and execute npm login . Fill in the account password and email according to the prompts. Note that before running the command, you must first switch the download server address to the official server of npm (the official server in AU), otherwise it will cause the release of the package to fail.

    3. Run the npm publish command (move to the root directory of the package to publish, no need to write the package name in the command) to publish the package to npm. Package names must not be repeated.

    4. Log in to npm and click on yourself -> package to view the package.


  1. Remove published package

    Run the npm unpublish package name --force command to delete it. But note:

    1. The npm unpublished command can only delete packages published within 72 hours. After exceeding it can never be deleted.
    2. Packages deleted by npm unpublished cannot be published repeatedly within 24 hours.
    3. Be careful when publishing packages, try not to publish meaningless packages on npm.

Share