在日常开发中,如果遇到要强制使用包管理工具比如pnpm有什么好的办法吗?
    通过搜索和Vue的管理中找到了一个方法:

    通过这个问答,了解到Lifecycle of npm install

    It is described on the NPM page: npm-scripts. It is a bit cryptic, but the logic is straightforward. E.g. running npm install will do preinstall install postinstall prepublish - this is rather exceptional case, prepublish only runs if there are no arguments, i.e. run locally. Also check which version of npm you are running, as prepublish with local install has been deprecated in 4.x in favour of another approach, described in issue 10074 and has a nice explanation in this blog. It comes down to the fact that npm install without arguments runs when you clone a package and it makes sense to prepare it. But people dislike this behaviour, so it was decided to split prepublish into two stages. prepare runs instead of prepublish during publishing and local npm install. prepublishOnly runs only with npm publish. Hence npm publish will do prepublish publish postpublish or prepare prepublishOnly publish postpublish on 4.x Finally, with version 6.x, npm install runs preinstall install postinstall prepare while npm publish runs prepare prepublish publish postpublish. As far as I know, all other commands follow the logic of preX, X, postX.

    简单翻译如下:

    它在NPM的页面上有描述:npm-scripts。它有点晦涩难懂,但逻辑很简单。 例如,运行npm install会进行preinstall install postinstall prepublish - 这是相当特殊的情况,prepublish只在没有参数的情况下运行,即在本地运行。还要检查你运行的是哪个版本的npm,因为prepublish与本地安装在4.x版本中已经被废弃,而采用另一种方法,在10074期中有描述,在这个博客中有一个很好的解释。归根结底,当你克隆一个软件包时,npm install不需要参数就可以运行,而且准备它是有意义的。但是人们不喜欢这种行为,所以决定将prepublish分成两个阶段。prepare在发布和本地npm安装时代替prepublish运行。prepublishOnly只在npm publish时运行。 因此,在4.x版本上,npm publish会进行prepublish publish postpublish或prepare prepublishOnly publish postpublish。 最后,在6.x版本中,npm install运行preinstall install postinstall prepare,而npm publish运行prepare prepublish publish postpublish。 据我所知,所有其他命令都遵循preX、X、postX的逻辑。

    通过www.DeepL.com/Translator(免费版)翻译

    大致意思就是,在执行npm install时会执行preinstall,这样既可以在这里做文章了。
    package.json scripts里面加上

    1. {
    2. "scripts": {
    3. "preinstall": "node ./scripts/preinstall.js",
    4. }
    5. }
    1. if (!/pnpm/.test(process.env.npm_execpath || '')) {
    2. console.warn(
    3. `\u001b[33mThis repository requires using pnpm as the package manager ` +
    4. ` for scripts to work properly.\u001b[39m\n`
    5. )
    6. process.exit(1)
    7. }

    npm install时会运行这个preinstall,虽然npm文档规定了,但是npm没实现,惊喜吧,yarn实现了

    效果如图:

    image.png
    这样就可以强制用户使用pnpm了!