nodejs

To “Gulp” or to “Grunt”

Well, the answer is what you prefer! 🙂

Both of these are great tools to automate the tasks for your application. Both have great plugins and allow the developers to really speed up their development cycle by integrating some very cool plugins to automate your process. For instance code analysis, linting, minification and many more, but the question is what would you choose?

Here are some high level differences:

Grunt Gulp
Matured.

More support and bigger community.

More than 5000 plugins.

Relatively newer.

Lesser number of plugins

 

File based

Each task input/output spins up hard-disk as the results are stored in the hard-drive. Multiple tasks require multiple disk read/writes.

Stream based

Gulp uses in-memory streams to run multiple tasks sequentially. Therefore only the final output results in disk activity.

Might perform slower when there are multiple tasks configured. Relatively faster due to in memory processing.
 

Configuration over code.

Tasks are configured using configuration files. This configuration method requires the developer to be knowledgeable of the options for each task.

Gulp prefers code over configuration. Developers might find themselves comfortable with this as it is easier to understand and debug.

Also here are the sample files:

Sample Gulp file

var gulp = require('gulp');
var args = require('yargs').argv;
var $ = require('gulp-load-plugins')({lazy: true});
var config = require('./gulp.config')();
var del = require('del');

gulp.task('vet', function(){
    return gulp.
    src(config.alljs)
        .pipe($.if(args.verbose, $.print()))
        .pipe($.jscs())
        .pipe($.jshint())
        .pipe($.jshint.reporter('jshint-stylish', {verbose: true}))
        .pipe($.jshint.reporter('fail'));
});


gulp.task('styles'/*, ['clean-styles']*/, function(done){
 return gulp.
    src(config.less)
        .pipe($.plumber())
        .pipe($.less())
//        .on('error', errorLogger)
        .pipe($.autoprefixer({browsers: ['last 2 version', '> 5%']}))
        .pipe(gulp.dest(config.temp));
});


//----- MORE TASKS

 

Sample Grunt file

module.exports = function(grunt) {
  grunt.initConfig({
    jshint: {
      files: ['Gruntfile.js', 'src/**/*.js', 'test/**/*.js'],
      options: {
        globals: {
          jQuery: true
        }
      }
    },
    watch: {
      files: ['<%= jshint.files %>'],
      tasks: ['jshint']
    }
  });

  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-watch');

  grunt.registerTask('default', ['jshint']);
};

Most likely if you are just starting up a new project and don’t have much prior experience with these tools then I would assume that you’ll be leaning toward choosing gulp as your task automation tool because of its ease of use and performance but again I will leave it up to you to decide.

References:

http://gruntjs.com/

http://gulpjs.com/

 

 

Standard
nodejs

Publishing a node package

Publishing your node package to npm registry is a pretty easy task. To publish to npm registry you should have valid user credentials. You either create your user at link https://www.npmjs.com/signup or you can create a user using following commands:

   C:\Users\achana
   λ npm adduser
   Username: User12345
    npm WARN Name must be lowercase
    Username: user_test
    Password: *******
    Email: (this IS public) usertest@gmail.com
    Logged in as user_test on https://registry.npmjs.org/

If you already have a user then you can simply use npm login command to login to to npm registry. Once you are logged in, run the command npm whoami command to confirm that you are logged in.

Publishing a package

You can publish any folder which has a valid package.json file. To publish cd to the directory which has package.json file and then type  npm publish command.

Note: Your package name must be a unique, so ensure that it is not in use by someone else. To confirm this go to:

https://www.npmjs.com/search?q=mypackage&page=1&ranking=popularity where mypackage is the name your package.

If while publishing you get following error then most likely the package already exists in the npm repository:

npm ERR! you do not have permission to publish "my-package". 
Are you logged in as the correct user? : my-package

if the publish is successful then you should see following:

npmpublishsuccess

Publish successful!

Now switch over to the browser and browse to www.npmjs.com and search for your package to see if it is now visible in npm registry:

updatedpackage

Your published package found in search

And voila !

Updating package

Since you’d also like to keep adding functionality to your packages and therefore would also like the update the package in the npm registry as well. For this you would need to do following:

  1. npm version <major|minor|patch>, this will ensure that your version in package.json is incremented.
  2. npm publish

When both the commands are successful then you can see your changes are updated in the npm registry.

Standard