Skip to content

Instantly share code, notes, and snippets.

@pelevesque
Last active April 6, 2024 16:34
Show Gist options
  • Save pelevesque/c39cac3ef432baff42a3ad61832d5621 to your computer and use it in GitHub Desktop.
Save pelevesque/c39cac3ef432baff42a3ad61832d5621 to your computer and use it in GitHub Desktop.
How to create and publish a node.js module.

How to Create and Publish a node.js Module.

Based On

https://medium.com/@jdaudier/how-to-create-and-publish-your-first-node-js-module-444e7585b738

Prerequisites

  • a Github account
  • a Node.js local installation
  • update to the latest version of npm
npm install npm@latest -g
  • an npm account (confirm using your email address)
  • a Travis CI account (using Github sign in)
  • a Coveralls account (using Github sign in)

Create a Github Repository

Create a Github repository and clone it locally.

Tell npm Who You Are

npm set init.author.name “youryame”
npm set init.author.email “your.name@gmail.com”
npm set init.author.url “http://www.yoururl.com"
  1. Login to npm with your username, password, and email.
npm login

Initialize an npm Package

without an npm scope

npm init

with an npm scope

npm init --scope=username

start with version

0.0.1

This uses SemVer version naming.

For the test command, answer:

mocha --reporter spec

You should now have a package.json file in your repository.

You can specify a minimum node version by adding this to package.json:

"engines": {
  "node": ">=4.2.4"
},

Create a Node Module

Create a node module and name it index.js.

Write Some Tests

  1. Setup the Mocha testing framework and the Chai assertion library.
npm i mocha -D
npm i chai -D
  1. Create a test folder.
mkdir test && touch test/test.js
  1. Write your tests in test.js.

Run Your Tests

npm test

Commit & Push to Github

git add .
git commit -m “Initial release”
git tag v0.0.1 
git push origin master --tags

Publish to npm

For unscoped npm packages.

npm publish

For scoped npm packages.

npm publish --access=public

Pro tip! When creating a scoped module, it’s a good idea to check your package.json before publishing just to make sure that the name of your package is “@username/project-name” — otherwise it won’t be scoped and if it’s meant to be private, it will end up being public by accident.

Add Continuous Integration

Travis CI is a lightweight continuous integration tool that runs tests on every commit to your GitHub repo, even pull requests. It’s free for open source projects.

  1. Log into your Travis CI account and flick your github repository switch on.

  2. Create a .travis.yml file in your repository

language: node_js

node_js:
  - stable

install:
  - npm install

script:
  - npm test
  1. Commit and push to Github.

Add Code Coverage Statistics

Istanbul is a JavaScript code coverage library that will check statement, branch, and function coverage statistics by inserting lines of code into your JavaScript as your tests run and then report back how many of its inserted lines were called.

Coveralls is a test coverage reporter that you can integrate with Travis. It formats the coverage data in a nice way. You can get pretty much everything from the reports from Istanbul, but only locally and less pretty.

  1. Add Coveralls and Istanbul node modules.
npm i coveralls -D
npm i istanbul -D
  1. Log into Coveralls with your Github account and turn your repo's switch to on.

  2. Configure your package.json file

"scripts": {
  "test": "node_modules/.bin/mocha --reporter spec",
  "cover": "node_modules/istanbul/lib/cli.js cover node_modules/mocha/bin/_mocha -- -R spec test/*"
},

  1. Update your .travis.yml file.
language: node_js
node_js:
  - stable
install:
  - npm install

script:
  - npm run cover

# Send coverage data to Coveralls
after_script: "cat coverage/lcov.info | node_modules/coveralls/bin/coveralls.js"

Run your tests locally with

npm run cover
  1. Commit and Push to Github.

Add Badges

  1. In your Travis-CI and Coveralls accounts, you can get the markdown needed to show tests in your Github README.md file. Put this markdown at the top of your README.md file.

  2. Commit and push to Github.

  3. Update your npm version

npm version patch -m "Version %s - add sweet badges"
  1. After bumping your version.
git push && git push --tags (or git push origin master --tags)

npm publish

Test Out Your Module

  1. Add a package.json file in an empty repository.
{
  "dependencies": {
    "project-name": "*"
  }
}
  1. From the command line.
npm i
  1. Create an index.js file and require your package
var package = require('project-name');
// do stuff to test your package

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment