What is NPM? Why does NPM come with node.js?

What is NPM? Why does NPM come with node.js?

Daily short news for you
  • I just discovered the idb-keyval library that helps implement a key-value database simply. As shared in the series of posts about the process of making OpenNotas, I was struggling to find a type of database for storage, and it seemed quite difficult; in the end, I settled on localForage.

    idb-keyval is quite similar to localForage, but it seems to be doing a little better. For example, it has an update function to update data, simply imagine it like this:

    update('counter', (val) => (val || 0) + 1);

    Unlike the set function, which completely replaces the data.

    » Read more
  • At the beginning of the new year, may I share the amount earned after 1 month of advertising at indieboosting.com 🥳🥳🥳

    » Read more
  • Since the Lunar New Year holiday has started, I won't be posting anymore. See you all after the holiday! 😁

    » Read more

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.

Premium
Hello

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!

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!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.

Comments (1)

Leave a comment...
Avatar
Xuân Hoài Tống3 years ago
npm là một công cụ tuyệt vời cho node.js, nhờ npm ta không phải lang thang trên mạng để tìm những gói từ nhiều nguồn khác nhau, cũng không cần quá lo lắng khi tải một project trên mạng về mà không biết nó dùng những gói phụ thuộc nào. Tất cả đã có file package.json
Reply
Avatar
Gió3 years ago
quay tay à bạn ?
Avatar
Hoai Tong Xuan3 years ago
Vẫn là tôi nhưng đến từ Github
Avatar
Xuân Hoài Tống3 years ago
Bạn nói hay quá. Bạn gỡ bình luận đi
Scroll or click to go to the next page