Home > OS >  SyntaxError : Unexepected token 'export'
SyntaxError : Unexepected token 'export'

Time:10-14

So yeah I'll just get to the point, an error gets thrown at me, while I don't know what to do, it's quite weird because the code is fine there is just something wrong with what I'm saying here. Here is where the problem is:

const { codeBlock } = require("@discordjs/builders");
const { ExplicitContentFilterLevels } = require("discord.js/typings/enums.d.ts");

async function clean(client, text) {
  if (text && text.container.name == "Promise")
    text = await text;
  if (typeof text !== "string")
    text = require("util").inspect(text, {depth: 1});

  text = text
    .replace(/`/g, "`"   String.fromCharCode(8023))
    .replace(/@/g, "@"   String.fromCharCode(8023));

  text = text.replaceAll(client.token, "[REDACTED]");

  return text;
}

exports.run = async (client, message, args, level) => {
  const code = args.join(" ");
  const evaled = eval(code);
  const cleaned = await clean(client, evaled);
  message.channel.send(codeBlock("js", cleaned));
};

exports.conf = {
  enabled: true,
  guildOnly: false,
  aliases: [],
  permLevel: "Bot Owner"
};

exports.help = {
  name: "eval",
  category: "System",
  description: "Evaluates arbitrary javascript.",
  usage: "eval [...code]"
};

here is where the error gets thrown at me:

ERROR Unhandled rejection: SyntaxError: Unexpected token 'export' 
C:\Users\emilb\OneDrive\Desktop\Tap\node_modules\discord.js\typings\enums.d.ts:4
export const enum ActivityTypes {
^^^^^^

SyntaxError: Unexpected token 'export'
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:94:18)
    at Object.<anonymous> (C:\Users\emilb\OneDrive\Desktop\Tap\commands\eval.js:2:41)

CodePudding user response:

You are not supposed to import a *.d.ts file. Those files are TypeScript definition files, which are there to help you IDE offer better type hints.

Change the import to this:

const { ExplicitContentFilterLevels } = require("discord.js/src/util/Constants");

Or even better change it to the line below, because that way you are not tied to the internal structure of the Discord package.

const { Constants: { ExplicitContentFilterLevels } } = require('discord.js');

CodePudding user response:

You can't import from a Typescript (.ts) file when your project is Javascript. (Not to mention you shouldn't import from .d.ts files anyway.)

Since you're not using ExplicitContentFilterLevels, just remove the line attempting to import it.

  • Related