Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't compile react app with forge viewer - can't compile - Autodesk not defined un-def

we want to move some of our forge viewer code base into a react-app and can't figure out how to use the viewer3d js api without appending all Autodesk.Viewer.... usages in our components with window.* This works in all the (excellent and extensive) git samples we've studied. why? we load the viewer3d.js file in the index.html between the and the react landing :

<body>
  <script src="https://developer.api.autodesk.com/derivativeservice/v2/viewers/viewer3D.js?v=6.0" />
  <div id="root" />

the error we get (for every instance of usage of the Autodesk namespace):

Failed to compile

./src/components/Viewer.js
  Line **:  'Autodesk' is not defined  no-undef

this works:

this.viewer = new window.Autodesk.Viewing.Private.GuiViewer3D(this.viewerContainer)

this doesn't work:

this.viewer = new Autodesk.Viewing.Private.GuiViewer3D(this.viewerContainer)
like image 883
Gregor Avatar asked Nov 26 '25 02:11

Gregor


1 Answers

As mentioned in this user guide, you need to explicitly read any global variables from window. Put this at the top of the file and it will work:

const Autodesk = window.Autodesk;

And it's recommended to yank your script tag to the header section of your app's entry html to make sure it gets loaded prior to the React emits:

<!DOCTYPE html>
<html lang="en">
  <head>
  ...
  <script src="https://developer.api.autodesk.com/derivativeservice/v2/viewers/viewer3D.js?v=6.0" />
  </head>
...
like image 108
Bryan Huang Avatar answered Nov 28 '25 04:11

Bryan Huang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!