13aal
13aal

Reputation: 1674

Requiring other files in node

I'm trying to require another file within a node project I'm working on; this will be a command line tool. What I'm trying to do is create a formatted color output using the following file format.js:

modules.exports = {
    warning: function(input){
        say("\033[31m" + input)
    },

    info: function(input){
        say("\033[36m" + input)
    }
}

From there I want to create the colored output and put it into a file named gen_email.js. That file has these two functions in it:

function say(input){
    console.log(input)
}

function helpPage(){
    say('');
    format.info("test")
}

When I attempt to run this it outputs the following:

C:\Users\thomas_j_perkins\bin\javascript\node\email\lib\format.js:1
(function (exports, require, module, __filename, __dirname) { modules.exports = {
                                                              ^

ReferenceError: modules is not defined
    at Object.<anonymous> (C:\Users\thomas_j_perkins\bin\javascript\node\email\lib\for
mat.js:1:63)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (C:\Users\thomas_j_perkins\bin\javascript\node\email\gen_ema
il.js:3:16)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)

I'm not understanding what I'm doing wrong, according to this I am requiring the file the correct way. What am I doing wrong here, do I need to move the say function into the other file?

Upvotes: 0

Views: 54

Answers (2)

user1554966
user1554966

Reputation: 433

It should be

module.exports = {
warning: function(input){
    say("\033[31m" + input)
},

info: function(input){
    say("\033[36m" + input)
}

}

in the other file

const format = require("whatEverPathIsOn/format.js")

if the file is under the same path just

const format = require("./format.js")

Upvotes: 1

SLaks
SLaks

Reputation: 888273

That should be module, not modules.

Upvotes: 0

Related Questions