自分で作成したjavascriptのクラスをimportしようとするとエラーになってしまいました。
調べたところElectronでは、ES6のimportが使用できないとあったのですが、
使用している例もあるのを見かけます。
どのようにしてEs6を可能にしているのかが見つからず
どうすればimportできるのでしょうか?
他にもvueをimportしようとしても同様のエラーになりました。
エラー内容
myclass.js:1
Failed to load module script: The server responded with a non-JavaScript MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec.
index.js:1
Uncaught SyntaxError: Unexpected token {
** 環境**
Electronバージョン:6.0.4
Windows10
javascript
1// index.js 2import { MyClass } from "./myclass.js"; // <----エラーになる 3 4let myclass = new MyClass("name"); 5 6consol.log(myclass.myName());
javascript
1// myclass.js 2export class MyClass { 3 constructor(name) { 4 this.name = name; 5 } 6 myName() { 7 return this.name; 8 } 9}
html
1<!---index.html---> 2<!DOCTYPE html> 3<html> 4 <head> 5 <meta charset="UTF-8"> 6 <title>Hello World!</title> 7 </head> 8 <body> 9 Hello World! 10 <script type="module" src="./myclass.js"></script> 11 <script type="text/javascript" src="./index.js"></script> 12 </body> 13</html>
javascript
1// main.js 2const electron = require('electron'); 3const app = electron.app; 4const BrowserWindow = electron.BrowserWindow; 5 6let mainWindow = null; 7app.on('ready', () => { 8 9 mainWindow = new BrowserWindow( 10 { 11 width: 400, 12 height: 300, 13 webPreferences: { 14 webSecurity: false 15 } 16 }); 17 18 mainWindow.loadURL('file://' + __dirname + '/index.html'); 19 20 mainWindow.webContents.openDevTools(); 21 22 mainWindow.on('closed', function() { 23 mainWindow = null; 24 }); 25});
// package.json { "name": "importtest", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { "electron": "^6.0.4" } }
あなたの回答
tips
プレビュー