[英]how to use import or require in electron
I'm trying to build an electron app.我正在尝试构建一个 electron 应用程序。 I want to import some functions from another js files.我想从另一个 js 文件中导入一些函数。 but while using import i gets error showing但是在使用导入时出现错误显示
cannot use import statement outside a module why this happening不能在模块外使用 import 语句为什么会发生这种情况
my code is eventsource.js我的代码是 eventsource.js
import { sample } from './eventhandler'
console.log('inside eventsource');
function test(){
console.log('test function')
}
test();
sample();
eventhandler.js事件处理程序.js
export function sample(){
console.log('sample')}
prototype.html原型.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>
As the error msg says you are unable to use ES6 imports in Node.js.正如错误消息所说,您无法在 Node.js 中使用 ES6 导入。 You should go for require
and module.exports
对于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
For ES6 export/import you need experimental support for the feature.对于 ES6 导出/导入,您需要对该功能的实验性支持。 Read more about this on Node.Js's site .在Node.Js 的网站上阅读有关此内容的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.