简体   繁体   中英

How can I resolve the webpack "unmet peer dependency" warning for my create-react-app package?

create-react-app (CRA) is a command-line tool that helps you set up a new React app. It creates a package.json that includes a react-scripts dependency.

The react-scripts package has a webpack dependency (currently version 4.19.1 .)

craco is a library that makes it easy to configure create-react-app .

I've released a craco-less package, which is a craco plugin. It makes it easy to set up Less support for create-react-app . The craco-less dependencies include the css-loader and style-loader packages, and these packages have webpack@^4.0.0 in their peerDependencies .

When I install the craco-less package ( yarn add craco-less ), I get the following warnings:

warning "craco-less > css-loader@1.0.1" has unmet peer dependency "webpack@^4.0.0".
warning "craco-less > less-loader@4.1.0" has unmet peer dependency "webpack@^2.0.0 || ^3.0.0 || ^4.0.0".

I tried adding this to my package.json :

"peerDependencies": {
  "webpack": "^4.0.0"
}

But this just gave me an extra warning:

warning "craco-less > css-loader@1.0.1" has unmet peer dependency "webpack@^4.0.0".
warning "craco-less > less-loader@4.1.0" has unmet peer dependency "webpack@^2.0.0 || ^3.0.0 || ^4.0.0".
warning " > craco-less@1.2.2" has unmet peer dependency "webpack@^4.0.0".

I also tried adding webpack as an explicit dependency, and this works 90% of the time, but sometimes causes create-react-app to crash with the error described in this GitHub issue :

npm 包崩溃

(Only seems to happen with npm install , and not yarn install .)

I just realized that this error happens because react-scripts depends on a specific webpack version, and my package has a loose dependency ( ^4.0.0 ). But I would have expected the react-scripts dependency to override this loose version constraint and install 4.19.1 instead of the latest version ( 4.27.1 ). I think this is why it's working with yarn and not with npm , because maybe yarn has smarter dependency resolution.

My library has a dependency on "react-scripts": "^2.1.1" (flexible), so I want to know if there is a way to delegate to their webpack dependency instead of installing the latest version?

It would be a pain to specify 4.19.1 as a hardcoded version, because I would have to update the craco-less library all the time whenever they update the react-scripts package. Or maybe I could write a script to just automate these updates if it can't be done in the package.json .

Thanks!

yarn upgrade webpack@^4.0.0

参考

Same issue here. Had a few devs sink a fair bit of time into this, we eventually swapped over to Yarn. Jenkins required a few tweaks:

Build Environment:

Provide Node & npm bin/ folder to PATH

NodeJS Installation: 8.10 

Version >8

Preface Exe Shell with:

curl -o- -L https://yarnpkg.com/install.sh | bash -s
export PATH=$HOME/.yarn/bin:$PATH

yarn install
yarn run build

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM