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

PNPM 如何使用 npx ?

6 个月前提问
4 个月前修改
浏览次数113

5个答案

1
2
3
4
5

pnpm环境中,确实可以使用相似于npmnpx命令的功能。npxnpm的一个工具,允许用户运行在没有全局安装的情况下的包。pnpm有一个类似的命令叫pnpx,但从pnpm版本6开始,pnpx被标记为废弃,转而推荐直接使用pnpm dlx

pnpm dlx命令的作用类似于npx,它允许你执行一个在本地未安装的npm包。这样你可以临时运行一个命令,而不需要将其添加到项目的依赖中。举个例子,如果你希望运行create-react-app来初始化一个新的React项目,你可以使用下面的命令:

sh
pnpm dlx create-react-app my-app

这条命令将会临时安装create-react-app并立刻在my-app目录下创建一个新的React应用,而无需将create-react-app作为全局或项目的依赖。

使用pnpm dlx的好处之一是,它会使用pnpm的存储方式,即使是临时安装,也会利用pnpm的缓存和存储机制,有效地节省磁盘空间并加快后续的执行速度。

2024年6月29日 12:07 回复

The are are two key uses of npx.

Running executables inside your downloaded dependencies

For example npx jest.

The pnpm equivalent is pnpm exec jest.

Running executable commands in packages you want to download transiently

For example npx create-react-app my-app.

The pnpm equivalent of this is pnpm dlx create-react-app my-app.

Note: There used to be a pnpx command, but it has been deprecated.

2024年6月29日 12:07 回复

Comment from the addition of dlx:

npx is also kind of deprecated and replaced by npm exec.

Which probably refers to npx being deprecated as standalone package and inclusion into npm itself by use of npm exec . The main difference is now that

npm exec supports -- to end argument parsing for itself and count following args to the package to be run.

Whereas npx requires all args to itself to be given before the package to be run and couts all following arguments to the latter.

2024年6月29日 12:07 回复

Short answer yes. npx create-next-app@latest --use-pnpm

2024年6月29日 12:07 回复

你的答案