Search code examples
javascriptnode.jsbotframework

Is __dirname unusable this way or is it undefined?


I have set a variable that is shown below
pathImage = __dirname + '/bot.jpg;

I want to pass the value of pathImage to IconUrl from my .env file. This is how I do it.
var invite = new Welcome(process.env.IconUrl=pathImage, process.env.BotVersion);

Finally, I'm getting this error from NodeJs

[onTurnError]: SyntaxError: Unexpected token U in JSON at position 633

But, this works for me (setting path manually, not preferable way)
var invite = new Welcome(process.env.IconUrl='C:/Users/Mackhem/Desktop/testBot/bot.jpg', process.env.BotVersion)


Solution

  • I think there's a few things that are off:

    1. You're missing a ' in pathImage = __dirname + '/bot.jpg; -- maybe just a typo
    2. I believe that you're trying to call process.env.IconUrl, but you're setting it at the same time with process.env.IconUrl=pathImage

    Here's the fix:

    In your .env file:

    IconUrl=bot.jpg
    

    In your other file:

    const pathImage = path.join(__dirname, '..', process.env.IconUrl);
    var invite = new Welcome(pathImage, process.env.BotVersion);
    

    Note: You cannot use __dirname in the .env file since everything in the .env file gets converted to a string.