Problems calling .env variables to config.json


#1

I’m currently making a discord bot, using glitch to host.
The bot runs successfully but only if I put my token in the config file rather than the .env file which isnt very secure.
Everytime I try and run the bot while I have the following code:
config.json
{
“token”: process.env.TOKEN,
“prefix”: “.”,
“ownerID”: process.env.OWNER
}
.env
TOKEN=“random token”,
OWNER=“random ID”,
PROJECT_DOMAIN=“random-name”

I get a Syntax error that I can’t seem to solve saying:

SyntaxError: /app/config.json: Unexpected token p in JSON at position 13

at JSON.parse (<anonymous>)

at Object.Module._extensions..json (module.js:592:27)

at Module.load (module.js:507:32)

at tryModuleLoad (module.js:470:12)

at Function.Module._load (module.js:462:3)

at Module.require (module.js:517:17)

at require (internal/module.js:11:18)

at Object. (/app/server.js:8:16)

at Module._compile (module.js:573:30)

at Object.Module._extensions..js (module.js:584:10)

module.js:595

throw err;

^

SyntaxError: /app/config.json: Unexpected token p in JSON at position 13


Any idea what I’m doing wrong?


#2

Hi @R-Hollywood,

you can’t use variables in json files. If your project loads config.json into a javascript object, you might be able to set the relevant fields there.

Perhaps some developer with more experience with discord bots will help you more though :slight_smile:


#3

Hey @R-Hollywood!

If you’re using the .env file for keeping keys, secrets, etc. it really isn’t needed to connect it to your package.json file. The only places you’ll be using it anyways would be in executable files such as JavaScript.