Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

bug with require #1694

Closed
bobrik opened this issue Sep 13, 2011 · 4 comments
Closed

bug with require #1694

bobrik opened this issue Sep 13, 2011 · 4 comments

Comments

@bobrik
Copy link

bobrik commented Sep 13, 2011

I've got a strange behavior with require when in some cases require() at the beginning of the file causes undefined instead of contents of exports object. Lazy require in methods works well in that case (I really hope so).

You may see example code here: node-require-bug.

Sorry for my poor English explanation, I hope you'll see everything in example.

@kurokikaze
Copy link

Strange behavior. When you move require('UserFactory') inside getAdminUser functtion body, it works.

@bobrik
Copy link
Author

bobrik commented Sep 13, 2011

yep, that's my hack for now. But I suppose there may be some troubles too

@isaacs
Copy link

isaacs commented Sep 13, 2011

This is by design.

When you have circular require() calls, a module might not be done being executed when it is returned.

Consider this:

// a.js
console.log("a starting")
exports.done = false
var b = require("./b.js")
console.log("in a, b.done = %j", b.done)
exports.done = true
console.log("a done")
// b.js
console.log("b starting")
exports.done = false
var a = require("./a.js")
console.log("in b, a.done = %j", a.done)
exports.done = true
console.log("b done")
// main.js
console.log("main starting")
var a = require("./a.js")
var b = require("./b.js")
console.log("in main, a.done=%j, b.done=%j", a.done, b.done)
$ node main.js 
main starting
a starting
b starting
in b, a.done = false
b done
in a, b.done = true
a done
in main, a.done=true, b.done=true

Todo: Add this to the module docs.

@ghost ghost assigned isaacs Sep 13, 2011
@isaacs isaacs closed this as completed in dc8f4ee Sep 13, 2011
@kurokikaze
Copy link

Thanks for clarifications :)

lyonplus pushed a commit to lyonplus/node that referenced this issue May 22, 2015
PR-URL: nodejs/node#1679

Notable Changes:

* win,node-gyp: the delay-load hook for windows addons has now been
correctly enabled by default, it had wrongly defaulted to off in the
release version of 2.0.0 (Bert Belder) nodejs#1433
* os: tmpdir()'s trailing slash stripping has been refined to fix an
issue when the temp directory is at '/'. Also considers which slash is
used by the operating system. (cjihrig) nodejs#1673
* tls: default ciphers have been updated to use gcm and aes128 (Mike
MacCana) nodejs#1660
* build: v8 snapshots have been re-enabled by default as suggested by
the v8 team, since prior security issues have been resolved. This
should give some perf improvements to both startup and vm context
creation. (Trevor Norris) nodejs#1663
* src: fixed preload modules not working when other flags were used
before --require (Yosuke Furukawa) nodejs#1694
* dgram: fixed send()'s callback not being asynchronous (Yosuke
Furukawa) nodejs#1313
* readline: emitKeys now keeps buffering data until it has enough to
parse. This fixes an issue with parsing split escapes. (Alex Kocharin)
* cluster: works now properly emit 'disconnect' to cluser.worker (Oleg
Elifantiev) nodejs#1386
events: uncaught errors now provide some context (Evan Lucas) nodejs#1654
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants