I am trying to install the latest version of threejs, and I am going by the directions here:
https://threejs.org/docs/index.html#manual/en/introduction/Installation
I am going according to " Install from CDN or static hosting". And I am having my own server.
Here is the code.
<script async src="./shims_file.js"></script>
<script type="importmap">
{
"imports": {
"three": "./three.module.js"
}
}
</script>
<script type="module">
import * as THREE from 'three';
const scene = new THREE.Scene();
</script>
I can have this given code run.
Now the first thing I want to do, is to put the threejs code into a script of it’s own.
I have a module, that serves as an API which let’s me talk to threejs.
So instead of the last script, the module script. I have:
<script src="./threejs_api.js"></script>
It runs without error, regardless of if I hadd type=“module” or not.
Now that I have defined the threejs api module.
I need to make an instance of it.
So I have another script tag,
<script src="init_threejs.js"></script>
Which tries to make an instance of the threejs_api module.
I then get error:
mthreejs_api is not defined.
It is like it cannot reach the variable from the previous script tag.
In the previous script tag, the threejs api. It is not clear to me, where I should put the
import * as THREE from 'three';`
If I do add it in the first line of the constructor, I get error:
Uncaught SyntaxError: import declarations may only appear at top level of a module
But I can put it in the top line of the entire threejs_api file. So I put it there.
But the initiation file still cannot reach the threejs_api module.
So it can’t see it so I can’t make an instance of it there.
How do I reach the threejs_api module, from the init file?