-
Notifications
You must be signed in to change notification settings - Fork 1
NPM Configurations
- What is NPM
- Initializing and Running NPM scripts
- Installing Third Party Packages
- Working with NODEMON
- NPM stands for Node Package Manager and it is installed together with nodejs. It is used to install some additional third party packages that are not included in node's core modules.
-
First step is to initialize your project with
npm init
command. Here you'll be asked certain questions pertaining to project configuration. you can hit enter key to skip those questions. If you skip those questions, default configurations will be applied to your project. -
All these configurations are stored in the file package.json in the JSON format. It contains a scripts section which has one default script, you can add your own scripts here and execute by using command
npm run <name>
. E.g."scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "node app.js", "greet": "echo \"Welcome Aakash\"" }, ... npm start npm run test npm run greet
-
In the above example
start
. It is a reserved name and this will always look starting point for application to run. You can execute it by commandnpm start
- You can install third party packages available through the npm repository which is a cloud package repository where all these packages live and you can conveniently install and manage them via command
npm install <package_name>
e.g.
npm install nodemon
-
npm install
command comes with multiple configurations-
npm install -g <package_name>
: this will install package globally on your machine -
npm install --save <package_name>
: this will install package locally on your current project directory with PROD dependencies -
npm install --save-dev <package_name>
: this will install package locally on your current project directory with DEV dependencies i.e. packages that we need in development environment only and we don't intend to ship them in PROD.
-
-
After successful installation, it gives you node_modules folder, the package.log.json file and it updated the package.json file. Within node_modules folder, you can see multiple packages installed along with nodemon. The reason is - if you look at nodemon folder within node_modules, it has package.json files that contains dependencies or helper packages required by nodemon. These dependencies are also auto-downloaded whenever you install any package.
-
So we got a bunch of dependencies in there and these and their dependencies are also installed, that is why you could end up with quite a big node modules folder but you can always delete that node_modules folder if you need to free up space. Now you can't use that package but you can then rerun
npm install
if you start working on that project again and it will re-install this package and all its pure dependencies and therefore recreate the node_modules folder, this is how packages work in node projects.
-
NODEMON package helps to recompile and restart your application on every single file change.
-
In order to test your recent changes, you first need to stop the server using
cntrl+c
and then restart your appnpm start
. This could be a cumbersome process. NODEMON listens to every change and it automatically restart your application on every single file change. -
Installing and Configuring NODEMON
-
Install nodemon package globally
npm install -g nodemon
-
In package.json file, look for
script
section and updatestart
key withnodemon app.js
-
So if you now run
npm start
, this will simply start the nodemon.
-