1

I have a website that I'm running integration tests for, on a Linux Docker (Debian).

In the website's API I do this:

UPLOADED_FILES_LOCATION="C:\Users\username\Documents\uploadedFiles"
const filePath = "folderName/fileName.jpg";
const filePath = path.join(process.env.UPLOADED_FILES_LOCATION!, filePath);
const fileBuffer = await fs.readFileSync(filePath);

Buut, I get this error during the tests:

Error: ENOENT: no such file or directory, open 'C:\Users\username\Documents\uploadedFiles/folderName\fileName.jpg'

I tried also like this:

const filePath = path.normalize(path.join(process.env.UPLOADED_FILES_LOCATION!, filePath));

And like this:

const filePath = path.normalize(`${process.env.UPLOADED_FILES_LOCATION!}/${filePath}`);

Aren't both path.normalize and path.join supposed to replace slashes (normalize the path)? Why isn't this working?

Edit: I realize the path is a Windows directory, but if you look a the path it still has mixed slashes...

Nienaber
  • 101
  • 1
  • 2
  • 12

1 Answers1

0

From documentation

The path.normalize() method normalizes the given path, resolving '..' and '.' segments.
When multiple, sequential path segment separation characters are found (e.g. / on POSIX and either \ or / on Windows), they are replaced by a single instance of the platform-specific path segment separator (/ on POSIX and \ on Windows). Trailing separators are preserved.

In your case UPLOADED_FILES_LOCATION should be (or convert it):

UPLOADED_FILES_LOCATION="C:\\Users\\username\\Documents\\uploadedFiles"

So:

path.normalize('C:\\Users\\username\\Documents\\uploadedFiles/folderName\\fileName.jpg')
// C:\Users\username\Documents\uploadedFiles\folderName\fileName.jpg

Alternative:

Ali Shefaee
  • 327
  • 3
  • 12
  • So ex. I just tried it with the Linux path and got: Error: ENOENT: no such file or directory, stat '/home/jenkins/agent/workspace/siteName/dist/downloadedFiles\folderName\fileName.jpg' ...And path.normalize will not replace the slashes in the filePath for me? I mean if I try to join ...dist/downloadedFiles to folderName\fileName.jpg, the \ in "folderName\fileName.jpg" will not be replaced? – Nienaber Mar 01 '23 at 12:33
  • This "folderName\fileName.jpg" thing is stored in the database as a string. – Nienaber Mar 01 '23 at 12:40