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
  • Not to be outdone, Google recently introduced Gemini CLI - a type of AI agent similar to Codex or Claude Code.

    What’s notable is that they offer up to... 1000 queries for free each day. That’s quite a lot. Additionally, they have open-sourced this project to ensure transparency, learning, and research 🤓

    » Read more
  • There’s another tool to help quickly search command history: atuinsh/atuin.

    What’s interesting is that it uses SQLite for storage. It also provides the feature of completely synchronizing (encrypting) history between machines. How cool is that 🤓

    » Read more
  • I am really impressed with the gemma-3n-E4B model from Google. This is one of the promising models that aims to bring large language models to run on mobile devices or the web or embedded...

    It feels like it understands the prompts better, because I’ve tried many low-parameter models that often overlook my prompts. For example, when I say, "Just return the answer, no need for further explanation," many still tend to add introductory phrases or explanations... but with gemma-3n, the responses are very much to the point.

    » 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ống4 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
Xuân Hoài Tống4 years ago

Bạn nói hay quá. Bạn gỡ bình luận đi

Avatar
Hoai Tong Xuan4 years ago

Vẫn là tôi nhưng đến từ Github

Avatar
Gió4 years ago

quay tay à bạn ?