What is the path to a backend file?

I’ve created a backend file (using Backend > New File from the Site Structure nav) to store my service account keyfile required to access BigQuery. In the Web Module code, I need to provide the path to this file to create a connection to BigQuery.

Here’s the code:

const bigqueryClient = new BigQuery({ projectId: ‘XXXXXXX-XXXXXX’, keyFilename: ‘backend/XXXXXXXXXX.json’});

I get the following error message:

Error: ENOENT: no such file or directory, open ‘/elementory/backend/XXXXXXXXXX.json’

I know I can import this JSON file into the Web Module, which I tested to validate that the path was correct. However, is it possible to pass the path for this file to the BigQuery function?

Any help would be greatly appreciated!

Import {filename} from backend/xxxModule

Hopefully this helps! Good luck :slight_smile:

Thank you for your help, Brittani! In this case though, I needed the know the path of the file so I could pass the full path into the BigQuery constructor. Importing the file contents wasn’t sufficient for what BigQuery needed.

A great friend of mine was able to figure this out for me. In Node.js, there’s a global variable called “__dirname” that provides the path to the file. So to create a connection to BigQuery via a Web Module, here’s the corrected code:

const bigqueryClient = new BigQuery({ projectId: ‘XXXXXXX-XXXXXX’, keyFilename: __dirname + ‘/XXXXXXXXXX.json’});

Don’t forget the forward slash before the file name. Also, note that the Corvid editor will say that “__dirname” is not defined, but you can ignore that error flag.