阻止 npm 命令转而使用 pnpm

发布于 2025-01-13 16:50:46 字数 950 浏览 0 评论 0原文

出于常见原因,我想改用 PNPM 而不是 NPM。不幸的是,剪切和粘贴,或者肌肉记忆有时会占据主导地位,我会意外地使用 NPM 在已经使用 PNPM 的项目中安装包。该项目的进展不再顺利。

我希望阻止或别名 NPM 命令以支持 PNMP。

我尝试过的事情:

  1. 仅允许 - 尽管在 PNPM 网站上进行了推广,但它并没有似乎有效,至少对于最常见的用例单独导入不起作用。

  2. 由于我的终端是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:

  1. 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.

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

流云如水 2025-01-20 16:50:46

您已经提到了 only-allow 包。

NodeJS v16.9.0 和 v14.19.0 支持新的实验性 packageManager 字段package.json 文件

类型:<字符串>

<代码>{
  "packageManager": "<包管理器名称>@<版本>"
}

"packageManager" 字段定义在处理当前项目时预期使用哪个包管理器。它可以设置为任何支持的包管理器,并且将确保您的团队使用完全相同的包管理器版本,无需安装除 Node.js 之外的任何其他内容。

该字段目前处于实验阶段,需要选择加入;有关该过程的详细信息,请查看 Corepack 页面。

要恢复被 NPM 安装污染的 PNMP 项目,只要您指定了您真正希望它们工作的依赖版本,您只需删除 node_modules 文件夹和 package-lock.json 文件。但您也可以查看 PNPM 的 import 子命令

pnpm import 从另一个包管理器的锁定文件生成 pnpm-lock.yaml [...]

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.

Type: <string>

{
  "packageManager": "<package manager name>@<version>"
}

The "packageManager" field defines which package manager is expected to be used when working on the current project. It can be set to any of the supported package managers, and will ensure that your teams use the exact same package manager versions without having to install anything else other than Node.js.

This field is currently experimental and needs to be opted-in; check the Corepack page for details about the procedure.

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's import subcommand

pnpm import generates a pnpm-lock.yaml from another package manager's lockfile [...]

青柠芒果 2025-01-20 16:50:46

当它是特定于项目的时,您可以使用此 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.

皇甫轩 2025-01-20 16:50:46

根据 这些 文章 就足够了添加到您的 package.json:

"scripts": {
  "preinstall": "npx only-allow pnpm", 
  ...
}

我假设您首先需要为此安装 only-allow 包,即使两个教程中都缺少这样的步骤。

2.

另外,这篇文章是为 Yarn 编写,但同样适用于 pnpm:
您可以使用以下内容创建/编辑 .npmrc 文件

engine-strict = true

然后在 package.json

"engines": {
   "npm": "please-use-pnpm",
   "pnpm": ">= 2.0.0"
},

最后,应该指出的是,Node v16+ 支持带有实验性 Corepack 的预期包管理器

corepack prepare pnpm@latest --activate

我没有测试上述任何内容,但它们都应该防止运行 npm 而不是 pnpm

According to these articles it is enough to add to your package.json:

"scripts": {
  "preinstall": "npx only-allow pnpm", 
  ...
}

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

engine-strict = true

And then in package.json

"engines": {
   "npm": "please-use-pnpm",
   "pnpm": ">= 2.0.0"
},

Finally, it should be pointed out that Node v16+ supports a expected package manager with the experimental Corepack

corepack prepare pnpm@latest --activate

I have not tested any of the above, but they should all prevent running npm instead of pnpm.

冷血 2025-01-20 16:50:46

我在我的 .zshrc 文件中添加了以下名为 npm 的函数。


npm () {
    bash -c "if grep -sq 'pnpm' package.json; then pnpm $@; else npm $@; fi"
}

此函数测试您是否提到 pnpm 作为您的“packageManager”,在这种情况下它使用 pnpm,否则默认为新项目管理

I have added the following function called npm in my .zshrc file


npm () {
    bash -c "if grep -sq 'pnpm' package.json; then pnpm $@; else npm $@; fi"
}

This function tests if you have mentioned pnpm as your "packageManager" in which case it uses pnpm, otherwise defaults to npm

在你怀里撒娇 2025-01-20 16:50:46

编辑 package.json 文件,添加以下内容:

"engines": {
    "npm": "please-use-pnpm",
    "yarn": "please-use-pnpm",
    "pnpm": ">= 8.0.0"
},

create .npmrc

engine-strict = true

edit package.json file, add this:

"engines": {
    "npm": "please-use-pnpm",
    "yarn": "please-use-pnpm",
    "pnpm": ">= 8.0.0"
},

create .npmrc

engine-strict = true
吃颗糖壮壮胆 2025-01-20 16:50:46

我来这里是为了寻找你同样问题的好答案;我真正想要的是一个单独的别名,它只使用项目已经使用的任何东西。 我发现这篇文章确实符合我的需求*,并认为它也可能适合您的需求。

但是我希望我的工作方式有点不同。我的主要用例是我希望能够输入 run dev 并让它知道 pnpm devnpm run dev

run() {
  if [[ -f pnpm-lock.yaml ]]; then
    command pnpm "$@"
  elif [[ -f bun.lockb ]]; then
    command bun "$@"
  elif [[ -f yarn.lock ]]; then
    command yarn "$@"
  elif [[ -f package-lock.json ]]; then
    command npm "run $@"
  else
    command pnpm "$@"
  fi
}

仅供参考,这应该适用于所有包管理器的 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 either pnpm dev or npm run dev.

run() {
  if [[ -f pnpm-lock.yaml ]]; then
    command pnpm "$@"
  elif [[ -f bun.lockb ]]; then
    command bun "$@"
  elif [[ -f yarn.lock ]]; then
    command yarn "$@"
  elif [[ -f package-lock.json ]]; then
    command npm "run $@"
  else
    command pnpm "$@"
  fi
}

FYI This should work for any commands in your scripts object for all the package managers; and it works for base commands like run install on most of the package managers too. It only fails when you are trying to use run install or other non-script commands with NPM. (Because it tries to do npm run install, which fails.)

瘫痪情歌 2025-01-20 16:50:46

您可以使用 only-allow

"scripts": {
  "preinstall": "npx --yes only-allow pnpm",
  "preupdate": "npx --yes only-allow pnpm",
  ...
}

preinstall 进行安装时预执行

preupdate 进行更新时预执行

安装时您会收到此错误或使用 pnpm 以外的任何包管理器更新包

在此项目中使用“pnpm install”进行安装。

在此处输入图像描述

You can use only-allow

"scripts": {
  "preinstall": "npx --yes only-allow pnpm",
  "preupdate": "npx --yes only-allow pnpm",
  ...
}

preinstall pre-executes when doing an install

preupdate pre-executes when doing an update

You 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.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文