Search code examples
javascriptreact-nativenpmnpm-installjsdom

How to install jsdom in React Native


Steps to reproduce:

  1. react-native init myproject.
  2. cd myproject.
  3. npm install jsdom.
  4. In my App.js, I added import jsdom from 'jsdom';.
  5. react-native run-ios.

Expected Output: App starts on the React native landing page.

Actual Output: Unable to resolve module path from /Users/davidangulo/Desktop/mobile/myproject/node_modules/jsdom/lib/api.js: Module path does not exist in the Haste module map

According to their readme: (https://github.com/jsdom/jsdom)

const jsdom = require("jsdom");
const { JSDOM } = jsdom;

That is how you initialize it but unfortunately, it doesn't work.


Solution

  • Hey I had the same problem with jsdom and react-native, jsdom is made for node which would include path, fs and more, but react-native does not have all this.

    For me jsdom-jscore did the trick:

    import { jsdom } from 'jsdom-jscore-rn'
    
    // ...
    
    const dom = jsdom(html)