电子与电子Angular:fs.​​existsSync不是一个函数

时间:2017-05-14 16:33:25

标签: angular typescript ipc electron

我用Angular创建了一个新的Electron-Project。我使用Angular CLI构建我的应用程序。 现在,我想从Renderer-Process到Main-Process进行通信,并在Dev-Tools中出错:

> Uncaught TypeError: fs.existsSync is not a function
    at Object.<anonymous> (vendor.bundle.js:72643)
    at Object.splitPathRe (vendor.bundle.js:72649)
    at __webpack_require__ (inline.bundle.js:53)
    at Object.399 (main.bundle.js:54)
    at __webpack_require__ (inline.bundle.js:53)
    at Object.400 (main.bundle.js:107)
    at __webpack_require__ (inline.bundle.js:53)
    at Object.291 (main.bundle.js:24)
    at __webpack_require__ (inline.bundle.js:53)
    at Object.473 (main.bundle.js:234)
    at __webpack_require__ (inline.bundle.js:53)
    at webpackJsonpCallback (inline.bundle.js:24)
    at main.bundle.js:1

我使用此项目模板:https://github.com/auth0-blog/angular2-electron 重现此错误的步骤如下:

git clone https://github.com/auth0-blog/angular2-electron
npm install

3.将以下行添加到src / app / app.component.ts

const {ipcRenderer} = require('electron');

没有该行,应用程序运行没有任何问题。 由于电子,我必须以这种方式引用ipcRenderer ...... https://github.com/electron/electron/blob/master/docs/api/ipc-main.md

我不知道我做错了什么,希望,你可以帮助我。

4 个答案:

答案 0 :(得分:1)

我遇到了问题,下面的代码解决了这个问题。希望也能解决你的问题。

在yourcustom.component.ts

declare const window: any;
declare const ipcRenderer: any;
ipcRenderer = window.require('electron');
// then you can continue what you want to do with ipcRenderer.

答案 1 :(得分:0)

Webpack带来了自己的require clobbers node.js'require,所以当你require一个node.js核心模块时,webpack无法解析为你的某个文件或依赖,它抛出。 (您可以在堆栈跟踪中看到它包含__webpack_require__。这是因为webpack将所有require重写为__webpack_require__,以便它使用它自己的内部node.js-esque系统)。 Webpack是为Web /浏览器构建的,因此它不能很好地与开箱即用的节点配合使用。要绕过它,您可以使用:https://github.com/chentsulin/webpack-target-electron-renderer

但我也考虑使用webpack,请参阅:why use webpack with electron

答案 2 :(得分:0)

您可以使用如下

const { BrowserWindow } = (<any>window).require('electron')

答案 3 :(得分:0)

`

declare var window: ElectronWindow;

interface ElectronWindow {
    process: any;
    require: any;
    sudo: any;
}

let _rawRequire;
if (window && window.require) {
    _rawRequire = require;
    require = window.require;
}

/*** import the node module here ***/
import * as childProcess from 'child_process';
import { app, shell, ipcRenderer, OpenDialogOptions } from 'electron'
import { ChildProcess } from "child_process";
// import sudo  from 'sudo-prompt';
import Sudoer from 'electron-sudo';


if (_rawRequire) {
    require = _rawRequire;
}

/***  import the angular module here ***/
import { TS } from "typescript-linq";
import Exception = TS.Exception;
import { Injectable } from '@angular/core';
import { ElectronService } from "ngx-electron";

`

相关问题