我在导入文件模块时遇到问题,这就是为什么我无法在整个项目中导入它们的原因

我越来越:


  未捕获的ReferenceError:路径未定义


我有如下所示的main.js导入:

const { electron,
    app, // Module to control application's life.
    BrowserWindow, // Module to create native browser window.
    Menu, // The menu class is used to create native menus that can be used as application menus and context menus.
    ipcMain, // The ipcMain module, when used in the main process, handles asynchronous and synchronous messages sent from a renderer process (web page).
    shell, // Module that provides functions related to desktop integration.
    globalShortcut // Module can register/unregister a global keyboard shortcut with the operating system so that you can customize the operations for various shortcuts.
    // Keep a global reference of the window object, if you don't, the window will be closed automatically when the JavaScript object is garbage collected.
} = require('electron');
app.commandLine.appendSwitch('remote-debugging-port','8315');
app.commandLine.appendSwitch('host-rules','MAP * 127.0.0.1');
const nativeImage = require('electron').nativeImage;
const path = require('path');
const url = require('url');


在我想使用路径fs的js文件中:

usingfile.js

   if (inElectron()){
        var {ipcRenderer} = require('electron'),
        remote = require('electron').remote; // Allows IPC with main process in Electron.

    var {path} = require('path');

    var {fs} = require('fs');


    }


如果我使用use path或fs,我将获得错误(usingfile.js

    var fileLoc = path.join(__dirname, 'folder/');
    var fileList = fs.readdirSync( fileLoc );


在上面的行中我得到


  未捕获的ReferenceError:路径未定义
  
  未捕获的ReferenceError:未定义fs

最佳答案

path中没有对象或属性path,但是使用以下代码时,这就是您要的内容:

var {path} = require('path');


正确的代码是这样的:

if (inElectron()){
    var {ipcRenderer} = require('electron'),
    remote = require('electron').remote; // Allows IPC with main process in Electron.

    var path = require('path');
    var fs = require('fs');
}

09-17 15:32
查看更多