【问题标题】:NodeJS, Circular dependency: main module not in the cache?NodeJS,循环依赖:主模块不在缓存中?
【发布时间】:2026-01-14 09:35:01
【问题描述】:

我正在尝试在我的应用程序的一个子模块中访问 Express 'app' 变量。 一种方法是在调用子模块时将其作为参数传递,但我想避免它。 This solution 提到这是可能的,但是每次我尝试它时我都会收到异常说对象不是函数(下面的回溯)

server.coffee

express = require 'express'

# Application Config
config = require './lib/config/config'
app = express()

# Start server
server = app.listen config.port, ->
  logger.info "Express server listening on port #{config.port} in #{app.get("env")} mode"

# load procesor
fooBarStreamProcessor = require('./lib/modules/fooBarStreamProcessor')
fooBarUpstream = new fooBarStreamProcessor(config.fooBar_host, config.fooBar_port)

# Expose app
exports = module.exports = app

fooBarStreamProcessor.coffee

  events             = require('events')
  eventEmitter       = new events.EventEmitter()
  app = require("../../server")

  console.log(app.get('something'))

  module.exports = ->
    return "I am some function"

追溯

  TypeError: object is not a function
    at Object.<anonymous> (~/devel/exampleproj/server.coffee:30:18)
    at Object.<anonymous> (~/devel/exampleproj/server.coffee:1:1)
    at Module._compile (module.js:456:26)
    at Object.loadFile (~/devel/exampleproj/node_modules/coffee-script/lib/coffee-script/register.js:16:19)
    at Module.load (~/devel/exampleproj/node_modules/coffee-script/lib/coffee-script/register.js:45:36)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (~/devel/exampleproj/lib/modules/fooBarStreamProcessor.coffee:9:7)
    at Object.<anonymous> (~/devel/exampleproj/lib/modules/fooBarStreamProcessor.coffee:1:1)
    at Module._compile (module.js:456:26)
    at Object.loadFile (~/devel/exampleproj/node_modules/coffee-script/lib/coffee-script/register.js:16:19)
    at Module.load (~/devel/exampleproj/node_modules/coffee-script/lib/coffee-script/register.js:45:36)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (~/devel/exampleproj/server.coffee:3:25)
    at Object.<anonymous> (~/devel/exampleproj/server.coffee:1:1)
    at Module._compile (module.js:456:26)

经检查,fooBarStreamProcessor 似乎设置为{},而不是fooBarStreamProcessor.coffee 所需的实际功能。 在进行故障排除时,我尝试在server.coffee 的第一行中包含类似require("./server") 的内容,它消除了错误,但代码被执行了两次。

有什么办法可以解决这个问题吗?

【问题讨论】:

    标签: node.js express coffeescript


    【解决方案1】:

    问题是这一行将在您包含子模块之后执行

    # Expose app
    exports = module.exports = app
    

    尝试将其移至应用定义:

    app = module.exports = express()
    

    并确保在需要子模块之前在应用程序上设置了所需的任何内容。

    【讨论】:

    • 按照上面的建议进行了更改,但错误完全相同。你可以自己试试,我已经上传示例代码到github.com/skrobul/cyclic-deps-problem/archive/master.zip
    • 这一定是一个coffeescript错误。我做了建议的更改,编译为 JS,它似乎工作正常。我对coffeescript不是很熟悉,所以也许其他人可以帮忙。
    • 谢谢,您为我指明了正确的方向。它确实是 coffeescript 的全局函数包装器。
    【解决方案2】:

    是的,你有一个循环依赖。通常这意味着在需要时而不是在文件开头加载一个,但从您的示例代码中不清楚何时会这样做。在fooBarStreamProcessor.coffee 中,您可以在要导出的函数中要求server

    events             = require('events')
    eventEmitter       = new events.EventEmitter()
    
    module.exports = ->
      app = require("../../server")
      console.log(app.get('something'))
    
      return "I am some function"
    

    【讨论】:

      【解决方案3】:

      找到了解决方案 - 是 Coffeescripts 全局包装器导致了问题。 使用 '-b' (bare) 选项编译 coffeescript 可以防止这种情况发生。有趣的是,在原地运行coffeescript(没有-c 标志)会导致出现相同的错误。

      【讨论】:

        最近更新 更多