i'm not a node core dev, but i think this is correct. the require function
doesnt care about the cwd, it resolves alwlays relative to the module on
the filesystem. this is a good thing, because you can always rely on
require, __dirname and local dependency resolution. if require would use
the cwd for resolving module paths, it could not guarantee to always load
the dependencies you've specified for your module.
to set the cwd is more for your application logic, so it can find and store
the resources independently from the install dir.
Am Mittwoch, 16. April 2014 08:13:23 UTC+2 schrieb Aaron Burrow:
>
> directory looks like this
> ./main.js
> ./say-hi.js
> ./temp/thing.js
>
> code like diss
> main.js
> ========================================
> #!/usr/local/bin/node
>
> var child_process = require('child_process');
>
> console.log('main');
> console.log('cwd:', process.cwd());
> console.log('__dirname:', __dirname);
>
> var child_options = {
> cwd: "./temp",
> stdio: [ 'ignore', process.out, process.err ],
> env: process.env
> };
>
> var args = [];
> var fork_path = __dirname + "/say-hi.js";
> child_process.fork(fork_path, args, child_options);
> ========================================
>
> say-hi.js
> ========================================
> #!/usr/local/bin/node
>
> console.log('say-hi');
> console.log('cwd:', process.cwd());
> console.log('__dirname:', __dirname);
>
> t = require('./thing');
> ========================================
>
> temp/thing.js
> ========================================
> #!/usr/local/bin/node
>
> console.log('thing');
> ========================================
>
> run c0de
> prompt> ./main.js
> main
> cwd: /home/burrows/c/fork
> __dirname: /home/burrows/c/fork
> say-hi
> cwd: /home/burrows/c/fork/temp
> __dirname: /home/burrows/c/fork
> module.js:340
> throw err;
> ^
> Error: Cannot find module './thing'
> at Function.Module._resolveFilename (module.js:338:15)
> at Function.Module._load (module.js:280:25)
> at Module.require (module.js:364:17)
> at require (module.js:380:17)
> at Object.<anonymous> (/home/burrows/c/fork/say-hi.js:7:5)
> at Module._compile (module.js:456:26)
> at Object.Module._extensions..js (module.js:508:10)
> at Module.load (module.js:356:32)
> at Function.Module._load (module.js:312:12)
> at Function.Module.runMain (module.js:540:10)
>
> process.cwd() updates but __dirname doesn't; presumably require uses
> __dirname to lookup files.
> I think the relevant path resolution code is in node/lib/module.js if you
> want to see how 'require' functions.
>
> Is this behavior correct and documented somewhere?
>
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.