Difference between npx and npm?
Difference between npx and npm?
Introducing npx: an npm package runner
NP
M
- Manages packages but doesn't make life easy executing any.
NPX
- A tool for executing Node packages.
NPX
comes bundled withNPM
version5.2+
NPM
by itself does not simply run any package. it doesn't run any package in a matter of fact. If you want to run a package using NPM, you must specify that package in your package.json
file.
When executables are installed via NPM packages, NPM links to them:
- local installs have "links" created at
./node_modules/.bin/
directory. - global installs have "links" created from the global
bin/
directory (e.g./usr/local/bin
) on Linux or at%AppData%/npm
on Windows.
NPM:
One might install a package locally on a certain project:
npm install some-package
Now let's say you want NodeJS to execute that package from the command line:
$ some-package
The above will fail. Only globally installed packages can be executed by typing their name only.
To fix this, and have it run, you must type the local path:
$ ./node_modules/.bin/some-package
You can technically run a locally installed package by editing your packages.json
file and adding that package in the scripts
section:
{
"name": "whatever",
"version": "1.0.0",
"scripts": {
"some-package": "some-package"
}
}
Then run the script using npm run-script
(or npm run
):
npm run some-package
NPX:
npx
will check whether <command>
exists in $PATH
, or in the local project binaries, and execute it. So, for the above example, if you wish to execute the locally-installed package some-package
all you need to do is type:
npx some-package
Another major advantage of npx
is the ability to execute a package which wasn't previously installed:
$ npx create-react-app my-app
The above example will generate a react
app boilerplate within the path the command had run in, and ensures that you always use the latest version of a generator or build tool without having to upgrade each time you’re about to use it.
Use-Case Example:
npx
command may be helpful in the script
section of a package.json
file, when it is unwanted to define a dependency which might not be commonly used or any other reason:
"scripts": {
"start": "npx gulp@3.9.1",
"serve": "npx http-server"
}
Call with: npm run serve
Related questions:
- How to use package installed locally in node_modules?
- NPM: how to source ./node_modules/.bin folder?
- How do you run a js file using npm scripts?
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2019-01-25 Autofac is designed to track and dispose of resources for you.
2019-01-25 IIS Manager could not load type for module provider 'SharedConfig' that is declared in administration.config
2019-01-25 How to create and manage configuration backups in Internet Information Services 7.0