Problems calling .env variables to config.json

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:
“token”: process.env.TOKEN,
“prefix”: “.”,
“ownerID”: process.env.OWNER
TOKEN=“random token”,
OWNER=“random ID”,

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)


throw err;


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

Any idea what I’m doing wrong?

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:

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.

Just store your token in .env file and refer to it using client.login(process.env.TOKEN); in your bot’s main code. No need to store the token in config.json.