简体   繁体   中英

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

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM