javascript - how to use import or require in electron - Stack Overflow

I'm trying to build an electron app. I want to import some functions from another js files. but wh

I'm trying to build an electron app. I want to import some functions from another js files. but while using import i gets error showing

cannot use import statement outside a module why this happening

my code is eventsource.js

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

eventhandler.js

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

prototype.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>

I'm trying to build an electron app. I want to import some functions from another js files. but while using import i gets error showing

cannot use import statement outside a module why this happening

my code is eventsource.js

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

eventhandler.js

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

prototype.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>
Share Improve this question asked Jul 1, 2020 at 7:33 IjasIjas 4031 gold badge5 silver badges21 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

As the error msg says you are unable to use ES6 imports in Node.js. You should go for require and 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. Read more about this on Node.Js's site.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742389678a4434762.html

相关推荐

  • javascript - how to use import or require in electron - Stack Overflow

    I'm trying to build an electron app. I want to import some functions from another js files. but wh

    21小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信