首页 > 解决方案 > fs.existsSync 不是函数 electron-vue + microsoft graph(webpack 问题)

问题描述

我有一个使用 vue-electron cli 插件生成的应用程序,我想使用指南集成 Microsoft Graph 工具包。我按照指南在 background.js 中创建了这个主应用程序:

'use strict'

import { app, protocol, BrowserWindow } from 'electron'
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
import { ElectronAuthenticator, MsalElectronConfig } from '@microsoft/mgt-electron-provider/dist/Authenticator';


const isDevelopment = process.env.NODE_ENV !== 'production'

// Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([
  { scheme: 'app', privileges: { secure: true, standard: true } }
])

async function createWindow() {
  // Create the browser window.
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      
      // Use pluginOptions.nodeIntegration, leave this alone
      // See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
      nodeIntegration: true
    }
  })

  if (process.env.WEBPACK_DEV_SERVER_URL) {
    // Load the url of the dev server if in development mode
    await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
    if (!process.env.IS_TEST) win.webContents.openDevTools()
  } else {
    createProtocol('app')
    // Load the index.html when not in development
    win.loadURL('app://./index.html')
  }

  const config = {
    clientId: 'my client id went here',
    mainWindow: win, //This is the BrowserWindow instance that requires authentication
    scopes: [
      'user.read',
          'user.read',
          'people.read',
          'user.readbasic.all',
          'contacts.read',
          'presence.read.all',
          'presence.read',
          'user.read.all',
          'calendars.read'
    ],
  }

  await ElectronAuthenticator.initialize(config)

}

// Quit when all windows are closed.
app.on('window-all-closed', () => {
  // On macOS it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  // On macOS it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (BrowserWindow.getAllWindows().length === 0) createWindow()
})

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', async () => {
  if (isDevelopment && !process.env.IS_TEST) {
    // Install Vue Devtools
    try {
      await installExtension(VUEJS_DEVTOOLS)
    } catch (e) {
      console.error('Vue Devtools failed to install:', e.toString())
    }
  }
  createWindow()
})

// Exit cleanly on request from parent process in development mode.
if (isDevelopment) {
  if (process.platform === 'win32') {
    process.on('message', (data) => {
      if (data === 'graceful-exit') {
        app.quit()
      }
    })
  } else {
    process.on('SIGTERM', () => {
      app.quit()
    })
  }
}

在我的主 Vue 窗口中,我添加了这个

    import HelloWorld from './components/HelloWorld';
import {Providers} from '@microsoft/mgt-element';
import {ElectronProvider} from '@microsoft/mgt-electron-provider/dist/Provider'
import '@microsoft/mgt-components'
export default {
  name: 'App',

  components: {
    HelloWorld,
  },

  data: () => ({
    //
  }),
  methods:{

  },
  mounted:function(){
    Providers.globalProvider = new ElectronProvider();
  }
};

我很确定 webpack 会导致问题,因为我不确定如何配置它并添加指南中提到的内容。

我在浏览器窗口中遇到的错误:

Uncaught TypeError: fs.existsSync is not a function
at getElectronPath (webpack-internal:///./node_modules/electron/index.js:7)
at eval (webpack-internal:///./node_modules/electron/index.js:18)
at Object../node_modules/electron/index.js (chunk-vendors.js:4247)
at __webpack_require__ (app.js:849)
at fn (app.js:151)
at eval (webpack-internal:///./node_modules/@microsoft/mgt-electron-provider/dist/Provider/ElectronProvider.js:4)
at Module../node_modules/@microsoft/mgt-electron-provider/dist/Provider/ElectronProvider.js (chunk-vendors.js:1184)
at __webpack_require__ (app.js:849)
at fn (app.js:151)
at eval (webpack-internal:///./node_modules/@microsoft/mgt-electron-provider/dist/Provider/index.js:2)

在此先感谢您的帮助。

标签: javascriptvue.jswebpackelectronmicrosoft-graph-api

解决方案


推荐阅读