When installing node.js, we are "gifted" with npm.
Check the current version of npm.
npm -v
# 6.14.4
NPM, or Node Package Manager, is a repository for open-source node.js projects. It provides solutions to interact with the repository, install packages, manage versions, and manage dependencies through the command line.
Every day, hundreds or thousands of projects are uploaded here, including new libraries or frameworks. This means that most of the things you need are available here.
For example, if you need a library to convert Vietnamese with accents to without accents, instead of writing it yourself, there is already a package called vn-remove-accents available on npm that provides you with that functionality. You just need to install it.
# create a directory
mkdir remove-accents
# move into the directory
cd remove-accents
# install the vn-remove-accents package
npm i vn-remove-accents
Create an index.js file in the remove-accents directory, with the following content:
const removeAccents = require('vn-remove-accents');
const str = 'Blog chia sẻ lập trình kiến thức javascript';
console.log(removeAccents(str)) // Blog chia se lap trinh kien thuc javascript
Most packages on npm come with installation instructions and usage guides, so you don't have to worry too much about how to use them. What you need to do is to identify the package that supports solving your current problem (you can search on Google with your requirements and add "npm" at the end for many suggestions). For example: "vietnam remove accents npm".
Going back to the root remove-accents directory created above, you will see a package.json file is generated, including some information including the vn-remove-accents package that was pulled in, and a node_modules directory.
The node_modules directory will contain the packages that you have installed, such as the vn-remove-accents package mentioned above. The package.json file serves as a dependency management file. When you run npm install
, it will install all the packages listed in this file.
NPM is a great tool for node.js, as it saves you from searching for packages from various sources on the internet and eliminates the worry of downloading a project without knowing its dependencies. Everything is covered in the package.json file.
However, not all packages on npm are reliable. Be cautious when installing them.
5 profound lessons
Every product comes with stories. The success of others is an inspiration for many to follow. 5 lessons learned have changed me forever. How about you? Click now!
Subscribe to receive new article notifications
Comments (1)