contentful / contentful.js

JavaScript library for Contentful's Delivery API (node & browser)
https://contentful.github.io/contentful.js
MIT License
1.18k stars 197 forks source link

Contentful is no longer working with react-native 0.69.6 #1873

Open polcats opened 1 year ago

polcats commented 1 year ago

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

  1. Create a new react-native app
  2. Install contentful
  3. 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

wmcbain commented 3 months ago

@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);
    },
  },
};