阻止 npm 命令转而使用 pnpm
出于常见原因,我想改用 PNPM 而不是 NPM。不幸的是,剪切和粘贴,或者肌肉记忆有时会占据主导地位,我会意外地使用 NPM 在已经使用 PNPM 的项目中安装包。该项目的进展不再顺利。
我希望阻止或别名 NPM 命令以支持 PNMP。
我尝试过的事情:
仅允许 - 尽管在 PNPM 网站上进行了推广,但它并没有似乎有效,至少对于最常见的用例单独导入不起作用。
由于我的终端是Oh My ZSH,我发现代码添加到如果位于包含 PNPM 锁定文件的目录中,则 ~/.zshrc 阻止 npm。
NPM_PATH=$(which npm)
npm () {
if [ -e PNPM-lock.yaml ]
then
echo "Please use PNPM with this project"
elif [ -e yarn.lock ]
then
echo "Please use Yarn with this project"
else
$NPM_PATH "$@"
fi
}
这似乎适合我的目的,但是有人有任何更干净/更少特定于 zsh 的替代方案吗?
如果确实发生这种情况,恢复被 NPM 安装污染的 PNMP 投影的建议步骤是什么?
I would like to switch to using PNPM over NPM for the usual reasons. Unfortunately cut and paste, or muscle memory takes over sometimes and I will accidentally use NPM to install a package in a project that is using already using PNPM. Things do not go well for that project anymore.
I am hoping to block or alias NPM commands in favor of PNMP.
Things I have tried:
Only Allow - although promoted on the PNPM site, it does not seem to work, at least not work for individual imports which is the most common use case.
Since my terminal is Oh My ZSH, I found code to add to ~/.zshrc to block npm if in a directory containing a PNPM lockfile.
NPM_PATH=$(which npm)
npm () {
if [ -e PNPM-lock.yaml ]
then
echo "Please use PNPM with this project"
elif [ -e yarn.lock ]
then
echo "Please use Yarn with this project"
else
$NPM_PATH "$@"
fi
}
This seems to work for my purposes, but does anyone have any cleaner / less zsh-specific alternatives?
And in the case where it does happen, what is the recommended steps to recover a PNMP projected tainted by an NPM install?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您已经提到了 only-allow 包。
NodeJS v16.9.0 和 v14.19.0 支持新的实验性
packageManager
字段package.json 文件。要恢复被 NPM 安装污染的 PNMP 项目,只要您指定了您真正希望它们工作的依赖版本,您只需删除
node_modules
文件夹和 package-lock.json 文件。但您也可以查看 PNPM 的import
子命令You've already mentioned the only-allow package.
NodeJS v16.9.0 and v14.19.0 support a new experimental
packageManager
field in the package.json file.To recover a PNMP projected tainted by an NPM install, as long as you've specified your dependency versions as you'd really like them to work, you could just delete the
node_modules
folder and the package-lock.json file. But you could also take a look at PNPM'simport
subcommand当它是特定于项目的时,您可以使用此 https: //www.freecodecamp.org/news/how-to-force-use-yarn-or-npm/
我一直在使用它,因为我们切换到 PNPM,并且出于某种原因我仍然不断使用纱线。
When it's project-specific you could use this https://www.freecodecamp.org/news/how-to-force-use-yarn-or-npm/
I've been using it because we switched to PNPM, and I was still constantly using yarn for some reason.
根据 这些 文章 就足够了添加到您的 package.json:
我假设您首先需要为此安装
only-allow
包,即使两个教程中都缺少这样的步骤。2.
另外,这篇文章是为 Yarn 编写,但同样适用于 pnpm:
您可以使用以下内容创建/编辑 .npmrc 文件
然后在 package.json
最后,应该指出的是,Node v16+ 支持带有实验性 Corepack 的预期包管理器
我没有测试上述任何内容,但它们都应该防止运行
npm
而不是pnpm
。According to these articles it is enough to add to your package.json:
I assume that you first need to install the
only-allow
package for that, even though such a step is missing from both tutorials.2.
Alternatively, this article was written for Yarn, but should apply to pnpm equally:
You can create/edit a .npmrc file with the following
And then in package.json
Finally, it should be pointed out that Node v16+ supports a expected package manager with the experimental Corepack
I have not tested any of the above, but they should all prevent running
npm
instead ofpnpm
.我在我的
.zshrc
文件中添加了以下名为 npm 的函数。此函数测试您是否提到
pnpm
作为您的“packageManager”,在这种情况下它使用 pnpm,否则默认为新项目管理I have added the following function called npm in my
.zshrc
fileThis function tests if you have mentioned
pnpm
as your "packageManager" in which case it uses pnpm, otherwise defaults to npm编辑 package.json 文件,添加以下内容:
create .npmrc
edit package.json file, add this:
create .npmrc
我来这里是为了寻找你同样问题的好答案;我真正想要的是一个单独的别名,它只使用项目已经使用的任何东西。 我发现这篇文章确实符合我的需求*,并认为它也可能适合您的需求。
但是我希望我的工作方式有点不同。我的主要用例是我希望能够输入
run dev
并让它知道pnpm dev
或npm run dev
。仅供参考,这应该适用于所有包管理器的
scripts
对象中的任何命令;它也适用于大多数包管理器上的基本命令,例如run install
。仅当您尝试将run install
或其他非脚本命令与 NPM 结合使用时,它才会失败。 (因为它尝试执行npm run install
,但失败了。)I came here looking for a good answer to your same question; what I really wanted was a single alias that would just use whatever the project is already using. I found this article really matched my need*, and thought it might fit yours too.
But I wanted mine to work a bit differently. My main use case is I want to be able to type
run dev
and have it know eitherpnpm dev
ornpm run dev
.FYI This should work for any commands in your
scripts
object for all the package managers; and it works for base commands likerun install
on most of the package managers too. It only fails when you are trying to userun install
or other non-script commands with NPM. (Because it tries to donpm run install
, which fails.)您可以使用
only-allow
preinstall
进行安装时预执行preupdate
进行更新时预执行安装时您会收到此错误或使用 pnpm 以外的任何包管理器更新包
在此项目中使用“pnpm install”进行安装。
You can use
only-allow
preinstall
pre-executes when doing an installpreupdate
pre-executes when doing an updateYou will get this error when installing or updating a package using any package manager other than pnpm
Use "pnpm install" for installation in this project.