demo-package-nodejs v0.0.11
DEMO-PACKAGE-NODEJS
This is a sample package which follows the instructions for publishing a NodeJS package to Yarn. Yarn is used for both ergonomics and performance.
This repository can be used as a template for building packages.
See section Reusing as Template for more info on how.
Outline
This README.md file is organized as follows:
- Outline
- Full Instructions
- Summarized/Compact Instructions
- Code Structure
- Demo Package Installation Instructions
- Reusing as Template
- Adding Additional Files
Full Instructions
Full instructions for publishing a NodeJS package using Yarn can be found here: https://classic.yarnpkg.com/en/docs/creating-a-package
I made a tinyurl.com shortlinks for the
tutorial link above:
https://tinyurl.com/how-to-package-nodejs
If you want, you can include the links above to your bookmarks.
Summarized/Compact Instructions
In summary the insructions can be divided into the outline below if you don't feel clicking the tutorial link again and again.
Full instructions can be found in the tutorials above in case the summary doesn't still help in recall.
- Creating a Package
- Creating the package folder
- This involves creating the package folder using
npm initand supplying related details to thepackage.jsonfile. - A terminal-based form will be provided
for entering the
package.jsoncontents.
- This involves creating the package folder using
- Making sure that the
package.jsoncontain essential and extra fields after setting up:- Common
package.jsonfields:- name
- version
- description
- main
- repository
- author
- license
- Additional fields for
package.json:- keywords
- homepage
- bugs
- contributors
- files
- bin
- Common
- Choosing a License
- Three common licenses for NodeJS packages as stated in the tutorial
- MIT License
- Apache License 2.0
- GNU General Public License
- The tutorial referenced a more complete list of licenses with
comparisons and summaries of their corresponding permissions, conditions, and liabilities:
- More complete details for LICENSING: https://choosealicense.com/licenses/
- Three common licenses for NodeJS packages as stated in the tutorial
- Code Sharing
- GitHub
- GitLab
- BitBucket
- Documentation
- Specify a
README.mdfile
- Specify a
- Keeping Packages Small (some tips from Yarn)
- Break large packages into smaller packages
- Don't include tests or any other files that aren't necessary for packaging
- Prefer smaller dependencies unless you have a good reason not to
- Creating the package folder
- Publishing a Package
- Log in to
yarn:yarn login(only username and email are asked, no password is asked yet)- An
npmaccount is needed for the credentials
- An
- Publish:
yarn publish - Accessing package:
yarn add [package]
- Log in to
Code Structure
demo-package-nodejs
bin/ # Binary Files
demo-package.js
node_modules/ # Module Files
src/ # Package Files
say-hello-world.js
say-hi.js
random-rainbow-color.js
tests/ # Test Files
main.test
index.js # Package Entry Point
README.md # This file
LICENSE.md # MIT License Demo Package Installation Instructions
The demo package has a simple API of 3 functions.
.sayHelloWorld().sayHi(name).randomRainbowColor()- uses thechancepackage
To use this package:
yarn add demo-package-nodejs (preferred)
npm install demo-package-nodejs
Then:
const demo_package = require("demo-package-nodejs")
console.log(".sayHello() => " + demo_package.sayHelloWorld())
console.log(".sayHi('John') => " + demo_package.sayHi('John'))
console.log(".randomRainbowColor() => " + demo_package.randomRainbowColor())It should return the following:
.sayHello() => Hello, World!
.sayHi('John') => Hi, John!
.randomRainbowColor() => blueReusing as Template
If you intend to reuse this package as a template for future package projects, use the following command:
git clone https://gitlab.com/demo-package/demo-package-nodejs.git
Edit package.json to match your project description.
You can also run delete package.json and run npm init again.
Adding Additional Files
Make sure to add additional files to the files field of package.json.
Any new files in bin/ and src/ are added by default.