#Has anyone gotten chalk to work with their discord bot split out into the index.js and then the comm

1 messages ยท Page 1 of 1 (latest)

sharp forum
#

Show me chalk file

zealous horizon
#

which one

#
{
    "name": "chalk",
    "version": "5.0.0",
    "description": "Terminal string styling done right",
    "license": "MIT",
    "repository": "chalk/chalk",
    "funding": "https://github.com/chalk/chalk?sponsor=1",
    "type": "module",
    "exports": "./source/index.js",
    "imports": {
        "#ansi-styles": "./source/vendor/ansi-styles/index.js",
        "#supports-color": {
            "node": "./source/vendor/supports-color/index.js",
            "default": "./source/vendor/supports-color/browser.js"
        }
    },
    "types": "./source/index.d.ts",
    "engines": {
        "node": "^12.17.0 || ^14.13 || >=16.0.0"
    },
    "scripts": {
        "test": "xo && c8 ava && tsd",
        "bench": "matcha benchmark.js"
    },
    "files": [
        "source",
        "!source/index.test-d.ts"
    ],
    "keywords": [
        "color",
        "colour",
        "colors",
        "terminal",
        "console",
        "cli",
        "string",
        "ansi",
        "style",
        "styles",
        "tty",
        "formatting",
        "rgb",
        "256",
        "shell",
        "xterm",
        "log",
        "logging",
        "command-line",
        "text"
    ],
    "devDependencies": {
        "@types/node": "^16.11.10",
        "ava": "^3.15.0",
        "c8": "^7.10.0",
        "color-convert": "^2.0.1",
        "execa": "^6.0.0",
        "log-update": "^5.0.0",
        "matcha": "^0.7.0",
        "tsd": "^0.19.0",
        "xo": "^0.47.0",
        "yoctodelay": "^2.0.0"
    },
    "xo": {
        "rules": {
            "unicorn/prefer-string-slice": "off"
        }
    },
    "c8": {
        "reporter": [
            "text",
            "lcov"
        ],
        "exclude": [
            "source/vendor"
        ]
    }
}
sharp forum
#

I guess chalk is in that folder in your case

zealous horizon
#

no chalk isnt a command it's a node package that lets u put colors in your logging

sharp forum
#

Oh, now it makes perfect sense

#

Do a console.log on chalk

zealous horizon
#

yeah it says chalk.blue isnt a function

#

but clearly it is

#

so im probably importing incorrectly

sharp forum
#

Like, I wanna see if you have anything at all in that module

#

console.log(chalk)

#

So I can see if it is an importing problem

zealous horizon
#

i see

#
Promise {
  [Module: null prototype] {
    Chalk: [class Chalk],
    chalkStderr: [Function: chalk] createChalk { level: 3 },
    default: [Function: chalk] createChalk { level: 3 },
    supportsColor: { level: 3, hasBasic: true, has256: true, has16m: true },
    supportsColorStderr: { level: 3, hasBasic: true, has256: true, has16m: true }
  }
}
sharp forum
#

Use await import("chalk")

zealous horizon
#

const chalk = await import('chalk');
^^^^^

SyntaxError: await is only valid in async functions and the top level bodies of modules

sharp forum
#

This is expected

zealous horizon
#

even when i put it in the modile.exports {} i still get the chalk.blue is not a function

sharp forum
#

I recommend you to use ES6 syntax when importing, and avoid dynamically import

zealous horizon
#

is that require?

sharp forum
#

require may work too

#

It doesn't return a promise, so it should work

zealous horizon
#
const chalk = require("chalk");
              ^

Error [ERR_REQUIRE_ESM]: require() of ES Module ... from ... not supported.
Instead change the require of index.js in ... to a dynamic import() which is available in all CommonJS modules.
#

this is what it says to do when i did require

sharp forum
#

Why don't you use ES6 syntax?

zealous horizon
#

I guess I don't know what ES6 syntax is for importing.

sharp forum
#

import chalk from 'chalk';

zealous horizon
#

back to the outside module thing:

import chalk from 'chalk';
^^^^^^

SyntaxError: Cannot use import statement outside a module
sharp forum
#

IMPORTANT: Chalk 5 is ESM. If you want to use Chalk with TypeScript or a build tool, you will probably want to use Chalk 4 for now

#

You're using TS?

zealous horizon
#

No clue tbh. I just looked at the discord.js tutorial and they made it seem real simple to install with no other considerations ๐Ÿคฃ

sharp forum
#

๐Ÿ˜‚

#

I have no clue either..

#

Where are you running your code? Node.js?

zealous horizon
#

yep

sharp forum
#

Do you use .ts or .js

zealous horizon
#

.js

sharp forum
#

In your package.json add "type": "module"

zealous horizon
#

now all my other require statements break

#

its ok this isnt worth colors in my console pepeHands

#

I'll just deal with it.

#

I appreciate your help though

sharp forum
#

Since I can't see all your project, is not easy to tell solutions that fits the best. If you insist on using require in other files, then you should do what the IMPORTANT notice tells you. Downgrade to version 4, and then use require("chalk") (also reversing the package.json)

zealous horizon
#

Yeah I'll look into chalk 4, thanks ๐Ÿ™‚

sharp forum
#

You're welcome