[Node.js] NPM Demystified Part-1 : npm install explained in detail.



In the previous chapter, we managed to run node application with inbuilt node modules. This chapter we will include some more external node module into our application.

For now, our index.js file will look like:

const http = require('http');

const hostname = 'localhost';
const port = 3000;
const server = http.createServer((req, res) => {
  res.end('Hello World\n');
});
server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Before using external node modules, we need to install those node modules into our application.

☝☝Question: How can I install node modules in our application?
Don't Worry, It is very simple you need to use following command npm install <package_name>.

☝☝Question: Where I should search for external node modules?
You can search for node modules in this url https://www.npmjs.com/. There are multiple node modules available for the same purpose and you can use which one suits you well.

☝☝Question: Can I write my own node modules and able to push in to npm website?
Yes, You can write your own node modules and you can push your node modules into npm website. So that other developer can use your code. We will see how to do it in the separate post.

Install external npm module:

Now I'm planning to install lodash modules. One of the popular module in node.




When you search lodash in the npm website, it will return multiple modules and we are going to use lodash. Let's install it.

npm install lodash --save

It takes less than a minute for installation based on your network bandwidth.

πŸ‘πŸ‘πŸ‘Great!! you have installed external node module!

Lets check our application folder structure now.

 nodeapp
    ├───package.json
    ├───index.js
    └───node_modules
    └───lodash

If node modules installed successfully, then you can able to see node_modules folder in the parent directory and inside node_modules you can see lodash folder available.

node_modules folder is created automatically when we do npm install so we are good now.

☝☝Question: Everything looks good! But why you have used --save option while installing lodash module?
Let's see why we need to put --save in during installation.

Let's check our package.json


{
  "name": "nodeapp",
  "version": "1.0.0",
  "description": "My node app",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "lodash": "^4.17.4"
  }

}

When we do --save, installed node module will be added as a dependency for our application. It is something similar to adding Jar as the dependency in maven file.

Let's discuss more about installing in the next blog (NPM Demystified Part-2).
Please share your comments or questions in the below comment section!!πŸ‘πŸ‘πŸ‘

Comments

  1. Can I know to render a html built on angular being called by node?

    ReplyDelete
    Replies
    1. You can use express to render html files from your node application.

      Reference url : http://expressjs.com/en/api.html

      You need to install express

      npm install express --save

      Then you need to install ejs, since express requires view engine to render html page.

      npm install ejs --save

      then modify your index.js as below

      var express = require('express');
      var path = require('path');
      var router = express.Router();

      var app = express();
      app.engine('html', require('ejs').renderFile);
      app.set('view engine', 'html');


      app.get('/', function(req, res){
      res.render('helloworld.html');
      });

      app.listen(3000);

      Please make sure you have helloworld.html available in the views folder.

      folder structure will look like.

      nodeapp
      ├───package.json
      ├───index.js
      ├───views
      └───node_modules

      If you need more information, Please reply for this post.




      Delete
  2. I need a few clarifications. Now if I have a js dependency n css within my html these aren't getting reflected. I have installed the required css n js using npm. Pls help

    ReplyDelete
    Replies
    1. Yes, By using express you can overcome the above problem.

      Delete

Post a Comment