contentful.js
contentful.js copied to clipboard
Contentful is no longer working with react-native 0.69.6
Expected Behavior
Importing contentful
library in a react-native app works without errors
Actual Behavior
Contentful is trying to import a Node functionality:
The package at "../../node_modules/contentful/dist/contentful.node.js" attempted to import the Node standard library module "fs".
Possible Solution
Find a workaround to avoid fs
?
Steps to Reproduce
- Create a new react-native app
- Install contentful
- Import contentful
Context
We are working on a monorepo project that uses NextJS and react-native. The contentful library is already getting used in the NextJS app. The ideal situation is that we can also use the library in react-native with minimal changes.
Environment
- Language Version: v18.12.1
- Package Manager Version: 8.19.2
- Browser Version: N/A
- Operating System: Linux DESKTOP-80O6G0P 5.15.90.1-microsoft-standard-WSL2
- Package Version: 9.2.17
- Which API are you using?: Delivery
@polcats I was able to solve this by adding a custom resolver to metro.config.js:
const blacklistedModules = [
'fs',
'http',
'https',
'net',
'tls',
'child_process',
'cluster',
'path',
'stream',
'tty',
'zlib',
];
module.exports = {
...config,
...your_settings_here,
resolver: {
...resolver,
...your_settings_here,
resolveRequest: (context, moduleName, platform) => {
if (blacklistedModules.includes(moduleName)) {
return {
type: 'empty',
};
}
return context.resolveRequest(context, moduleName, platform);
},
},
};