Description
Which application is this bug report for?
Documentation
Issue description
The code example in the documentation uses import
and top-level await
:
import { REST, Routes } from 'discord.js';
await rest.put(...);
This throws a SyntaxError
unless the project is set up as an ES module, either by:
-
Adding
"type": "module"
in package.json, or -
Renaming the file extension to
.mjs
Even though the TOP they mentions "Node.js 22.12.0 or newer", that alone isn’t enough — top-level await
is only supported in ES modules.
The code example in the documentation (see here) uses import
and top-level await
:
Suggestion
To run the code exactly as written (using import
and top-level await
), one of the following changes should be made:
- Update the documentation to mention that the project must use ES Modules by:
- Adding
"type": "module"
topackage.json
, or - Renaming the file to
.mjs
- Adding
OR
- Change the code example to use CommonJS (
require
) and wrap theawait
in an IIFE:
const { REST, Routes } = require('discord.js');
(async () => {
await rest.put(...);
})();
Steps to Reproduce
-
Create a new file named
deploy-commands.js
-
Copy the example code from the documentation:
import { REST, Routes } from 'discord.js'; const rest = new REST({ version: '10' }).setToken('your-token'); await rest.put(Routes.applicationCommands('your-client-id'), { body: [{ name: 'ping', description: 'Replies with Pong!' }], });
-
Run the file using Node.js 22+:
node deploy-commands.js
-
Observe the following error:
SyntaxError: Cannot use import statement outside a module
or
SyntaxError: await is only valid in async functions and the top level bodies of modules
The code example in the documentation (see here) uses import
and top-level await
:
Versions
- Node.js: v22.16.0
- discord.js: v14.19.3
- OS: Windows 10
Issue priority
Medium (should be fixed soon)