1

I have a directory structure as follows:

home/
├── module1/
│   ├── index.js
│   └── file.csv
└── module2/
    └── index.js

Module 1 index.js has code that reads in file.csv using a node module called csvtojson. module2/index.js requires module1/index.js.

When I run the code directly from module1 everything works fine and the file is read in successfully. However, when I run the code in module2 I get a "File does not exist." error. Do I need to explicitly make all of my paths absolute?

Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
James Stott
  • 129
  • 1
  • 7

1 Answers1

0

I was running into the exact same problem using csvtojson as well.

Whenever you are using a plain path to a file it is searched relative to the caller script, except when using require, which only works for source files and json as far as I know.

But you have the option to use __dirname to get the absolute directory of the specific script it is used in, so you can get the right path like this:

const yourPath = __dirname + "/file.csv";
Marian Klühspies
  • 15,824
  • 16
  • 93
  • 136