The difference between npm and npx
When running node-related commands such as Prisma,
we often use `npx prisma migrate dev` instead of `npm prisma migrate dev`, but sometimes I wonder, "What is the difference between npm and npx?"
What is npm?
npm is a Node.js package manager.
Its roles can be broadly divided as follows.
Package management
npm install lodash→ lodash is added to node_modules
・Execution of scripts recorded in package.json
npm run dev→ Execute commands written in scripts.dev
In short, the main role of npm is "dependency management and script execution."
What is npx?
npx is a command for easily executing npm packages.
Specifically:
・Execute locally installed packages
npx prisma generate→ Find and execute node_modules/.bin/prisma
・Temporarily execute packages that are not installed
npx cowsay "Hello"→ Temporarily install and execute cowsay (saved in cache)
In short, npx is like a "launcher that executes packages when needed."
Why use npx with Prisma
It is common to fix the version of the Prisma CLI (prisma) for each project.
If you have installed it with npm install prisma --save-dev,
the CLI will be placed in node_modules/.bin/prisma.
Calling this directly is what npx prisma does.
npx prisma migrate dev→ Execute the project-local Prisma CLI
(does not interfere with the global Prisma)
Summary
・npm = command to manage packages
・npx = command to execute packages
・In Prisma, by using npx prisma,
└ You can call the project-local Prisma CLI
└ You can use it safely without depending on the global environment
💡 Supplementary note
In recent versions of Node.js (v16 and later), npm exec behaves almost the same as npx.
Therefore, I think there will be an increasing number of cases in the future where it is written as follows instead of npx prisma.
npm exec prisma migrate devいいなと思ったら応援しよう!
よろしければ応援お願いします!
いただいたチップはクリエイターとしての活動費や音楽活動(主に地域の学生への教育)に使わせていただきます!