Aleksandr Belkin
Aleksandr Belkin

Reputation: 422

Node.js cluster child process path

Started exploring node.js and faced the following problem

Let's say I've got 3 files: start.js, core/core.js and core/child.js

  1. start.js requires core.js in the code
  2. core.js creates a child process (core/child.js) using cluster with these settings

    cluster.setupMaster({
        exec: './core/child.js'
    });
    

core.js and child.js are in the same folder, but I get an error (not found) if I use

exec: './child.js'

Didn't find anything similar in documentation, however

require('./child.js')

works perfectly. I have no problem if the path is a bit longer, just trying to understand why can't I use path local to core.js

Upvotes: 0

Views: 1178

Answers (1)

Bret Copeland
Bret Copeland

Reputation: 24000

require() works relative to the location of the current code file, but most other operations in Node.js (including launching other processes) are relative to the current working directory process.cwd().

If you need to generate a path relative to the current file, you can use the __dirname variable available in every module at runtime.

var childPath = require('path').join(__dirname, 'child.js');

Upvotes: 1

Related Questions