乐闻世界logo
搜索文章和话题

Npx 和 npm 的区别?

7 个月前提问
3 个月前修改
浏览次数228

6个答案

1
2
3
4
5
6

npx 和 npm 之间的区别?npxnpm 都是 Node.js 环境中常用的工具,它们在 Node.js 和 JavaScript 生态系统中扮演着关键的角色。以下是它们之间的一些主要区别:

npm (Node Package Manager):

  • Package 管理器:npm 是 Node.js 默认的包管理器,用来安装、更新和管理项目中的依赖包。
  • 全局安装:npm 可以全局安装包,这样你就可以在命令行中任何位置使用这些包。
  • 本地安装:npm 也可用来在特定项目中安装包,通常这些包会被放在项目的 node_modules 文件夹中。
  • 脚本运行:npm 还可以运行定义在 package.json 文件中的脚本。
  • 版本管理:npm 通过 package.jsonpackage-lock.json 文件帮助管理包的版本。
  • 包发布:npm 可用于发布和更新包到 npm registry。

npx (Node Package Execute):

  • 执行包:npx 用来执行在 npm registry 中的包,无需手动下载或者安装。
  • 一次性命令:npx 非常适合一次性使用命令,它可以在不全局安装包的情况下执行包的二进制文件。
  • 即时安装执行:npx 会在本地或者全局找不到命令的时候,自动从 npm registry 安装包并立即执行。
  • 避免全局污染:npx 避免了全局安装多个包可能导致的版本冲突或环境污染问题。
  • 测试不同版本:npx 可以用来轻松地测试不同版本的包,而不需要更改项目中的依赖。

简而言之,npm 主要用作包的安装和管理工具,而 npx 是一个辅助工具,用于执行包中的命令,特别是在不想或不需要永久安装这些包的情况下。这两个工具经常一起使用,以更有效地开发和管理 Node.js 项目。

2024年6月29日 12:07 回复

Introducing npx: an npm package runner

NPM - Manages packages but doesn't make life easy executing any.

NPX - A tool for executing Node packages.

NPX comes bundled with NPM version 5.2+

NPM by itself does not simply run any package. It doesn't run any package as 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:

  1. local installs have "links" created at ./node_modules/.bin/ directory.
  2. global installs have "links" created from the global bin/ directory (e.g. /usr/local/bin) on Linux or at %AppData%/npm on Windows.

Documentation you should read


NPM:

One might install a package locally on a certain project:

shell
npm install some-package

Now let's say you want NodeJS to execute that package from the command line:

shell
$ 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:

shell
$ ./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:

shell
{ "name": "whatever", "version": "1.0.0", "scripts": { "some-package": "some-package" } }

Then run the script using npm run-script (or npm run):

shell
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:

shell
npx some-package

Another major advantage of npx is the ability to execute a package which wasn't previously installed:

shell
$ 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:

shell
"scripts": { "start": "npx gulp@3.9.1", "serve": "npx http-server" }

Call with: npm run serve


  1. How to use package installed locally in node_modules?
  2. NPM: how to source ./node_modules/.bin folder?
  3. How do you run a js file using npm scripts?
2024年6月29日 12:07 回复

npx is a npm package runner (x probably stands for eXecute). One common way to use npx is to download and run a package temporarily or for trials.

create-react-app is an npm package that is expected to be run only once in a project's lifecycle. Hence, it is preferred to use npx to install and run it in a single step.

As mentioned in the main page https://www.npmjs.com/package/npx, npx can run commands in the PATH or from node_modules/.bin by default.

Note: With some digging, we can find that create-react-app points to a Javascript file (possibly to /usr/lib/node_modules/create-react-app/index.js on Linux systems) that is executed within the node environment. This is simply a global tool that does some checks. The actual setup is done by react-scripts, whose latest version is installed in the project. Refer https://github.com/facebook/create-react-app for more info.

2024年6月29日 12:07 回复

NPM is a package manager, you can install node.js packages using NPM

NPX is a tool to execute node.js packages.

It doesn't matter whether you installed that package globally or locally. NPX will temporarily install it and run it. NPM also can run packages if you configure a package.json file and include it in the script section.

So remember this, if you want to check/run a node package quickly without installing locally or globally use NPX.

npM - Manager

npX - Execute - easy to remember

2024年6月29日 12:07 回复

npm - Package manager for JavaScript, just like: pip (Python), Maven (Java), NuGet (.NET), Composer (PHP), RubyGems (Ruby), ...

npx - runs a command of a package without installing it explicitly.

Use cases:

  • You don't want to install packages neither globally nor locally.
  • You don't have permission to install it globally.
  • Just want to test some commands.
  • Sometime, you want to have a script command (generate, convert something, ...) in package.json to execute something without installing these packages as project's dependencies.

Syntax:

shell
npx [options] [-p|--package <package>] <command> [command-arg]...

Package is optional:

shell
npx -p uglify-js uglifyjs --output app.min.js app.js common.js +----------------+ +--------------------------------------------+ package (optional) command, followed by arguments

For example:

shell
Start a HTTP Server : npx http-server Lint code : npx eslint ./src # Run uglifyjs command in the package uglify-js Minify JS : npx -p uglify-js uglifyjs -o app.min.js app.js common.js Minify CSS : npx clean-css-cli -o style.min.css css/bootstrap.css style.css Minify HTML : npx html-minifier index-2.html -o index.html --remove-comments --collapse-whitespace Scan for open ports : npx evilscan 192.168.1.10 --port=10-9999 Cast video to Chromecast : npx castnow http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerFun.mp4

More about command:

2024年6月29日 12:07 回复

NPX:

From https://www.futurehosting.com/blog/npx-makes-life-easier-for-node-developers-plus-node-vulnerability-news/:

Web developers can have dozens of projects on their development machines, and each project has its own particular set of npm-installed dependencies. A few years back, the usual advice for dealing with CLI applications like Grunt or Gulp was to install them locally in each project and also globally so they could easily be run from the command line.

But installing globally caused as many problems as it solved. Projects may depend on different versions of command line tools, and polluting the operating system with lots of development-specific CLI tools isn’t great either. Today, most developers prefer to install tools locally and leave it at that.

Local versions of tools allow developers to pull projects from GitHub without worrying about incompatibilities with globally installed versions of tools. NPM can just install local versions and you’re good to go. But project specific installations aren’t without their problems: how do you run the right version of the tool without specifying its exact location in the project or playing around with aliases?

That’s the problem npx solves. A new tool included in NPM 5.2, npx is a small utility that’s smart enough to run the right application when it’s called from within a project.

If you wanted to run the project-local version of mocha, for example, you can run npx mocha inside the project and it will do what you expect.

A useful side benefit of npx is that it will automatically install npm packages that aren’t already installed. So, as the tool’s creator Kat Marchán points out, you can run npx benny-hill without having to deal with Benny Hill polluting the global environment.

If you want to take npx for a spin, update to the most recent version of npm.

2024年6月29日 12:07 回复

你的答案