C
C
Cyber_bober2016-10-21 11:56:09
Node.js
Cyber_bober, 2016-10-21 11:56:09

Why do modules require a variable?

Hello, I am including two modules that fs requires.

const fs = require('fs');

var module1 = require('./module1');
var module2 = require('./module2');

Here is the trace
/Users/cyberbober/Desktop/nodejs/2/module1.js:8
        fs.readdir(path, (err, files) => {
        ^

ReferenceError: fs is not defined
    at getFiles (/Users/cyberbober/Desktop/nodejs/2/module1.js:8:9)
    at Seek (/Users/cyberbober/Desktop/nodejs/2/module1.js:59:1)
    at Object.<anonymous> (/Users/cyberbober/Desktop/nodejs/2/module1.js:69:30)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
    at Module.runMain (module.js:575:10)
    at run (bootstrap_node.js:352:7)

To which, at startup, I get an error from modules of the form “fs undefined”. What am I doing wrong? Or do I need to declare the fs variable in each module?
Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
Zlatoslav Desyatnikov, 2016-10-21
@Cyber_bober

You are missing the fs variable in module1.
The variables here are not global, and in each module you need to separately connect dependencies. Good luck!

V
Vitaly, 2016-10-21
@vshvydky

fs should probably be connected inside the modules, since you access fs from there.
The node does not have many global variables, these are module, console, process , maybe something else, I don’t remember. All other functions require inclusion in those files, links in which they are maintained. Yes, and in the log it gives you a specific line of a specific file where the error occurred, I think this is obvious.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question