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.
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 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.
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 init
and supplying related details to thepackage.json
file. - A terminal-based form will be provided
for entering the
package.json
contents.
- This involves creating the package folder using
- Making sure that the
package.json
contain essential and extra fields after setting up:- Common
package.json
fields:- 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.md
file
- 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
npm
account is needed for the credentials
- An
- Publish:
yarn publish
- Accessing package:
yarn add [package]
- Log in to
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
The demo package has a simple API of 3 functions.
.sayHelloWorld()
.sayHi(name)
.randomRainbowColor()
- uses thechance
package
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() => blue
If you intend to reuse this package as a template for future package projects, use the following command:
git clone https://gitlab.com/lvjhn/demo-package-nodejs [your_project]
Edit package.json
to match your project description.
You can also run delete package.json
and run npm init
again.
Make sure to add additional files to the files
field of package.json
.
Any new files in bin/
and src/
are added by default.