https://medium.com/@jdaudier/how-to-create-and-publish-your-first-node-js-module-444e7585b738
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 and clone it locally.
npm set init.author.name “youryame”
npm set init.author.email “your.name@gmail.com”
npm set init.author.url “http://www.yoururl.com"
- Login to npm with your username, password, and email.
npm login
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 and name it index.js.
- Setup the Mocha testing framework and the Chai assertion library.
npm i mocha -D
npm i chai -D
- Create a test folder.
mkdir test && touch test/test.js
- Write your tests in test.js.
npm test
git add .
git commit -m “Initial release”
git tag v0.0.1
git push origin master --tags
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.
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.
-
Log into your Travis CI account and flick your github repository switch on.
-
Create a .travis.yml file in your repository
language: node_js
node_js:
- stable
install:
- npm install
script:
- npm test
- Commit and push to Github.
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.
- Add Coveralls and Istanbul node modules.
npm i coveralls -D
npm i istanbul -D
-
Log into Coveralls with your Github account and turn your repo's switch to on.
-
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/*"
},
- 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
- Commit and Push to Github.
-
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.
-
Commit and push to Github.
-
Update your npm version
npm version patch -m "Version %s - add sweet badges"
- After bumping your version.
git push && git push --tags (or git push origin master --tags)
npm publish
- Add a package.json file in an empty repository.
{
"dependencies": {
"project-name": "*"
}
}
- From the command line.
npm i
- Create an index.js file and require your package
var package = require('project-name');
// do stuff to test your package