Ask coding questions

← Back to all posts
How can I use multiple Files in a NodeJS Repl?
somecodingguy (0)

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

Comments
hotnewtop
somecodingguy (0)

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

Coder100 (18137)

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

somecodingguy (0)

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

Coder100 (18137)

using object destructoring, if your file was

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

then you can import like

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

@somecodingguy

somecodingguy (0)

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

Coder100 (18137)

same thing
functions are variables??? @somecodingguy

Coder100 (18137)

no, here is the correct example:

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

you only do destructuring for objects.
@somecodingguy

somecodingguy (0)

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

EpicGamer007 (1736)

put ./ in the string for the require.