Skip to content
Sign upLog in
← Back to Community
How can I use multiple Files in a NodeJS Repl?
Profile icon
somecodingguy

I am a creating a chat app and I want to be able to use multiple server-side files. I have created a demo Repl as linked. I tried using const file = require('indexOne.js'); but it thinks I want to install. How can I do this?

Thank you

Voters
Profile icon
wJU5jipM3LWiftV
Profile icon
somecodingguy
Comments
hotnewtop
Profile icon
somecodingguy

but how would I access a function from the other file?

Profile icon
Coder100

use module.exports to export a variable, function, or object.

@somecodingguy

Profile icon
somecodingguy

@Coder100
Yes this works but how would I have multiple functions? Sorry

Profile icon
Coder100

using object destructoring, if your file was

module.exports = { a: "1", b: 2 };

then you can import like

const { a, b } = require("...");

@somecodingguy

Profile icon
somecodingguy

@Coder100
so how would I do it with functions? Sorry

Profile icon
Coder100

same thing
functions are variables???

@somecodingguy

Profile icon
somecodingguy

@Coder100
like that?

Profile icon
Coder100

no, here is the correct example:

module.exports = function something() {}
const something = require("...");

you only do destructuring for objects.


@somecodingguy

Profile icon
somecodingguy

@Coder100
yes I realized that but i gues i cant use multiple functions.

Profile icon
EpicGamer007

put ./ in the string for the require.