Find absolute path of a node/JS project file

Hey everyone.

I am trying to switch to glitch full time but I am running into an issue.

When I link a file and use ```

console.log(__dirname);

The reply I get is just

/app/keys.json

I am working with dialogflow and I need to use the API for part of the project.

This requires me to do
export GOOGLE_APPLICATION_CREDENTIALS='/app/keys.json'
on the terminal.

The issue is that it is not working because app/keys.json isn’t the absolute path.

On another platform I can execute the same code and I get:

/home/runner/myproject/app/keys.json

Using their terminal I can do

export GOOGLE_APPLICATION_CREDENTIALS='/home/runner/myproject/keys.json

and my project works as expected.

How can I get glitch to give me the full path?

That really is the full path. The directory structure is just not as deep as on some other :eyes: platforms.

3 Likes

Answering my own question I guess.

Full path is ‘/rbd/pnpm-volume/bunchofnumbers/app/keys.json’

Glitch doesn’t work for my needs because I can’t set an environment variable from terminal since terminal isn’t synced with the project itself and needs to be manually synced.

I seem to lose environment variables I have exported upon refreshing the terminal(IE: I can’t print them with printenv)

Glitch terminal is focused on creating files or similar from terminal which is not what I am after.

what did you mean by “full path”? you mean an absolute path, starting with a / right?

/rbd/pnpm-volume/… is a different place in the filesystem. /app/node_modules is a symlink to somewhere in /rbd/pnpm-volume/…, but a path outside of node_modules like /app/keys.json is really only /app/keys.json.

I seem to lose environment variables I have exported upon refreshing the terminal(IE: I can’t print them with printenv)

btw, this sounds like not a Glitch specific issue, but rather part of how environment variables are. they’re scoped to a process and shared with descendant processes. when you set some environment variables in a terminal session, only processes you run from that session get them. other terminal sessions and the app itself (Glitch runs this from outside of the terminal sessions) aren’t affected. you should be able to reproduce this behavior on your own computer by opening multiple terminal windows.

if you want to set some environment variables for newly opened terminal sessions and for the app, write them in a .env file Adding Private Data - Glitch Support. it’s similar to how you’d have to set up a .profile file or .bashrc to set environment variables on your own computer.

2 Likes

Thanks for your help.

Just went through the support article again and it mentions being a temporary value and it being restored for every terminal session as you said.

.env file works with /app/… as its value.

Just marked you as the solution. Fairly new to node sorry.

2 Likes