Ionic2 with electron


#1

Using electron with ionic2, i cant access filesystem options on windows using
Const fs = require(‘fs’);

Keeps throwing error : fs.exists is not a function


#2

In this case, you’ll need to run

npm install @types/node

Since fs is part of the node API, you need the extra types.
Then you can do

import { readFileSync} from 'fs';

or any of the methods from fs


How to read a local file?
Error reading a file with FS
#3

Thank a lot and great job with ionic2 2.00 release. but it still isnt workng

@types/node is installed

declare var require: any
const fs = require(“fs”);

fs.readFileSync(’.’);

error: TypeError: fs.readFileSync is not a function

am i missing something


#4

Not sure, but you didn’t actually try what @mhartington suggested… LOL


#5

i did to my understanding.
i installed @types/node

if i import { readFileSync} from ‘fs’; how am i to use it
the method in the node docs is fs.readFileSync().
i dont know


#6

Once you’ve imported it, can’t you just call it as readFileSync()? Webpack should handle gluing it together.


#7

Tried it but not working. i am getting

TypeError: webpack_require.i(…) is not a function

thanks.


#8

require is nowhere in @mhartington’s suggestions.


#9

still same error :cry:


#10

Gotten it to work

added this to index.html

<script const fs = require("fs"); </script then declare var fs: any; in .ts file Thanks

#11

That import statement imports only the readFileSync function, not the entire fs module, so you can use the function directly as readFileSync('.'); (without the fs. part)