Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I am making a Discord bot using Javascript. I am using a .env file and the dotenv library for security. I am getting an error with importing dotenv

I am making my first Discord bot using Javascript. I decided to use a .env file to hide my token so I can push the files to Github. To do that I had to use the dotenv library. When I try to import that library, I get an error.

import { config } from ‘dotenv’; ^^^^^^

SyntaxError: Cannot use import statement outside a module

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I get this error with the first import statement only, I know that when I switch them I get the same error but with the other one.
(I am using nodemon, Discord.js and dotenv only)

import { config } from 'dotenv';
import { Client } from 'discord.js';

config();

const client = new Client({ intents: ['Guilds', 'GuildMessages'] });
const TOKEN = process.env.t;

client.login(TOKEN);

Here is my code.

What did I do wrong?
How do I fix this?
How do I prevent this in the future?
Is there a more efficient way?

>Solution :

Add "type": "module" to your package.json file.

or

<script type="module" src="your script addres"></script>

or use require

const config = require('dotenv')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading