How to write a public NPM package yourself

with markdown-clear , create process as an example, explain the entire NPM package creation and publishing process

1 How to create a package

1.1 Create and use a project

  • Create a new warehouse on GitHub named markdown-clear

  • clone This project goes local

1.2 Add toLICENCEorLICENSEFile describing the corresponding open source protocol

MIT License
Copyright (c) <year> <copyright holders>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

1.3 Add README or ReadMe.md or README.md files

  • Describe some information about the project

  • Links to detailed references

  • Navigation Content as a Whole for Readers

1.4 Add.gitignore files, ignoring file changes that do not need to be submitted

1.5 Initialize NPM Package

  • Initialize the project using npm init

  • Fill in the appropriate content as prompted

1.6 to here directory structure

  • All three components of the project and the npm package configuration file are available

markdown-clear
------------- .gitignore
------------- LICENCE
------------- README.md
------------- package.json

1.7 EditorConfig

Editor settings across editors, website hangs up, EditorConfig

1.8 ESLint

New Generation of JavaScript Code Quality Detection Tools ESLint

2 Code structure organization

2.1 Add code-related directories

markdown-clear
-------------- src     // Source code directories such as coffee,typescript,es6+, and so on
-------------- lib     // The directory of escaped generated code, such as babel escaped es5 code
-------------- docs    // Code-related design and usage documentation
-------------- tests   // Related test catalogs

2.2 Code implementation

  • Write code src directory

  • Translated code lib directory

2.2.1 Convert code using babel
{
  "presets":["es2015","stage-0"]
}
  • Add npm command

  "scripts": {
    "build": "babel src -d lib",
   }
2.2.2 Implement a globally installable npm package
  • Add configuration for package.json

  "bin": {
    "markdown-clear": "./lib/cli.js"
  }
  • Add the first line of cli.js file

#!/usr/bin/env node

2.3 Test

  • Write test case tests directory

  • Call the directory below the resulting lib

  • Consider using the test framework mocha, jasmine, karma...

2.3.1 Installation Test
  • Install local files as local packages using npm

npm install path/to/markdown-clear
  • Install local files as global packages using npm

npm install path/to/markdown-clear -g

2.4 Document Output

  • Write document docs directory

  • Write code-related design and usage documentation, there is no natural need to write

  • Documents here should have an entry in README.md.

3 Publish NPM packages

npm adduser USERNAME
  • If you are not logged in

npm login
  • Publish package after login, execute under project directory

npm publish

Keywords: Javascript npm github JSON

Added by jonez on Fri, 07 Jun 2019 20:04:13 +0300