Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Domajno opened this issue Jun 20, 2017 · 1 comment
Open

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

Domajno opened this issue Jun 20, 2017 · 1 comment
Labels

Comments

@Domajno
Copy link

Domajno commented Jun 20, 2017

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

  • node version: v6.11.0
  • trails version: v2-latest
  • operating system: OSX Sierra 10.12
@tjwebb
Copy link
Member

tjwebb commented Jun 24, 2017

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'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants