最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can i import thing in js ? (discord.js) - Stack Overflow

programmeradmin0浏览0评论

I got a problem with import in js for a discord bot. This is a part of my code :

import * as Discord from "discord.js";
import * as fs from "fs";

import config from "./config.json";

const client = new Discord.Client();

But, i got an error :

import * as Discord from "discord.js";
       ^

SyntaxError: Unexpected token *
    at Module._pile (internal/modules/cjs/loader.js:722:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:798:10)
    at Module.load (internal/modules/cjs/loader.js:645:32)
    at Function.Module._load (internal/modules/cjs/loader.js:560:12)
    at internal/modules/esm/translators.js:100:15
    at Object.meta.done (internal/modules/esm/create_dynamic_module.js:48:9)
    at file:///C:/Users/Teo/Desktop/altv-server/resources/discord/bot/index.js:9:13
    at ModuleJob.run (internal/modules/esm/module_job.js:111:37)
    at processTicksAndRejections (internal/process/task_queues.js:89:5)
    at async Loader.import (internal/modules/esm/loader.js:131:24)

I don't know why I'm getting this error. Can someone help me to solve my issue ?

I got a problem with import in js for a discord bot. This is a part of my code :

import * as Discord from "discord.js";
import * as fs from "fs";

import config from "./config.json";

const client = new Discord.Client();

But, i got an error :

import * as Discord from "discord.js";
       ^

SyntaxError: Unexpected token *
    at Module._pile (internal/modules/cjs/loader.js:722:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:798:10)
    at Module.load (internal/modules/cjs/loader.js:645:32)
    at Function.Module._load (internal/modules/cjs/loader.js:560:12)
    at internal/modules/esm/translators.js:100:15
    at Object.meta.done (internal/modules/esm/create_dynamic_module.js:48:9)
    at file:///C:/Users/Teo/Desktop/altv-server/resources/discord/bot/index.js:9:13
    at ModuleJob.run (internal/modules/esm/module_job.js:111:37)
    at processTicksAndRejections (internal/process/task_queues.js:89:5)
    at async Loader.import (internal/modules/esm/loader.js:131:24)

I don't know why I'm getting this error. Can someone help me to solve my issue ?

Share Improve this question asked May 6, 2020 at 15:21 ScoffgardScoffgard 131 silver badge4 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

According to dicord.js documentation since they released their package as a mon js package in order to load it, you should use ES5 importing style with require.

So it should be like this:

const Discord = require('discord.js');

Have you tried using the Require Library?

You can't selectively load only the pieces you need with require but with imports, you can selectively load only the pieces you need. That can save memory.

const Discord = require('discord.js/browser');

You can try:

const { Client, Events, GatewayIntentBits } = require("discord.js");

source: // https://discordjs.guide/additional-info/es6-syntax.html#template-literals

发布评论

评论列表(0)

  1. 暂无评论