trailsjs / trails

:evergreen_tree: Modern Web Application Framework for Node.js.
http://trailsjs.io
Other
1.66k stars 70 forks source link

TypeError: Cannot read property 'filename' of undefined #293

Open Domajno opened 7 years ago

Domajno commented 7 years ago

Issue Description

When I open node and type following commands (same as inside server.js) I get following error:

$ node
> const TrailsApp = require('trails');
undefined
> const app = require('./');
undefined
> const server = new TrailsApp(app);
TypeError: Cannot read property 'filename' of undefined
    at Function.buildConfig (/node_modules/trails/lib/Configuration.js:58:52)
    at new Configuration (/node_modules/trails/lib/Configuration.js:16:31)
    at TrailsApp (/horizon-core/node_modules/trails/index.js:61:16)
    at repl:1:16
    at sigintHandlersWrap (vm.js:22:35)
    at sigintHandlersWrap (vm.js:73:12)
    at ContextifyScript.Script.runInThisContext (vm.js:21:12)
    at REPLServer.defaultEval (repl.js:340:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.<anonymous> (repl.js:538:10)
    at emitOne (events.js:101:20)
    at REPLServer.emit (events.js:188:7)
    at REPLServer.Interface._onLine (readline.js:233:10)
    at REPLServer.Interface._line (readline.js:585:8)
    at REPLServer.Interface._ttyWrite (readline.js:862:14)

When I run node server.js everything is fine. It is because require.main is not defined when node is running without a script being specified. Is is an expected behaviour? It is causing us headaches when we try to use Jest for testing.

Environment

tjwebb commented 7 years ago

This is because Trails figures out its current root directory from require.main, which is the module from which it is required. Because you're running from the node repl, there is no main module. I'm sorry you're having this issue -- we hadn't considered this use case in designing the configuration.

Try setting require.main manually before instantiating Trails, e.g.

require.main = process.env.PWD + '/server.js'