Skip to content
Advertisement

ReferenceError Can’t find variable: Image – React Native, create-expo-app. Can’t create an HTMLImageElement with Image()

The Error

I’m early in learning to use React-Native, working with create-expo-app.

When I run npm start, I run into an error giving this output:

ERROR ReferenceError: Can’t find variable: Image

I expect that the second two errors are caused by the first error.

ERROR Invariant Violation: Failed to call into JavaScript module method AppRegistry.runApplication(). Module has not been registered as callable. Registered callable JavaScript modules (n = 11): Systrace, JSTimers, HeapCapture, SamplingProfiler, RCTLog, RCTDeviceEventEmitter, RCTNativeAppEventEmitter, GlobalPerformanceLogger, JSDevSupportModule, HMRClient, RCTEventEmitter. A frequent cause of the error is that the application entry file path is incorrect. This can also happen when the JS bundle is corrupt or there is an early initialization error when loading React Native.

ERROR Invariant Violation: Failed to call into JavaScript module method AppRegistry.runApplication(). Module has not been registered as callable. Registered callable JavaScript modules (n = 11): Systrace, JSTimers, HeapCapture, SamplingProfiler, RCTLog, RCTDeviceEventEmitter, RCTNativeAppEventEmitter, GlobalPerformanceLogger, JSDevSupportModule, HMRClient, RCTEventEmitter. A frequent cause of the error is that the application entry file path is incorrect. This can also happen when the JS bundle is corrupt or there is an early initialization error when loading React Native.

My Project I’ve built a minimal app that causes the error.

I ran:

JavaScript
JavaScript
JavaScript

(react-canvas-map: https://www.npmjs.com/package/react-canvas-map)

App.js

The error originates from line 7: “new Image();”

JavaScript

The error occurs when I run

JavaScript

(which calls expo start)

The entirety of my code either comes as the standard create-expo-app boilerplate code, or from a demonstrably working example: https://bor3ham.github.io/react-canvas-map/ (an example project on the react-canvas-map library).

Some Explanation

Image() is a HTML Standard element: https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/Image

It is equivalent to “document.getElement(“img”). If I try that, I get the same error, for ‘document’.

ERROR ReferenceError: Can’t find variable: document

System Details

JavaScript

package.json:

JavaScript

Have I set up my project incorrectly?

Am I missing an import that allows me to use HTMLImageElements from the HTML Standard Specification (https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement)?

Does React 18.1.0 (my version) require me to do something differently from the tutorial (running on 16.14.0) in relation to using “Image()”? I’ve tried setting react in package.json to 16.14.0, then re-running npm install and npm start (to match what I saw in the demo – https://bor3ham.github.io/react-canvas-map/ ). The same error occurs, though it notes:

JavaScript

I didn’t expect this problem to last as long as it has done. I haven’t found any sources talking about this as an issue, but I may have missed them as I don’t have a good grasp of what terms to use to search here.

Advertisement

Answer

I’ve worked this out now.

My question was effectively a duplicate of: Exception: Can’t find variable: document

Quoting an explanative answer from that question: https://stackoverflow.com/a/66003682/20535652

Unfortunately, both window and document are part of the web standard, not JavaScript (ECMA standard).

React Native uses JS to control rendering, but lack of DOM-manipuling itself, so you can not use many web-based APIs directly in React Native.

And that answer poses two possible options for resolution:

Using WebView in React Native app to load your web app

Try controling the React Native component yourself, rewrite logic

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement