Understanding The Basics Of Npm Scripts
NPM ( Node Package Manager ) is mainly used for two things: First and foremost, it is an online repository for the publishing of open-source Node. js projects; Second, it is a command-line utility for interacting with a said repository that aids in package installation, version management, and dependency management.
We use scripts to automate repetitive tasks. For example, building your project, minifying Cascading Style Sheets (CSS), and JavaScript (JS) files. Scripts are also used in deleting temporary files and folders, etc.
Some useful feature
- Pre-installed with Node
- Easy install packages/modules on your system
- Modules are basically javascript libraries
- Make it easy for developers to share and reuse code
If you want to use NPM you have to have node js installed. It’s preinstalled with node js.
Let’s first understand what is a package manager with an example
Consider you were building your own website and you need some special feature (Eg: animated vertical carousel) in it and you build that by your self then you think the functionality you build will help others also, so you decide to make it available to everyone by publishing it to sites like npm. Now other developers who are in need of that functionality can use your code by merely downloading that code from the npm. Suppose in the future you added some extra functionality to your carousel, with npm other developers who are using your code can easily get those updates.
The functionality that you build is called package or module and package manager is nothing tool or software that manages all the packages is called a package manager. In simple words, packages are pice of reusable code.
Every package has a package.json file that contains all the metadata of that package such as name, author, scripts, dependencies required for that project, etc. we can manually create a package.json file or we can run a command (npm init
)to create that file for us.

use of package.json file
1. manage dependencies
2. scripts (To automate tasks)
Run the below commands in the terminal (or) cmd prompt.
To Get Version
npm --version
(or)npm -v
To Get Help npm help
To set default value in package.json file
npm config set init-author-name "your-name"
you can set a default value for all the values in the package.json file with the above command ( Eg: init-license ).
To get a value in package.json file
npm config get init-author-name
To remove a default value in package.json file
npm config delete init-author-name
To initialize a project
npm init
As disscused above it will initilize project with a packahge.json file.
npm init --yes
the command will prefill all the values for you with the default value.
To Install a Package
we can classify packages as two types :
- Local packages
- Global packages
Local Packages
Package which can be used only for specific project.
npm install package-name
will install the package inside the node_modules
folder.
By default npm install package-name
will install the latest stable package. We can also install packages with a specific version.
npm install package-name@4.16.1
we can see detailed explainition in the upcomming section.
[NOTE – You can use i
instead of install
]
npm i package-name
If you want to install and make an entry in the package.json file as dependencies run.
npm install package-name --save
( or )
npm install package-name -S
( or )
npm i package-name -S
If you want to install and make an entry in the package.json file as dev dependencies run.
npm install package-name --save--dev
( or )
npm install package-name -D
( or )
npm i package-name -D
[ Note: The difference between dependencies and dev dependencies are :
1. The package which is installed as dependencies are used for the project to run (used in production).
2. The package which is installed as dev dependencies are used only for development purpose. ]
Global Packages
Package which can be used for all the project in your machine.
npm install package-name --global
will install the package globally.
[ You can use -g
instead of --global
]
To Uninstall Package
Uninstall Local Package
npm uninstall package-name
will uninstall the package.
To uninstall and remove the entry from dependencies in the package.json file run.
npm uninstall package-name --save
To uninstall and remove the entry from dev-dependencies in the package.json file run.
npm uninstall package-name --save--dev
Uninstall Global Package
npm uninstall package-name -g
will uninstall the global packages.
[ NOTE – You can use remove
or rm
or un
instead of uninstall. ]
npm remove package-name
( or )
npm rm package-name
( or )
npm un package-name
To List Packages
To list packages that are installed locally and globally run the following commands.
npm list
will shows all the packages that are installed locally.
npm list -g
will shows all the packages that are installed globally.
If we run npm list
by default, it shows all the packages and also its dependencies packages as a tree-like structure. we can limit the depth of the tree with --depth
a flag followed by a number.
npm list --depth
( npm list --depth 1
)
versioning of packages

we can also install the packages with specified major (or) minor (or) patch version
Eg :npm install package-name@4.12.3
will install the specified version.npm install package-name@4.12
will install the specified major and minor version and the latest patch version.
npm install package-name@4
will install the specified major version and the latest patch and minor version.
Installing Packages from package.json file
npm install
will install all the packages listed as dependencies and dev dependencies in the package.json.
For Eg: If you download any projects from git hub it don’t have the required dependencies but the package.json file contains all the list of required dependencies and dev dependencies. If we run the npm install
it will download all the listed recorded into the node modules.
NOTE – When you download any project online you will see these symbols ^ * ~
before the dependencies and dev-dependencies packages

^
will install the specified major version and latest minor , patch version
~
will install the specified major, minor version and latest patch version
*
will install the latest major, minor and patch version
without any symbol before the version will install the specified versions
To Update Packages
Update locally
To update all the locally installed package run npm update
.
To update the specified dependencies package run .
npm update package-name --save
To update the specified dev-dependencies package run.
npm update package-name --save--dev
Update Globally
To update all the globally installed package run npm update -g
.
To update specific global package run npm update -g package-name
.
Remove package
To uninstall unused packages from your project run npm prune
.
NPM SCRIPTS
As mentioned above we can also use npm scripts to run some commands or automate tasks.
You can find the scripts in the package.json file.

we can declare the scripts in the package.json file as mentioned above and execute it by running.
npm run script-name
Eg: npm run start
[ we can also create our own custom scripts ]
Will learn more about NPM Scripts in the future posts
Conclusion
To see some of the shortcodes of npm CLICK HERE
To learn more about npm script CLICK HERE