gpt4 book ai didi

reactjs - npm postinstall 仅在未安装任何内容时运行

转载 作者:行者123 更新时间:2023-12-03 18:00:59 24 4
gpt4 key购买 nike

我错了

原因npm install whatever删除 node_modules/-文件夹不同:我认为它运行 preinstall但没有postinstall我错了。

我没有运行这两个脚本。

原因似乎是npm install whatever还删除了我的 package.json 中未提及的所有软件包.有趣的是,它不会安装我的 package.json 中提到的软件包。但在 node_modules 中缺失.就像迈克尔沃德尔在评论中写的一样。这对我来说毫无意义,但是......

关注 this comment ,我切换到绝对导入并依靠 postinstall(在我自己的 package.json 中)来创建链接

{
"scripts": {
"preinstall": "rm node_modules/-",
"postinstall": "ln -s `pwd`/src node_modules/-",
...
},
}

它工作得很好,除非我真的安装了一些东西。和
npm install

该链接被删除,然后重新创建。然而,随着
npm install whatever

链接被删除,但没有重新创建。这是一个错误还是故意的,我应该使用其他一些“安装后”?

请注意,我不担心丢失我的 src目录。那是一个不同的问题。我的问题是“安装后”只运行有时。

更新

人们评论说只有在运行 npm install 时才会运行 Hook 。没有任何论据。我可以忍受,但事实并非如此:
> ls -l node_modules/-
lrwxrwxrwx 1 maaartinus maaartinus 40 May 29 11:53 node_modules/- -> /home/maaartinus/work/octopus/reocto/src

> npm i whatever
foo bar baz blah

> ls -l node_modules/-
ls: cannot access 'node_modules/-': No such file or directory

这意味着 preinstall运行,但 postinstall没有。这听起来像一个错误...
> node -v
v12.16.3
> npm -v
6.13.4

更新 2

安装前和安装后脚本在我自己的 package.json 中(这可以从上下文中理解,但我应该说清楚)。

最佳答案

有时节点由于某种原因currpted,因此它删除了node_module文件夹。你可以通过 yarn 安装任何东西

yarn add whatever

preinstall & postinstall

preinstall:在安装包之前运行 install

postinstall:安装包后运行。

绝对路径:

安装 babel-plugin-module-resolver经过
yarn add -D babel-plugin-module-resolver
yarn add -D customize-cra

然后在 babel.config.js 中添加相对路径像这样的文件
const { override, addBabelPlugins } = require('customize-cra');
module.exports = override(

...addBabelPlugins([
'module-resolver',
{
alias : {
'@components' : './src/components',
}
}
])
);


那么你可以使用这样的绝对路径
import TextLabel from '@components/textLabel/index.js'

关于reactjs - npm postinstall 仅在未安装任何内容时运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61823406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com