So if you are new to Node, here you go with me to lock down the node for your pet project.
How to create a basic application in node.js using express 4 frameworks to elaborate how to lock dependency version of an application. By locking dependency version you can control exactly which versions of each dependency will be used when your package is installed. By default ‘npm install’ everytime installs latest version of each dependency but there may be so many scenarios where you want a specific version of any module not the latest one, for that case you must lock dependencies of modules. To implement this feature follow the steps given below:
Create project folder e.g. nodeDemo.
Now open a command prompt and Install express generator using the following command to generate an express framework application.
npm install -g express-generator
Above command will install express-generator module globally on your system.
3. Go to your project folder and run following commands:
$ npm init
$ cd ..
$ express nodeDemo
‘express nodeDemo’ will generate directory structure for an application using the express framework.
$ cd nodeDemo
$ npm install
Above command will install all dependencies of express framework application.
4. Go to project directory on command prompt and run node using the following command:
5. Now you can run your application on browser with URL http://localhost: 3000
6. Next step is to lock it’s dependencies version using npm shrinkwrap. To read about shrinkwrap you should follow https://docs.npmjs.com/cli/shrinkwrap.To do that follow the steps given below
Now add a npm-shrinkwrap.json file to project root same as the place of the package.json
Open cmd prompt and go to project folder
Run ‘npm shrinkwrap’ command
Above command will write dependencies in npm-shrinkwrap.json file. Now everytime you add any module you have to run ‘npm shrinkwrap’ command to write related dependency in npm-shrinkwrap.json file and after that whenever you run ‘npm install’ it uses npm-shrinkwrap.json file instead of package.json to install dependencies.