React Native中用rn-nodeify配置crypto后遇RNRando...未定义错误求助
Hey there, I’ve dealt with this exact headache when setting up crypto dependencies in React Native iOS projects. Let’s work through fixing this, building on the steps you’ve already taken:
First, let’s recap what you’ve done so far (since that context helps):
- Created your project with
create-react-native-app proj && cd proj && npm i - Installed crypto-related packages
- Fixed initial crypto dependency issues by adding
import './shim.js'to App.js after hittingreact-native linkfailures
Now, to squash that RNRandomBytes (I’m guessing that’s the full missing object reference) undefined error when running yarn run ios, here’s what to do next:
Step 1: Manually link react-native-randombytes
Auto-linking didn’t work here, so we’ll set this up manually in Xcode:
- Open your project in Xcode by launching
proj/ios/proj.xcworkspace - In the Project Navigator, right-click your project name and select Add Files to "proj"...
- Navigate to
node_modules/react-native-randombytes/RCTRandomBytes.xcodeprojand add it to your project - Head to your project’s Build Phases tab, expand the Link Binary With Libraries section
- Click the + button, find
libRCTRandomBytes.a, and add it to the list
Step 2: Update your Podfile (if using CocoaPods)
If your project uses CocoaPods, you need to ensure the pod is properly included:
- Open
ios/Podfile - Add this line inside your target block:
pod 'react-native-randombytes', :path => '../node_modules/react-native-randombytes' - Run
cd ios && pod install && cd ..to install the pod and update your workspace
Step 3: Clean and rebuild your project
Cached builds often cause weird lingering issues, so let’s do a full clean:
- In Xcode, go to Product > Clean Build Folder (or use the shortcut
Cmd+Shift+K) - Close Xcode entirely, then run
yarn run iosagain from your terminal
Quick Additional Checks
- Make sure your
shim.jshas the necessary polyfills. A standard setup for react-native-crypto looks like this:global.Buffer = require('buffer').Buffer; global.process = require('process'); global.process.env.NODE_ENV = __DEV__ ? 'development' : 'production'; - Double-check that
import './shim.js'is the first import in your App.js—this ensures the polyfills load before any crypto code tries to run.
These steps should resolve that undefined object error. I’ve used this exact fix multiple times when auto-linking failed to set up react-native-randombytes correctly.
内容的提问来源于stack exchange,提问作者user1232690

