Im learning three.js and are setting up my first project. Im basically running the example code from three.js website. It works fine when running three.js from a folder like:
js/ directory
but I cannot get it to run when importing it as a module. Im installing the module with:
<body>
<script>
import * as THREE from 'three';
const scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
var geometry = new THREE.BoxGeometry(1, 1, 1);
var material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
var cube = new THREE.Mesh(geometry, material);
scene.add(cube);
camera.position.z = 5;
var animate = function () {
requestAnimationFrame(animate);
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render(scene, camera);
};
animate();
</script>
</body>
How come I cannot import the module? Im getting this errormessage in the browser:
Uncaught SyntaxError: Cannot use import statement outside a module
Using npm modules normally means you perform some sort of build which is then included into your html page. Check out the following start project that demonstrates how such a setup does look like:
In any event, if you use ES6 import statements inside a <script> tag, you have to write it like so <script type="module">.
A build tool like rollup will be able to resolve import * as THREE from 'three';. But notice that this statement does not work if you put it directly in a HTML page (since the browser does not know how to resolve three).
It does not matter where the app is hosted. No browser can resolve three. If you study how ES6 modules work, this topic will automatically become clear.
All official examples are using modules. So yes, it definitely works. The following example is one of the more simple demos that demonstrates the respective setup:
Ah, got it! Thank you.
But, I must admit that the documentation, in this case, is confusing. Because there is a link to MDN where they say how to use ES6 modules in HTML and right under the link you’ve got the code sample. Automatically you copy and paste the code and expect it to work. And I totally forgot that there is a third .js file in the build folder. That’s why didn’t try it.
Thanks for your answer but i mean i don’t know how to run the code.
I installed rollup package and included a build file to HTML page too. But I could not image how to run the code you shared.
This boilerplate demonstrates importing threejs modules using ES6 syntax in the client.
There is no need to add libs on the client to handle requires, umd, exports, defines, systemjs.
You also don’t need to bundle using webpack, rollup, parcel, browserify, bunch, fusebox or any other magic.
The boilerplate also includes a NodeJS server component and is written in typescript.
It demonstrates how to import the main three.module.js and also the orbitcontrols.
When you include a module file via a script tag, you need to add type="module" to it.
However, in app.js you can’t import modules by just using the module name like three. That only works when using a build tool like rollup that produces a bundle from your source files. Try it with this instead:
import * as THREE from "https://cdn.jsdelivr.net/npm/three@0.114/build/three.module.js";