@chris-dickson/hello-world-node

1.0.7 • Public • Published

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

  1. Create user account on https://npmjs.com

  2. Once your account is created, create an organization. For example '@my-organization'

  3. Initialize it as an NPM project and call it "hello-world-node":

    npm init
  1. In your package.json, ensure that you have the name field set to scope the package to your organization:
    "name": "@my-organization/hello-world-node"
  1. Create an index.js file with a simple export:
    exports.printMsg = function() {
        console.log("Hello world, from node");
    }
  1. Authorize your NPM user account on this machine for pushing to your NPM repository:
    npm login
  1. Publish version 1.0.0 of the package to public NPM:
    npm publish --access public

Testing the Published Package

  1. In a separate directory, create another NPM project called "hello-world-node-consumer" by running:
    npm init
  1. Install your published packaged:
    npm install --save @my-organization/hello-world-node
  1. In your index.js file, add the following:
    var hello = require('@my-organization/hello-world-node');
    
    hello.printMsg();
  1. Running node index.js should output the text:
    "Hello world, from node"

Setting up CircleCI

  1. Create a Github respository for your "hello-world-node" project and push the existing source code

  2. Go to https://circleci.com/ and sign-up, using Github authorization

  3. Ensure that your "hello-world-node" is the only project imported

  4. 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.

  1. Install gulp in your "hello-world-node" project:
    npm install gulp
  1. Add a gulpfile.js to 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);
    });
  1. CircleCI needs to have Gulp install globally to run it. Create a circle.yml file to install Gulp before each build:
    dependencies:
      pre:
        - npm install -g gulp
  1. Commit and push the changes to Github, and your build should now pass.

Continuous Deployment to NPM

  1. Get the NPM authorization token from the NPM resource file:
    cat ~/.npmrc

It should look like the following:

    //registry.npmjs.org/:_authToken=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  1. In the project settings for your CircleCI project, add the following environment variable:
    name = NPM_TOKEN
    value = xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  1. Modify your circle.yml file 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
  1. Create a new version of "hello-world-node" by running:
    npm version 1.0.1
  1. Push to Github and follow tags:
    git push --follow-tags
  1. Once the build completes, version 1.0.1 of "@my-organization/hello-world-node" should be deployed to NPM

/@chris-dickson/hello-world-node/

    Package Sidebar

    Install

    npm i @chris-dickson/hello-world-node

    Weekly Downloads

    5

    Version

    1.0.7

    License

    Apache-2.0

    Last publish

    Collaborators

    • cdickson