簡體   English   中英

如何在 electron 中使用 import 或 require

[英]how to use import or require in electron

我正在嘗試構建一個 electron 應用程序。 我想從另一個 js 文件中導入一些函數。 但是在使用導入時出現錯誤顯示

不能在模塊外使用 import 語句為什么會發生這種情況

我的代碼是 eventsource.js

import { sample } from './eventhandler'
console.log('inside eventsource');
function test(){
console.log('test function')
}
test();
sample();

事件處理程序.js

export function sample(){
console.log('sample')}

原型.html

<!DOCTYPE html>
<html>
<head>
<meta charset = "UTF-8">
<title>sample</title>
<script type="module" src="../views/eventsource.js"></script>
</head>
<body class="content">
</body>
</html>

正如錯誤消息所說,您無法在 Node.js 中使用 ES6 導入。 對於require和 module.exports,您應該使用module.exports

const { sample } = require('./eventhandler');
console.log('inside eventsource');
function test() {
  console.log('test function');
}
test();
sample();
function sample() {
  console.log('sample');
}

module.exports.sample = sample

對於 ES6 導出/導入,您需要對該功能的實驗性支持。 Node.Js 的網站上閱讀有關此內容的更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM