I was curious about how to upload a package to npm, and what kind of project structure is needed. When I was working on a project recently, I considered using the npm package to develop, but I found it was just my blind spot. This article is used to record writing npm packages. the process of.
This package is an example, mainly to determine the user's login status, temporarily named: login-test.
Let's start the production process of our npm package
-
Initialize a project
mkdir login-test cd login-test npm init
-
Add our code
Create a new index.js file. Determine whether the user is logged inmodule.exports = function(options) { const options = options || {}; const sessionUser = options["sessionUser"] || "user"; const redirect = options["redirect"] || "/login"; return function(req,res,next) { if(!req.session[sessionUser]) { return res.redirect(redirect); } next(); } }
The code is relatively simple, no login when sessionUser is empty, jump to /login (login interface)
-
Improve package.json file
Personalized configurationname
: The name of the package, make sure the name is unique
version
: the version of the package, the default is 1.0.0
description
: the description of the package
main
: the entry file, the default is index.js
test command
: the test command
repository
: the git warehouse address, generally "type": "git ","Url":"git’s url"
keyword
: This is very important, try to use more appropriate keywords as the index of this package, so that more people can search for your package
author
: write your account or your github Account
license
: Which is used in the open source agreement -
Add a LICENCE file to explain the corresponding open source agreement
We create a new file named LICENSE and paste the following content in it
The MIT License (MIT) 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.
Will
<year>
and<copyright holders>
modifications to the year and copyright owner of the name of the corresponding -
Adding the README.md file and .gitignore
README.md file is mainly used for some instructions of the project, how to use it, etc.
In the README.md in some npm packages, you will see a good-looking version information similar to the picture below
The version information icon in https://shields.io production
-
Publish npm package
1. Register an account https://www.npmjs.com
2. In the project, enter the terminal
npm adduser
, and then follow the prompts to enter. Successful login verification is: terminal inputnpm whoami
, if your name appears.3. Publish the npm package
npm publish --access=public
.4. On your computer, directly
npm install
your package name, or you can find the package you published here https://www.npmjs.com .