Jupyter Notebook出现THREE未定义错误,fourd.js依赖加载异常求助
Hey there, let’s dig into this frustrating error you’re hitting. I’ve dealt with similar dependency loading quirks in Jupyter before, so here are targeted steps to get to the bottom of why THREE isn’t being recognized even though fourd.js seems to load:
1. Fix Script Loading Order & Async Behavior
Fourd.js almost certainly relies on THREE being available before it runs. Even if you ordered your script tags correctly, browsers load scripts asynchronously by default—which means fourd might try to execute before three.js finishes loading.
Try these fixes:
- Double-check your script tag order: Load
r89dev.three.min.jsfirst, thenfourd.js. - Force sequential loading with the
async=falseattribute to make sure three.js is fully loaded before fourd runs:<script src="js/r89dev.three.min.js" async=false></script> <script src="js/fourd.js" async=false></script> - If fourd runs code immediately on load, wrap its initialization in a callback that waits for
THREEto exist. For example:<script src="js/r89dev.three.min.js"></script> <script> function initFourd() { if (typeof THREE !== 'undefined') { // Load fourd.js here or call its init function const script = document.createElement('script'); script.src = 'js/fourd.js'; document.head.appendChild(script); } else { setTimeout(initFourd, 100); } } initFourd(); </script>
2. Verify File Paths (They’re Easier to Break Than You Think)
Even if it worked before, path issues can creep in—like if you moved the notebook, renamed the js folder, or Jupyter’s working directory changed.
- Confirm the files exist where you think they do: Run a code cell with
!ls js(Linux/macOS) or!dir js(Windows) to list the contents of thejsfolder relative to your notebook. - Use Jupyter’s shareable links to avoid path ambiguity: Right-click
r89dev.three.min.jsin Jupyter’s file browser, select "Copy Shareable Link", and paste that into your script tag. It’ll look something like this:<script src="/files/your-notebook-folder/js/r89dev.three.min.js"></script>
3. Dig Into the Network Tab (Not Just the Console)
You mentioned not seeing a load failure in the console, but the Network tab in your browser’s dev tools will tell you exactly if three.js is being requested and loaded successfully.
- Open DevTools (F12 or Ctrl+Shift+I), go to the Network tab, and filter by "JS".
- Run your
%%htmlcell again. Look forr89dev.three.min.js:- If it doesn’t show up at all: Your path is wrong.
- If it shows a 404 status: The file is missing or the path is incorrect.
- If it shows a 200 status but loads after fourd.js: Timing is the issue (go back to step 1).
4. Rule Out Caching or Corrupted Files
Browsers love caching old scripts, and sometimes files can get corrupted unexpectedly.
- Hard-refresh your browser (Ctrl+Shift+R or Cmd+Shift+R) to clear the page’s cache.
- Add a cache-busting query parameter to force a fresh load of three.js:
<script src="js/r89dev.three.min.js?v=1"></script> - Re-download or replace
r89dev.three.min.jsto make sure the file itself isn’t corrupted.
5. Test Outside Jupyter to Isolate the Issue
To confirm if the problem is Jupyter-specific, create a simple static HTML file in the same directory as your notebook and js folder, with the same script tags. If it works here, the issue is with Jupyter’s %%html environment; if it still fails, the problem is with your files or paths.
Example static HTML file:
<!DOCTYPE html> <html> <head> <script src="js/r89dev.three.min.js"></script> <script src="js/fourd.js"></script> </head> <body> <script> console.log("THREE loaded:", typeof THREE !== 'undefined'); console.log("fourd loaded:", typeof fourd !== 'undefined'); // Adjust based on fourd's exposed variable name </script> </body> </html>
内容的提问来源于stack exchange,提问作者Joshua M. Moore

