#Checking if a const is defined in a certain file

37 messages · Page 1 of 1 (latest)

sturdy spear
#

Hey, I'm using D.JS (that shouldn't matter) and I have an input, basically I want to check if input is for example debug, check in an another .ts file if debug is introduced there (like export const debug). If so run the function, else prompt a console.log

slow magnet
#

you could use a dynamic import to do that

#

await import("./file") and then check the debug object is present or not

#

but I don't really recommend doing that

#

maybe you should explain what you're trying to do a bit more, so that we can come up with a better solution

sturdy spear
#

Well I guess just if input debug exists in some function or whatever you can come up with, execute it, else console.log. Just basically going to introduce a docs commands, so I can have multiple inputs

slow magnet
#

you should set an env variable

#

and check if it exists or not

#

and based on that, do whatever you wanna do

#

oh, but wait
don't don't need to check if the variable exists or not

#

you can just set it

#

and check it's value

#

like, true or false

#

that also works and is way easier

sturdy spear
#

Currently I'm confused, so I would need to check if input is in the env variable, if so, just import the function and run it?

slow magnet
#

yes

#

you cfould also just import the function, just like any function

#

and the debug function would check if the env variable is defined or not

#

and if not, it just doesn't do anything and return directly

#

might be cleaner

sturdy spear
#

i mean it could be anything other than debug

#

Also it would be cleaner to check if there's a function like I explained, but why don't you recommend?

slow magnet
#

no, don't check if variable exist or not

#

it's not cleaner

sturdy spear
slow magnet
#

since you can't really access a variable that doesn't exit

#

so there is no way to check if it exists or not by accessing it

slow magnet
sturdy spear
#

Then why don't you recommend? I mean it would be more cleaner and simpler, no?

slow magnet
#

no

#

you can't access it if it doesn't exist

#

and since you can't access it, you can't check to see if it's defined or not

#

if you need flags, use env variables

#

or a global config object

#

don't check for the existance or not of a variable

sturdy spear
#

How about instead of a function, maybe introduce it in a single function and make something like a JSON

slow magnet
#

I don't see how that is related