简体   繁体   English

Node.js集群子进程路径

[英]Node.js cluster child process path

Started exploring node.js and faced the following problem 开始探索node.js并遇到以下问题

Let's say I've got 3 files: start.js, core/core.js and core/child.js 假设我有3个文件:start.js,core / core.js和core / child.js

  1. start.js requires core.js in the code start.js在代码中需要core.js
  2. core.js creates a child process (core/child.js) using cluster with these settings core.js使用具有以下设置的集群创建子进程(core / child.js)

     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 core.js和child.js在同一个文件夹中,但是如果我使用,则会收到错误消息(未找到)

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 如果路径更长一点,我没有问题,只是试图了解为什么我不能使用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() . require()相对于当前代码文件的位置有效,但是Node.js中的大多数其他操作(包括启动其他进程)都相对于当前工作目录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. 如果需要生成相对于当前文件的路径,则可以在运行时使用每个模块中可用的__dirname变量。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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