fs.readFileSync is not file relative? Node.js
Just to expand on the above, if you are using fs.readFileSync with TypeScript (and of course CommonJS) here's the syntax:
import fs from 'fs';
import path from 'path';
const logo = fs.readFileSync(path.resolve(__dirname, './assets/img/logo.svg'));
This is because fs.readFileSync()
is resolved relative to the current working directory, see the Node.js File System docs for more info.
Source: Relative fs.readFileSync paths with Node.js
And of course, the CommonJS format:
const fs = require('fs');
const path = require('path');
const logo = fs.readFileSync(path.resolve(__dirname, './assets/img/logo.svg'));
You can resolve the path relative the location of the source file - rather than the current directory - using path.resolve
:
const path = require("path");
const file = fs.readFileSync(path.resolve(__dirname, "../file.xml"));