Ask coding questions

← Back to all posts
How do I run a file from my main file
RetroBotMaker (0)

Hi there I trying to run a file called clyde.js from my index.js but I don't know the code pls help (This is for a discord bot)

Comments
hotnewtop
Andy_4sberg (50)

I'm not completely sure, but I hope this helps:

import <FUNCTION> from '/clyde.js'
Andy_4sberg (50)

@RetroBotMaker, <FUNCTION> must be a function inside clyde.js, you call the function and run it.

RetroBotMaker (0)

@Andy_4sberg I tryed it but is said
"(node:4178) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
/home/runner/Music-Man-Clyde/index.js:67
import code from '/clyde.js'
^^^^^^"

Andy_4sberg (50)

@RetroBotMaker, This worked for me:

var clyde = require('./clyde.js');
Wumi4 (482)

You can use the exec function from child_process.

const { exec } = require("child_process");

exec("node clyde.js", (err, strerr, stdout) => {
    if (err) {
        console.log(`An error occurred: {err}`);
    }

    if (stderr) {
         console.log(`stderr: {stderr}`);
    }

    if (stdout) {
          console.log(`stdout: {stdout}`);
    }
});
RetroBotMaker (0)

@Wumi4 I trying to do it with a discord bot and it does not work

Wumi4 (482)

@RetroBotMaker Oh, so you want to execute the clyde.js though your Discord bot? Then sorry, I don't know how. But at least, you have learnt that you should give more information to your post, or you will waste your time.