简体   繁体   中英

TypeError: object is not a function - node.js / express

I get the following Type Error when I run the node.js application

/Users/khinester/Sandboxes/zeitgeist/Blade/server.js:5
  application = require("./.app/")();
                                  ^
TypeError: object is not a function
    at Object.<anonymous> (/Users/khinester/Sandboxes/zeitgeist/Blade/server.js:5:35)
    at Object.<anonymous> (/Users/khinester/Sandboxes/zeitgeist/Blade/server.js:21:4)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3
DEBUG: Program node server exited with code 8

DEBUG: Starting child process with 'node server'
00:55:26 - compiled src/social.coffee

I have structured my application as follows:

☺  tree -L 3 .
.
├── Cakefile
├── README.md
├── server.coffee
├── src
│   ├── config
│   │   ├── config.coffee
│   │   ├── errors.coffee
│   │   ├── i18n.coffee
│   │   ├── models.coffee
│   │   ├── passport.coffee
│   │   └── routes.coffee
│   ├── controllers
│   │   ├── api.coffee
│   │   ├── index.coffee
│   │   ├── login.coffee
│   │   ├── map.coffee
│   │   └── user.coffee
│   ├── index.coffee
│   ├── models
│   │   └── user
│   │       └── user.coffee
│   └── utils
│       ├── dbconnect.coffee
│       ├── emailer.coffee
│       ├── i18n.coffee
│       ├── logger.coffee
│       └── passport.coffee
└── views
    ├── 404.blade
    ├── 500.blade
    ├── footer.blade
    ├── forms
    │   ├── federated-form.blade
    │   ├── login-form.blade
    │   └── registration-form.blade
    ├── header.blade
    ├── index.blade
    └── user
        ├── create.blade
        ├── login.blade
        └── user.blade

my server.coffee is as follows:

application = require("./.app/")()
nowjs = require "now"
port = process.env.PORT or process.env.VMC_APP_PORT or process.env.VCAP_APP_PORT or 3000
server = application.listen(port)
everyone = nowjs.initialize(server)
console.log "Server running at http://127.0.0.1: "+ port  + "\nPress CTRL-C to stop server."

which compiles to:

// Generated by CoffeeScript 1.6.2
(function() {
  var application, everyone, nowjs, port, server;

  application = require("./.app/")();

  nowjs = require("now");

  if (!process.env.NODE_ENV) {
    process.env.NODE_ENV = "local";
  }

  port = process.env.PORT || process.env.VMC_APP_PORT || process.env.VCAP_APP_PORT || 3000;

  server = application.listen(port);

  everyone = nowjs.initialize(server);

  console.log("Server running at http://127.0.0.1: " + port + "\nPress CTRL-C to stop server.");

}).call(this);

and /app/index.coffee :

#Load external dependencies
express = require("express")
stylus = require("stylus")
mongoose = require("mongoose")
i18next = require "i18next"

#Load local dependencies
models = require("./config/models")
i18n = require("./config/i18n")
config = require("./config/config")
routes = require("./config/routes")

#Load database dependencies
dbconnection = require "./utils/dbconnect"

#Load logger
logger = require "./utils/logger"

# Initialize logger
logger.configure()
logCategory = "APP config"

#  Create Server
app = express()
logger.info "---- App server created ----", logCategory

#Exports
module.exports = ->
  #  Load Mongoose Models
  models app

  # Load i18next config
  i18n app
  # Init i18next
  i18next.init(app.i18n)
  i18next.registerAppHelper(app)
  #  Load Expressjs config
  config app

  #  Load routes config
  routes app

  app

logger.info "---- Modules loaded into namespace ----", logCategory
# Connect to database
dbconnection.init (result) ->
  if result
    logger.info "Database initialized", logCategory

so, every time i run the application, i get the TypeError, but the application runs!

what am i missing and not understanding here?

any advice much appreciated.

I have a guess - maybe it's some kind of a race condition between compiling the coffee script and running the js?

Either way, if I were you I would try to isolate the problem - open node (or coffee) in interactive mode - require your app code - can you call the function? is it a function? Object? try to use coffee to run your app instead of node. do you get the same error?

another thing - you said it's in ./app but in your code it's in ./.app - I guess it's not a problem because a different error would occur.

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