What is npm — October 25, 2014

What is npm

npm is the package manager for node.js. npm (node package manager) runs through the command line and manages dependencies for an application.

Let’s look into an example

For this we create two files “npm.js” and “package.json

npm.js :

var express = require('express'),
mongodb = require('mongodb'),
cons     = require('conslidate');

Package.json :

{
    "name"           :     "intro_to_npm",
    "version"        :     "0.0.0",
    "description"    :     "npm instruction",
    "main"           :     "npm.js",
    "dependencies":   {
        "consolidate" : "~0.9.1",
        "mongodb"     : "~1.3.10",
        "express"     : "~4.9.8"
    },

    "author" : "Deepak",
    "license":  "BSD"
}

Now look at npm.js. If you compile npm.js with node.js
$ nodejs npm.js
It pop up an error message something like this :

Error: Cannot find module 'express'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (C:ChatServerServerserver.js:6:9)
    at Object.<anonymous> (C:ChatServerServerserver.js:25:4)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)

It describes that in npm.js file we are asking for “express” but it is not yet install or present. So in order to get rid of this error install express

$ npm install express

Similarly if we run it again it again pop-up an error for “consolidate”, as it ask to install “consolidate” package and so on. Instead of getting or facing error everytime we create a file called “package.json” in which we added everything which like

        "consolidate" : "~0.9.1",
        "mongodb"     : "~1.3.10",
        "express"      : "~4.9.8"

if we run

$ npm install

It will automatically finds .json file and install all dependencies.  And this will install local modules in local modules directory named “node_modules”. You can check node_modules directory in order to check whether all required packages are installed or not.

Mean.io – Full stack javascript development — May 30, 2014

Mean.io – Full stack javascript development

What is mean.io?

Mean stands for : Mongodb, Express, AngularJS and node.js

  • Mongodb : MongoDB is the leading NoSQL database, empowering businesses to be more agile and scalable.
  • Express : Express is a minimal and flexible node.js web application framework, providing a robust set of features for building single and multi-page, and hybrid web applications.
  • AngularJS : AngularJS lets you extend HTML vocabulary for your application. The resulting environment is extraordinarily expressive, readable, and quick to develop.
  • Node.js : Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications.


Mean.io Installation

Before installting mean.io we have to install certain pre-requisities. Some of them are written below with versions that i’m using

  • NodeJS – v0.10.28
  • Bower – 1.3.3
  • npm – 1.4.9
  • Mongodb –  MongoDB shell version: 2.4.1

Let’s install them one by one

  1. Node.js :
    NOTE: When you installed node.js “npm” automatically installed no need to install it manually. We Install node.js through apt-get, run following commands on your terminal

    • sudo apt-get install python-software-properties
    • sudo apt-add-repository ppa:chris-lea/node.js
    • sudo apt-get update
    • sudo apt-get install nodejs
  2. Mongodb :

    • sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
    • echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
    • sudo apt-get update
    • sudo apt-get install mongodb-10gen
      
      To pin a package, issue the following command at the system prompt to pin the version of MongoDB at the currently installed version: 
      
      $ echo "mongodb-10gen hold" | sudo dpkg --set-selections
      
      
      sudo service mongodb start
      sudo service mongodb stop

      Restart MongoDB

      sudo service mongodb restart
  3. Mean.io :
    • sudo npm install -g meanio@latest
    • sudo apt-get install git
    • mean init myApp
    • cd myApp
    • sudo npm install -g bower
    • npm install
    • Lastly type "grunt" in myApp folder.

Congrats you have installed mean.io. Have fun! 🙂

Errors And Solutions :

  1. If you face error somewhat like :
    > kerberos@0.0.3 install /home/deepak/myApp/node_modules/mongoose/node_modules/mongodb/node_modules/kerberos
    > (node-gyp rebuild 2> builderror.log) || (exit 0)

    Try to install following
     $ sudo npm install -g node-gyp
  2. If you face error like :
    No command ‘grunt’ found, did you mean:
    Command ‘grun’ from package ‘grun’ (universe)
    grunt: command not found

    Then try to install following command on terminal
     $ sudo npm install -g grunt-cli
     $ cd ~
     $ npm update

    Now type “grunt in myApp folder”. If it run succesfully, open your browser and type “localhost:3000”.