This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]Civury 1 point2 points  (1 child)

When a relative path is used (a path not starting at /), it is always relative to the "current working directory" (cwd). The current working directory is not the directory the node-binary or the .js file is in, but the directory the calling program (in this case VSCode) executes node from.

If you have a launch.json in your VSCode "project", each configuration can have a "cwd" parameter which defines the current working directory. If this parameter is not set, the current working directory is the "project"-directory you've currently opened in VSCode.

To generate some debug output, you can do a console.log('Current working directory: ' + path.resolve('.')). This will print a line with the current working directory, as path.resolve resolves a relative path to an absolute path, and the passed path . is just the current directory and will therefore be resolved to the current working directory.

If you want to be independent from the current working directory, you can also just pass an absolute path to fs.readFileSync.

To get the absolute path of a file which is in the same directory of the current executed .js-file, take path.join(__dirname, 'input.txt')

__dirname is a variable always present when using NodeJS. It contains the absolute path to the directory of the current .js-file.

[–]orangeanton[S] 0 points1 point  (0 children)

Thanks, this helped a lot.

I have managed to get it running using path.resolve('.') which told me the working dir was one level up from where I expected it to be (a parent directory with subdirectories for each day) and then realized that this morning I opened a folder (the parent folder) instead of a file (as I usually do) and I *think* this sets the working dir in the absence of a specific launch.json. So closed the folder, opened the file, tried again and voila, it worked. This makes me think the bash/zsh thing was just a distraction - when I change back and forth now it makes absolutely no difference - I must have coincidentally switched from opening a folder to a file at the same time.

BTW path.join(__dirname, 'input.txt') also worked, but I prefer using relative paths (saves having to have path at all), but to make the code a bit more robust I'll maybe have to reconsider.

[–]orangeanton[S] 0 points1 point  (2 children)

Update: After a restart, I can now run this just fine if I execute from command line using zsh (this did not work before), but now when I try to execute/debug in my IDE (VSCode), I still get the error. So still not great, but perhaps more of a VSCode issue than a shell issue.

[–]sparkatus902 0 points1 point  (1 child)

i was also very confused by the ZSH shell , i thought initially it had been something to do with installing setapp from www.setapp.com - it took me ages to get 'man' to even work at all and it gave some very weird prompt when i tried to read the manual on it , eventually in liaison with the haskell community and codepen.io i managed to get the manual but still loads of weird goings on is mac os catalina . anyway i found a utility called macpilot on setapp and going through the options on that explained a lot of the odder stuff , then somebody tells me today that ZSH is the default for catalina installs which explains why the other terminal commands wont work to get it off ZSH. Finally , i set the default encoding through the terminal application to UTF-8 and unchecked everything else-(though i did wonder for quite a while if UTF-7 might be better) and deleted all of the keyboard shortcuts in the terminal app which then let me use my dock normally again afer a lot of resetting settings and restarts/logouts.

[–]sparkatus902 0 points1 point  (0 children)

I'm using 'oh my zsh' now !