Reputation: 1179
I started messing around with Angular 2 and Electron, and used this starter package. Everything seems to be going well, I'm even able to use the node fs package to read from a directory.
The problem I'm having is that I can't seem to be able to use an absolute path for the readdirSync() method. It only takes a relative path.
I did find this in the docs for fs that you can use the URL package to show an absolute path for readdirSync(), like so:
const fs = require('fs');
const { URL } = require('url');
// file:///C:/tmp/hello => C:\tmp\hello
fs.readFileSync(new URL('file:///C:/tmp/hello'));
That looks great, but in my Angular 2/TypeScript/Electron world that doesn't seem to work. Mine looks like this:
import { Injectable } from "@angular/core";
import {readdirSync} from "fs";
import { URL } from "url";
@Injectable()
export class LinkDocRetrieverService {
getFiles():Array<String> {
// read through all the directories to get all the files and folder names
let u = new URL("file:///C:/SAFE/MISC");
let x = readdirSync(u);
console.log("files retrieved="+ x.length);
let files: Array<string> = [];
x.forEach(f => {
files.push(f);
});
return files;
}
}
Both intellisense and runtime both tell me that
[ts] Argument of type 'URL' is not assignable to parameter of type 'string | Buffer'. Type 'URL' is not assignable to type 'Buffer'. Property 'write' is missing in type 'URL'.
I would have expected the syntax to be the same, but no luck.
I have read a lot of posts stating that they can't run the fs package in Angular 2 + Typescript, but it works well for me, as long as I use a relative path. I just need help getting the absolute path to work. Thanks in advance.
Upvotes: 1
Views: 984
Reputation: 1129
Documentation states that URL support was introduced in v7.6.0 and is experimental. Electron uses v7.4.0, thus you can not use URL with fs yet.
fs.readFileSync('C:/tmp/hello')
Should work just fine.
Upvotes: 1