0

I have the following code:

module.exports = function(db, threads) {
    var self = this;
    this.tick = function() {
        //process.chdir("AA/BB"); // This line gives error "Error: ENOENT: no such file or directory, uv_chdir"
    }

    this.start = function() {
        process.chdir("AA/BB"); // this works
        console.log("The new working directory is " + process.cwd());
        self.tick(process);
    }
}

I call start() from another class like this:

var man = require('./temp.js');
var manager = new man(db, threads);
manager.start();

Can somebody explain why I can change directory from start(), but not from tick()? Do I need to pass something between these functions?

Thanks.

Monday to Friday
  • 239
  • 5
  • 16
  • in line - self.tick(process); you are passing a variable process, but "this.tick = function() { " does not have any parameters. – Justinjs Jun 19 '18 at 14:18
  • I tried adding parameters to the function and using them, it didn't help :( Is this the way to go? Pass process to the function and somehow use it? – Monday to Friday Jun 20 '18 at 07:21

2 Answers2

0

try this so we could catch some more information about the error

 this.tick = function() {
   try {
    console.log('__dirname: ', __dirname);
    console.log("The directory from which node command is called is " + process.cwd());
    process.chdir("root_path/AA/BB");
}
catch (err) {
        //handle the error here
}
}

just be sure that you give the correct root path.

./ and process.cwd() refers to the directory on which the node command was called. It does not refer to the directory of the file being executed.

__dirname refers to the directory where the file being executed resides.

So be careful, the next time you use ./, process.cwd(), or __dirname. Make sure what you are using is exactly what you want.

Justinjs
  • 130
  • 7
0

You used relative dir path AA/BB, by calling start() the process has already chdired to that directory relative to the cwd, ./AA/BB.

Calling tick() consequently will make it look up AA/BB in the current cwd ./AA/BB, e.g. ./AA/BB/AA/BB, which doesn't exists.

dotslashlu
  • 3,361
  • 4
  • 29
  • 56
  • Yes you are right :( I was checking __dirname and it was still pointing to the parent directory, so I assumed that chdir didn't work. __dirname must mean something else. Thanks! – Monday to Friday Jun 20 '18 at 15:20