@chris-dickson/hello-world-node
v1.0.7
Published
A step-by-step guide to setting up a public NPM module with CircleCI continuous integration and deployment
Readme
Hello-World-Node
A step-by-step guide to setting up a public NPM module with CircleCI continuous integration and deployment
Setting up NPM publishing to an Organization
Create user account on https://npmjs.com
Once your account is created, create an organization. For example '@my-organization'
Initialize it as an NPM project and call it "hello-world-node":
npm init- In your
package.json, ensure that you have thenamefield set to scope the package to your organization:
"name": "@my-organization/hello-world-node"- Create an
index.jsfile with a simple export:
exports.printMsg = function() {
console.log("Hello world, from node");
}- Authorize your NPM user account on this machine for pushing to your NPM repository:
npm login- Publish version 1.0.0 of the package to public NPM:
npm publish --access publicTesting the Published Package
- In a separate directory, create another NPM project called "hello-world-node-consumer" by running:
npm init- Install your published packaged:
npm install --save @my-organization/hello-world-node- In your
index.jsfile, add the following:
var hello = require('@my-organization/hello-world-node');
hello.printMsg();- Running
node index.jsshould output the text:
"Hello world, from node"Setting up CircleCI
Create a Github respository for your "hello-world-node" project and push the existing source code
Go to https://circleci.com/ and sign-up, using Github authorization
Ensure that your "hello-world-node" is the only project imported
CircleCI automatically detects that this is an NPM project and immediately starts a build. You'll notice the builds failed because tests did not pass. By default, NPM will have the following script for newly initialized projects:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},It will execute the test script, and fail on any non-zero return code.
- Install gulp in your "hello-world-node" project:
npm install gulp- Add a
gulpfile.jsto run a test:
var gulp = require('gulp');
gulp.task('test',function(done) {
console.log("Run your tests here, any non-zero exit code causes CirlcleCI to fail");
process.exit(0);
});- CircleCI needs to have Gulp install globally to run it. Create a
circle.ymlfile to install Gulp before each build:
dependencies:
pre:
- npm install -g gulp- Commit and push the changes to Github, and your build should now pass.
Continuous Deployment to NPM
- Get the NPM authorization token from the NPM resource file:
cat ~/.npmrcIt should look like the following:
//registry.npmjs.org/:_authToken=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx- In the project settings for your CircleCI project, add the following environment variable:
name = NPM_TOKEN
value = xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx- Modify your
circle.ymlfile to look like the following. It will automatically push tagged builds to NPM:
dependencies:
pre:
- echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
- npm install -g gulp
deployment:
npm:
tag: /v[0-9]+(\.[0-9]+)*/
commands:
- npm publish --access public- Create a new version of "hello-world-node" by running:
npm version 1.0.1- Push to Github and follow tags:
git push --follow-tags- Once the build completes, version 1.0.1 of "@my-organization/hello-world-node" should be deployed to NPM
