質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Nuxt.js

Nuxt.jsは、ユニバーサルなSPAが開発可能なVue.jsベースのフレームワーク。UIの描画サポートに特化しており、SSRにおけるサーバーサイドとクライアントサイドのUIレンダリングなどさまざまな機能を持ちます。

import

自身のプラットフォーム・プログラム・データセットに対して、外部ソースを取り込むプロセスをimportと呼びます。

Three.js

Three.jsはWebGLをサポートしているJavaScriptの3D描画用ライブラリです。

Q&A

0回答

2173閲覧

Nuxt.jsでThree.jsを利用した時、importで [ERR_REQUIRE_ESM] エラーが出る

ZOI_dayo

総合スコア0

Nuxt.js

Nuxt.jsは、ユニバーサルなSPAが開発可能なVue.jsベースのフレームワーク。UIの描画サポートに特化しており、SSRにおけるサーバーサイドとクライアントサイドのUIレンダリングなどさまざまな機能を持ちます。

import

自身のプラットフォーム・プログラム・データセットに対して、外部ソースを取り込むプロセスをimportと呼びます。

Three.js

Three.jsはWebGLをサポートしているJavaScriptの3D描画用ライブラリです。

0グッド

0クリップ

投稿2021/07/05 10:17

前提・実現したいこと

Nuxt.jsとThree.jsを用いてWebページを作っています。
Three.jsの一部拡張機能(?)をimportしようとするとエラーが発生します。
エラーメッセージにはrequire()が使えないのに使っている、といった内容が書かれているように見えますが、requireは使用していませんし、Three.js内部も見てみましたが、使われていないように思います。
Babelか何かが変換したコードに含まれているのでしょうか?
解決策を教えてもらえるとありがたいです。

発生している問題・エラーメッセージ

$ npm run ganarateしたときに発生するエラーです。

...... ℹ Generating output directory: dist/ 18:57:37 ℹ Generating pages ERROR / 18:51:03 Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /(PROJECT_PATH)/node_modules/three/examples/jsm/loaders/OBJLoader.js require() of ES modules is not supported. require() of /(PROJECT_PATH)/node_modules/three/examples/jsm/loaders/OBJLoader.js from /(PROJECT_PATH)/node_modules/vue-server-renderer/build.prod.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules. Instead rename OBJLoader.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /(PROJECT_PATH)/node_modules/three/examples/jsm/package.json. at Object.Module._extensions..js (internal/modules/cjs/loader.js:1085:13) at Module.load (internal/modules/cjs/loader.js:933:32) at Function.Module._load (internal/modules/cjs/loader.js:774:14) at Module.require (internal/modules/cjs/loader.js:957:19) at n (/(PROJECT_PATH)/node_modules/jiti/dist/v8cache.js:2:2472) at /(PROJECT_PATH)/node_modules/vue-server-renderer/build.prod.js:1:77944 at Object.<anonymous> (webpack:/external "three/examples/jsm/loaders/OBJLoader.js":1:0) at __webpack_require__ (webpack/bootstrap:25:0) at Module.22 (pages/index.js:32:21) at __webpack_require__ (webpack/bootstrap:25:0) at async server.js:747:21 at async Promise.all (index 0) at async getRouteData (.nuxt/utils.js:181:0) at async Promise.all (index 0) at async setContext (.nuxt/utils.js:266:0) at async createApp (.nuxt/index.js:120:0) ✔ Client-side fallback created: 200.html

該当のソースコード

/components/ThreeJsComp.vue

vue

1<template> 2 <div id='threeJsComp'> 3 aaabbb 4 <canvas id='threeJsCompCanvas' ref='canvas'></canvas> 5 </div> 6</template> 7<script lang='js'> 8import * as THREE from 'three' 9import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls.js' 10import {OBJLoader} from 'three/examples/jsm/loaders/OBJLoader.js'; 11import {MTLLoader} from 'three/examples/jsm/loaders/MTLLoader.js'; 12 13 14export default { 15 name: 'ThreeJsComp', 16 props: {}, 17 data() { 18 return { 19 canvas: null, 20 renderer: null, 21 controls: null, 22 scene: null, 23 camera: null, 24 clock: null 25 } 26 }, 27 async mounted() { 28 await this.initCanvas() 29 }, 30 methods: { 31 async initCanvas() { 32         ... 33 (Three.jsのコード) 34         ... 35 } 36 } 37}

/pages/index.vue

vue

1<template lang='html'> 2 <div class='main'> 3 <ThreeJsComp /> 4 aaa 5 </div> 6</template> 7 8<script lang="ts"> 9// import ThreeComponent from '~/components/ThreeComponent.vue' 10import ThreeJsComp from '~/components/ThreeJsComp.vue' 11 12export default { 13 components: { 14 ThreeJsComp, 15 }, 16} 17</script> 18

試したこと

  • 4つあるimportのうち、import * as THREE from 'three'以外が存在するときにエラーが発生します。
  • ほか3つのimport文を消した時は正常にビルドできました。
  • staticフォルダにThree.jsを直接ダウンロードして、

import {OBJLoader} from '/static/three/examples/jsm/loaders/OBJLoader.js';
のようにimportした時はエラーは発生せず、関数も正常に動作しました。

補足情報(FW/ツールのバージョンなど)

/package.json

json

1{ 2 "name": "(プロジェクト名)", 3 "version": "1.0.0", 4 "private": true, 5 "scripts": { 6 "dev": "nuxt", 7 "build": "nuxt build", 8 "start": "nuxt start", 9 "generate": "nuxt generate" 10 }, 11 "dependencies": { 12 "@nuxt/typescript-runtime": "^2.1.0", 13 "@nuxtjs/composition-api": "^0.24.6", 14 "@types/three": "^0.129.2", 15 "core-js": "^3.15.1", 16 "next-transpile-modules": "^8.0.0", 17 "nuxt": "^2.15.7", 18 "nuxt-property-decorator": "^2.9.1", 19 "nuxt-typed-vuex": "^0.2.0", 20 "pug": "^2.0.4", 21 "pug-loader": "^2.4.0", 22 "pug-plain-loader": "^1.1.0", 23 "three": "^0.130.0", 24 "three-orbitcontrols-ts": "^0.1.2" 25 }, 26 "devDependencies": { 27 "@nuxt/types": "^2.15.7", 28 "@nuxt/typescript-build": "^2.1.0", 29 "typescript": "^4.2.4", 30 "webpack": "^4.46.0" 31 } 32}

/nuxt.config.json

json

1export default { 2 // Target: https://go.nuxtjs.dev/config-target 3 target: 'static', 4 5 // Global page headers: https://go.nuxtjs.dev/config-head 6 head: { 7 title: '(プロジェクト名)', 8 htmlAttrs: { 9 lang: 'en' 10 }, 11 meta: [ 12 { charset: 'utf-8' }, 13 { name: 'viewport', content: 'width=device-width, initial-scale=1' }, 14 { hid: 'description', name: 'description', content: '' }, 15 { name: 'format-detection', content: 'telephone=no' } 16 ], 17 link: [ 18 { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' } 19 ] 20 }, 21 22 // Global CSS: https://go.nuxtjs.dev/config-css 23 css: [ 24 ], 25 26 // Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins 27 plugins: [ 28 ], 29 30 // Auto import components: https://go.nuxtjs.dev/config-components 31 components: true, 32 33 // Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules 34 buildModules: [ 35 // https://go.nuxtjs.dev/typescript 36 '@nuxt/typescript-build', 37 ], 38 39 // Modules: https://go.nuxtjs.dev/config-modules 40 modules: [ 41 ], 42 43 // Build Configuration: https://go.nuxtjs.dev/config-build 44 build: { 45 } 46}

/tsconfig.json

json

1{ 2 "compilerOptions": { 3 "target": "ES2018", 4 "module": "ESNext", 5 "moduleResolution": "Node", 6 "lib": [ 7 "ESNext", 8 "ESNext.AsyncIterable", 9 "DOM" 10 ], 11 "esModuleInterop": true, 12 "allowJs": true, 13 "sourceMap": true, 14 "strict": true, 15 "noEmit": true, 16 "experimentalDecorators": true, 17 "baseUrl": ".", 18 "paths": { 19 "~/*": [ 20 "./*" 21 ], 22 "@/*": [ 23 "./*" 24 ] 25 }, 26 "types": [ 27 "@nuxt/types", 28 "@types/node" 29 ] 30 }, 31 "exclude": [ 32 "node_modules", 33 ".nuxt", 34 "dist" 35 ] 36}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問