Beginner here, seeking assistance.
Operating System: Windows 10. Browser: Chrome. Framework: Nuxt with default configurations
I have successfully installed three.js using npm (via gitbash) by running npm install three --save
. It is included in the package.json dependencies as "three": "^0.116.1",
. Additionally, I have a scene.json file stored in ~/assets/ that was created using the three.js editor and then exported as a scene.
The issue I am encountering is "Cannot use import statement outside a module" regardless of including or excluding type="module"
Here is the Vue component being utilized: (It is rendered client-side only.)
<template>
<div id="Three">
<div id="threeContainer"></div>
</div>
</template>
<script type="module">
import * as Three from 'three'
import OBJLoader from 'three/examples/jsm/loaders/OBJLoader.js'
export default {
name: 'Three',
data() {
return {
camera: null,
scene: null,
renderer: null,
mesh: null
}
},
mounted() {
this.init()
},
methods: {
init() {
const container = document.getElementById('threeContainer')
this.camera = new Three.PerspectiveCamera(
70,
container.offsetWidth / container.offsetHeight,
0.01,
10
)
this.camera.position.z = 1
this.scene = new Three.Scene()
const loader = new OBJLoader()
loader.load(
'~/assets/scene.json',
function(obj) {
this.scene.add(obj)
},
function(xhr) {
console.log((xhr.loaded / xhr.total) * 100 + '% loaded')
},
function(err) {
console.error(err.stack)
}
)
this.renderer = new Three.WebGLRenderer({ antialias: true })
this.renderer.setSize(container.clientWidth, container.clientHeight)
container.appendChild(this.renderer.domElement)
}
}
}
</script>
<style lang="scss">
#threeContainer {
height: 300px !important;
background: black;
}
</style>