Configuration files

Now that you have your bot token, we can now create a config file to keep your token safe.

TIP

This is recommended and is a good practice for sensitive information.

Using a config.json file

Using a config.json file is a common way of keeping sensitive information safe. Create a config.json file in your project root and paste in your token. You can access your token inside other files by importing the file.

{
    "token": "your-token"
}
 
 
 
import { token } from './config.json';

console.log(token);
 

 

Using environment variables

Environment variables are another common way of hiding sensitive information. To add environment variables, add them before running node index.js.

GUILDED_TOKEN=your-token node index.js
 
console.log(process.env.GUILDED_TOKEN);
 

TIP

It is recommended to use .env instead.

Using dotenv

A better solution to using environment variables would be by using a .env file. This spares you from having to paste your token every time you run your bot. Each line in a .env file should be a KEY=value pair.

The dotenv packageopen in new window can be used to load your .env file. Once installed, import the package to load your .env file.

npm install dotenv
 
yarn add dotenv
 
pnpm add dotenv
 
GUILDED_TOKEN=your-token
 
import 'dotenv/config';

console.log(process.env.GUILDED_TOKEN);
 

 

Git and .gitignore

It is important to ignore files and folders that are not needed or have sensitive information like your bot token. If you are committing your progress to a platform like GitHubopen in new window, create a .gitignore file in your project root and add the following inside:

node_modules
config.json
.env
 
 
 

TIP

You should never commit the node_modules folder to your repository as you can generate this folder by installing your dependencies from the package.json and lcok file.

Check out the Git documentation for .gitignoreopen in new window for more information!