attempted to import the Node standard library module "node:stream" in React Native project
Previously, I was able to use Cheerio in one of my React Native applications. However after the recent release of v1.0.0 I receive an error upon launch due to it trying to import a NodeJS exclusive library:
The package at "node_modules/cheerio/dist/commonjs/index.js" attempted to import the Node standard library module "node:stream".
It failed because the native React runtime does not include the Node standard library.
expo-env-info output:
expo-env-info 1.2.0 environment info:
System:
OS: macOS 15.1
Shell: 5.9 - /bin/zsh
Binaries:
Node: 18.20.4 - ~/.nvm/versions/node/v18.20.4/bin/node
npm: 10.7.0 - ~/.nvm/versions/node/v18.20.4/bin/npm
Watchman: 2024.07.29.00 - /opt/homebrew/bin/watchman
Managers:
CocoaPods: 1.15.2 - /opt/homebrew/bin/pod
SDKs:
iOS SDK:
Platforms: DriverKit 24.0, iOS 18.0, macOS 15.0, tvOS 18.0, visionOS 2.0, watchOS 11.0
IDEs:
Android Studio: 2024.1 AI-241.18034.62.2411.12071903
Xcode: 16.0/16A5211f - /usr/bin/xcodebuild
npmPackages:
expo: ~51.0.26 => 51.0.26
expo-router: ~3.5.21 => 3.5.21
react: 18.2.0 => 18.2.0
react-dom: 18.2.0 => 18.2.0
react-native: 0.74.3 => 0.74.3
Expo Workflow: managed
```
What is the last working version? I had the same error too!
+2
I was able to downgrade to 1.0.0-rc.12 as a workaround. It’s an older version but it seems to work fine.
On Aug 14, 2024, at 09:37, 77TecShaeer @.***> wrote:
+2
— Reply to this email directly, view it on GitHub https://github.com/cheeriojs/cheerio/issues/4005#issuecomment-2288967956, or unsubscribe https://github.com/notifications/unsubscribe-auth/AIM3YKVA2OSCKQVSJVIXVJLZRNTSBAVCNFSM6AAAAABMMP27C6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEOBYHE3DOOJVGY. You are receiving this because you authored the thread.
Same here, I get the exact same issue in react native:
The package at "node_modules/cheerio/dist/commonjs/index.js" attempted to import the Node standard library module "node:stream". It failed because the native React runtime does not include the Node standard library.
In my use case, I simply want to extract data from an HTML string that I previously retrieved using fetch().
I was able to solve this in my React Native app by using the cheerio/slim export instead, as it doesn't rely on Node.js - at least not in the latest version 1.1.2
- import * as cheerio from 'cheerio';
+ import * as cheerio from 'cheerio/slim';
This probably won't be sufficient for some advanced use cases as cheerio/slim only exports load(...), but for all others, this could certainly be a solution without using the outdated RC version.
Additionally, but only in case you activated disableHierarchicalLookup in your metro.config.js:
htmlparser2 has a nested node_modules folder that needs to be declared in nodeModulesPaths.
For example, I had to do this in my project due to a mono-repo setup with Yarn workspaces.
// metro.config.js
resolver: {
disableHierarchicalLookup: true, // only if you already do this
nodeModulesPaths: [
...
path.resolve(workspaceRoot, 'node_modules/htmlparser2/node_modules'), // then you'll need this aswell
],
}
In my use case, I simply want to extract data from an HTML string that I previously retrieved using
fetch(). I was able to solve this in my React Native app by using thecheerio/slimexport instead, as it doesn't rely on Node.js - at least not in the latest version 1.1.2
- import * as cheerio from 'cheerio';
- import * as cheerio from 'cheerio/slim'; This probably won't be sufficient for some advanced use cases as
cheerio/slimonly exportsload(...), but for all others, this could certainly be a solution without using the outdated RC version.Additionally, but only in case you activated
disableHierarchicalLookupin yourmetro.config.js: htmlparser2 has a nested node_modules folder that needs to be declared innodeModulesPaths. For example, I had to do this in my project due to a mono-repo setup with Yarn workspaces.// metro.config.js resolver: { disableHierarchicalLookup: true, // only if you already do this nodeModulesPaths: [ ... path.resolve(workspaceRoot, 'node_modules/htmlparser2/node_modules'), // then you'll need this aswell ], }
the workaround cause the same issue with 1.1.2 and expo53. Cheerio need better support for web cheerio/web to avoid relying on node packages
In my use case, I simply want to extract data from an HTML string that I previously retrieved using
fetch(). I was able to solve this in my React Native app by using thecheerio/slimexport instead, as it doesn't rely on Node.js - at least not in the latest version 1.1.2
import * as cheerio from 'cheerio';
import * as cheerio from 'cheerio/slim'; This probably won't be sufficient for some advanced use cases as
cheerio/slimonly exportsload(...), but for all others, this could certainly be a solution without using the outdated RC version.Additionally, but only in case you activated
disableHierarchicalLookupin yourmetro.config.js: htmlparser2 has a nested node_modules folder that needs to be declared innodeModulesPaths. For example, I had to do this in my project due to a mono-repo setup with Yarn workspaces. // metro.config.js resolver: { disableHierarchicalLookup: true, // only if you already do this nodeModulesPaths: [ ... path.resolve(workspaceRoot, 'node_modules/htmlparser2/node_modules'), // then you'll need this aswell ], }the workaround cause the same issue with 1.1.2 and expo53. Cheerio need better support for web
cheerio/webto avoid relying on node packages
it works with the cheerio/slim, but maybe you need a rebuild and restart metro