【问题标题】:unexpected token export in electron-react js电子反应js中的意外令牌导出
【发布时间】:2020-05-20 01:53:03
【问题描述】:

我正在使用放置在公共文件夹中的电子反应 biolerplate.electron 的 main.js 文件。我想将另一个电子文件添加到常用常量函数中。当我尝试使用该文件时,它会抛出上述错误。 我想保持 main.js 简短。这就是我使用另一个文件的原因

/public/renderer/command-executor.js

export const  executeCommandWithOutput=(command)=>{
    const { exec } = require('child_process');
    console.log(command);
    return new Promise((resolve, reject) => {
      console.log('inside promise');
        exec(command, (err, stdout, stderr) => {
          console.log('inside execsync');
            if (err) {
              console.log(err);

                resolve(err);
            } else if (stderr) {
              console.log(stderr)

                resolve(stderr);
            } else {
                resolve(stdout);
            }
        });
    });
  }

public/main.js

const commandExecutor=require('./renderer/command-executor');
electron.ipcMain.on('launch-App',async(event,args)=>{
     commandExecutor.executeCommandWithOutput(`powershell -Command "& {Start-Process -Verb runas '${playLink}'}"`);

});

【问题讨论】:

  • 我在任何地方都没有看到export
  • 您的require 语句正在导入一个不同名称的文件。您的函数是在execute-command.js 中创建的,而您实际上是在导入./renderer/command-executor。除非我在这里遗漏了什么
  • @nopassport1 很抱歉在这里输入了错误的文件名
  • 你在用babel吗?

标签: reactjs electron


【解决方案1】:

问题在于您试图将 ES5 require 语句与 export 的 ES6 语法混合使用。两者不相容。您必须使用其中一个。

假设您使用 babel 的 ES6 实现工作正常,您应该像这样使用 import 语句:

// exporting like you are at the moment (called a named export):
export const executeCommandWithOutput = (command) =>{
    ...
}

// importing like so:
import { executeCommandWithOutput } from './renderer/command-executor';

但如果您在public/main.js 中使用require,您的export 语句应如下所示:

exports.executeCommandWithOutput = executeCommandWithOutput

您的require 将保持不变。

这篇文章可帮助您更好地了解正在发生的事情、export 功能在 ES5 中的工作原理以及您可以使用它实现什么:
https://www.sitepoint.com/understanding-module-exports-exports-node-js/

同样使用 ES6 语法:
https://alligator.io/js/modules-es6/

【讨论】:

    猜你喜欢
    • 2016-06-20
    • 1970-01-01
    • 2017-12-20
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    • 2016-12-14
    • 2021-05-24
    • 2017-04-29
    相关资源
    最近更新 更多