简体   繁体   中英

require('fs') not working with angular and electron

I am using angular 6.0, electron 2.0, typescript 2.9, nodejs 9.11 to make a desktop app using electron framework. I am struggling with accessing NodeJS native API from the typescript code. I have set "commonjs" in the "tsconfig.app.json" file. When I write : require('fs') or require('net') in any of the ts files which are part of angular application, the system isn't able to find those modules.

Only one solution has worked so far. It goes like this. First in 'native.js'

window.fs = require('fs')

Then in polyfill.ts :

declare global {
  interface Window{
    fs : any;
  }
}

Then access fs in the rest of codebase as window.fs .

While this is okay, but it is not scalable as if I have to use any library which depends on NodeJS native API, then that library has to be imported through this mechanism.

Is there any other solution to let angular allow importing of nodejs system libraries through normal require(<module>) syntax?

import * as Fs from 'fs';
const fs: typeof Fs = window['require']('fs');

on my side, this work if you use typescript for ref. You need give a defenition or is will any. Using this for nwjs, know nodejs is global.

const fs: typeof import('fs') = require('fs');


在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM