I'm having trouble importing dat.gui into javascript

545 Views Asked by At

I can't seem to import dat.gui into javascript;

I'm using

import * as dat from 'dat.gui';

const gui = new dat.GUI();

When I run my code an error shows stating: Uncaught SyntaxError: Cannot use import statement outside a module. I've tried adding type='module' to the script but it didn't end up working. I was wondering if anyone has run into the same issue and could help me out, thanks.

1

There are 1 best solutions below

0
Shri Hari L On

Once the library is loaded, you can access it via window.dat or simply dat

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>dat.gui Example</title>
</head>
<body>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.9/dat.gui.min.js" integrity="sha512-WoO4Ih0CDOSLYafy22wZD/mcJ7k0ESLqtQsFa6zFKnEUrbtuGU+GkLtVhgt93xa2qewG5gKEC6CWlN8OaCTSVg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
  <script>
    if(dat != null) {
      alert("dat is working!");
      const gui = new dat.GUI(); 
      if(gui != null) {
            alert("gui is working too!");
      }
    }
  </script>
</body>
</html>

When you are going with NodeJS, you can import (ES6) or require (CommonJS).