跳到主要内容

yarn dlx

yarn dlx是为了解决yarn1.x时期的历史遗留问题。

因为yarn2之后,系统范围里安装的包已经不在yarn的管理范围了,但是为了可以运行1.x时期在全局安装过的包,就设计了这个命令。

https://yarnpkg.com/getting-started/migration/#use-yarn-dlx-instead-of-yarn-global

yarn dlx is designed to execute one off scripts that may have been installed as global packages with yarn 1.x. Managing system-wide packages is outside of the scope of yarn. To reflect this, yarn global has been removed. Read more on GitHub.

这个命令会下载并运行包,它下载的东西不会保留在系统或者package.json里。

详情还是请看一下官方文档。 https://yarnpkg.com/cli/dlx

This command will install a package within a temporary environment, and run its binary script if it contains any. The binary will run within the current cwd.
这个命令会在临时环境里安装包,并且会运行包里的二进行脚本。脚本将会在当前工作目录下运行。

我拿hexo来试验了一下。

hexo项目的package.json大概是这样的

{
"scripts": {
"build": "hexo generate",
"clean": "hexo clean",
"deploy": "hexo deploy",
"server": "hexo server"
},
"dependencies": {
"hexo": "^6.1.0",
...
}
}

如果要生成静态文件,则可以运行

yarn run build

或者可以像npx一样直接运行本地包

yarn run hexo generate

甚至可以将run省略

yarn hexo generate

hexo的官方文档里说可以通过全局安装hexo-cli来运行hexo命令,

npm install -g hexo-cli

但是yarn现在已经没有全局安装了,也没有必要在全局装这个,像上面运行本地包就行。

如果你非得想用这个,可以这样

yarn dlx -p hexo-cli hexo generate