安装react-instagram-embed包后出现「Module not found: Error: Can't resolve 'querystring'」错误,如何解决?
Hey there, I’ve run into this exact issue before—let’s break down how to fix it quickly.
The error pops up because webpack 5 no longer includes polyfills for Node.js core modules by default (webpack < 5 used to handle this automatically). The finnhub package (a dependency of react-instagram-embed) is trying to access the querystring module, which webpack 5 doesn’t provide out of the box anymore.
You’ve got two clear solutions depending on whether you need the querystring functionality or not:
Option 1: Add the polyfill (if you need querystring)
If your app or its dependencies rely on querystring to work properly, follow these steps:
- First, install the polyfill package:
npm install querystring-es3 --save # Or with yarn: yarn add querystring-es3 - Then, update your webpack configuration file (usually
webpack.config.js) to add the fallback forquerystring:module.exports = { // ... your existing webpack config resolve: { fallback: { "querystring": require.resolve("querystring-es3") } } };
Option 2: Disable the module (if you don’t need querystring)
If you’re certain your app doesn’t depend on the querystring module, you can tell webpack to ignore it entirely:
- Update your webpack config with this fallback setting:
module.exports = { // ... your existing webpack config resolve: { fallback: { "querystring": false } } };
Note for Create React App users
If you’re using Create React App (which hides the webpack config by default), you’ll need a tool like craco or react-app-rewired to modify the config. For example, with craco:
- Install craco:
npm install @craco/craco --save - Create a
craco.config.jsfile in your project root with the sameresolve.fallbackconfiguration shown above. - Update your
package.jsonscripts to use craco instead of react-scripts (e.g., replace"start": "react-scripts start"with"start": "craco start").
内容的提问来源于stack exchange,提问作者payal_harwani

