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
  • openai/codex is the latest open-source project from OpenAI, following their announcement of the two newest models, o3 and o4 mini. It is said that both o3 and o4 mini are very suitable for being Agents, so they released Codex as a lightweight Agent that runs directly in the Terminal.

    Regarding its applicability, since it is an Agent, it can read/add/edit/delete the contents of your files. For example.

    codex "explain this codebase to me"

    Or integrate it into your CI/CD pipeline.

    - name: Update changelog via Codex run: | npm install -g @openai/codex export OPENAI_API_KEY="${{ secrets.OPENAI_KEY }}" codex -a auto-edit --quiet "update CHANGELOG for next release"

    Oh, I almost forgot, you need to use the OpenAI API 😆

    » Read more
  • Perhaps many people do not know that OpenAI has launched its own academy page to help users learn and fully harness the power of their language models.

    OpenAI Academy

    » Read more
  • Mornings have started with some sensational news: OpenAI wants to acquire Windsurf for $3 billion 😳

    » 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

The secret stack of Blog

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, 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
Gió3 years ago
quay tay à bạn ?
Avatar
Hoai Tong Xuan4 years ago
Vẫn là tôi nhưng đến từ Github
Avatar
Xuân Hoài Tống4 years ago
Bạn nói hay quá. Bạn gỡ bình luận đi