cute-stack icon indicating copy to clipboard operation
cute-stack copied to clipboard

TypeError: Cannot read property 'substr' of undefined

Open jamesdixon opened this issue 8 years ago • 19 comments

Loaded cute-stack programmatically and seeing the following when starting my server:

/Users/jamesdixon/Projects/scout/platform/api/node_modules/cute-stack/index.js:162
        .substr(0, col)
        ^

TypeError: Cannot read property 'substr' of undefined

.♦cute-stack/index.js  162,9
module.js  385,24   Module._compile
module.js  422,10   Module._extensions..js
module.js  357,32   Module.load
module.js  314,12   Module._load
module.js  367,17   Module.require
internal/module.js  16,19   require
.♦knex/lib/client.js  8,14   function (exports, require, module, __filename, __dirname) { [body] }
module.js  413,34   Module._compile
module.js  422,10   Module._extensions..js
module.js  357,32   Module.load
module.js  314,12   Module._load
module.js  367,17   Module.require
internal/module.js  16,19   require
.♦knex/lib/index.js  5,14   function (exports, require, module, __filename, __dirname) { [body] }
module.js  413,34   Module._compile

Note that the stack trace is formatted nicely, but obviously it crashes.

jamesdixon avatar Apr 22 '16 21:04 jamesdixon

@jamesdixon can you give me your node version?

davidmarkclements avatar Apr 23 '16 02:04 davidmarkclements

5.10.1

jamesdixon avatar Apr 23 '16 03:04 jamesdixon

@jamesdixon can you give me some sample code to check this against, I can't reproduce

davidmarkclements avatar Apr 23 '16 17:04 davidmarkclements

I'll try and get you something soon, but it may be a while 😄

jamesdixon avatar Apr 23 '16 17:04 jamesdixon

Most likely the "line" variable is invalid index into the source list in https://github.com/davidmarkclements/cute-stack/blob/master/index.js#L161 thus .substr is called on the undefined

bahmutov avatar Apr 26 '16 16:04 bahmutov

hey @bahmutov (hows things!) - yes I had the same thought, but I'd like to know what's causing that and why - I'm hesitant to bandaid it in case that just pushes the issue elsewhere

davidmarkclements avatar Apr 26 '16 16:04 davidmarkclements

Hi (very busy)! Yeah, it would be nice to actually see the failing code before protecting against it

bahmutov avatar Apr 26 '16 16:04 bahmutov

Sorry, gents. If it was a simple copy/paste, then I would take care of it immediately, but it's failing in a large, private project.

I can tell you that I'm using Hapi.js and simply calling require('cute-stack')(); at the beginning of my script that starts the server.

jamesdixon avatar Apr 26 '16 16:04 jamesdixon

I did some simple console.log debugging and it appears that @bahmutov is correct. line does appear to be an invalid index, which returns undefined and then subsequently fails when substr is called on it.

jamesdixon avatar Apr 26 '16 16:04 jamesdixon

Is there any on the fly transpilation or source rewriting or anything going after you require cute-stack?

The issue is we do a frame.getLineNumber() - that's a v8 call, that gives us a corresponding line number for that particular frame in the callstack.

To get the script source, which we're applying the line number to, we overwrite the internal Module.wrap function require('module') get's the internal Module constructor

So is there anything else in the code that's modifying scripts on the way through the module loading process?

Another thing to try, is to get a complete stack trace by setting Error.stackTraceLimit = Infinity

davidmarkclements avatar Apr 26 '16 16:04 davidmarkclements

also: try requiring cute-stack at the end of the server start script

davidmarkclements avatar Apr 26 '16 17:04 davidmarkclements

Here's the full stack trace:

/Users/jamesdixon/Projects/scout/platform/api/node_modules/cute-stack/index.js:162
        .substr(0, col)
        ^

TypeError: Cannot read property 'substr' of undefined

.♦cute-stack/index.js  162,9
module.js  385,24   Module._compile
module.js  422,10   Module._extensions..js
module.js  357,32   Module.load
module.js  314,12   Module._load
module.js  367,17   Module.require
internal/module.js  16,19   require
.♦knex/lib/client.js  8,14   function (exports, require, module, __filename, __dirname) { [body] }
module.js  413,34   Module._compile
module.js  422,10   Module._extensions..js
module.js  357,32   Module.load
module.js  314,12   Module._load
module.js  367,17   Module.require
internal/module.js  16,19   require
.♦knex/lib/index.js  5,14   function (exports, require, module, __filename, __dirname) { [body] }
module.js  413,34   Module._compile
module.js  422,10   Module._extensions..js
module.js  357,32   Module.load
module.js  314,12   Module._load
module.js  367,17   Module.require
internal/module.js  16,19   require
.♦knex/knex.js  8,18   function (exports, require, module, __filename, __dirname) { [body] }
module.js  413,34   Module._compile
module.js  422,10   Module._extensions..js
module.js  357,32   Module.load
module.js  314,12   Module._load
module.js  367,17   Module.require
internal/module.js  16,19   require
./config/bookshelf.js  7,14   function (exports, require, module, __filename, __dirname) { [body] }
module.js  413,34   Module._compile
module.js  422,10   Module._extensions..js
module.js  357,32   Module.load
module.js  314,12   Module._load
module.js  367,17   Module.require
internal/module.js  16,19   require
./config/config.js  8,19   function (exports, require, module, __filename, __dirname) { [body] }
module.js  413,34   Module._compile
module.js  422,10   Module._extensions..js
module.js  357,32   Module.load
module.js  314,12   Module._load
module.js  367,17   Module.require
internal/module.js  16,19   require
./server.js  25,18   module.exports.Glue.compose.then
./start.js  5,35   function (exports, require, module, __filename, __dirname) { [body] }
module.js  413,34   Module._compile
module.js  422,10   Module._extensions..js
module.js  357,32   Module.load
module.js  314,12   Module._load
module.js  447,10   Module.runMain
node.js  146,18   startup
node.js  404,3

As for anything modifying scripts, there's nothing that I'm aware of. No Babel transpilation or anything of that sort.

jamesdixon avatar Apr 26 '16 17:04 jamesdixon

Just tried adding the require after my server starts and that works!

jamesdixon avatar Apr 26 '16 17:04 jamesdixon

I believe I've narrowed down where the problem occurs based on that stack trace. It appears something happens with Glue that's causing the crash to occur. Here's my code for server.js:

Glue
        .compose(require('./config/config'), options)
        .then((server) => {

            // register rollbar to report uncaught exceptions
            if (isProductionLike) {
                server.plugins.icecreambar.default.handleUncaughtExceptions(process.env.ROLLBAR_ACCESS_TOKEN, {
                    exitOnUncaughtException: true
                });
            }

            // setup authentication strategy
            server.auth.strategy('jwt', 'jwt', 'required', {
                key: process.env.JWT_SECRET,
                validateFunc: require('./utils/auth/validateToken'),
                verifyOptions: {
                    algorithms: ['HS256']
                }
            });

            // register routes
            server.select('business-api').route(require('./routes/business').routes);

            return server;
        })
        .catch((err) => {

            console.error(err);
        });
};

Requiring cute-stack before Glue is called results in an error. However, placing the require after Glue has returned a server instance, works fine.

jamesdixon avatar Apr 26 '16 17:04 jamesdixon

FYI: Glue is a Hapi module that allows for composition of the server via an external configuration file: https://github.com/hapijs/glue

jamesdixon avatar Apr 26 '16 17:04 jamesdixon

yeah - so... not sure there's much to be done about this, other than identifying the case and outputting a more helpful message of what the issue might be

davidmarkclements avatar Apr 26 '16 17:04 davidmarkclements

Would it have anything to do with the fact that modules aren't loaded yet before Glue returns the server? Sorry, this is a little beyond me, so just speculating :)

jamesdixon avatar Apr 26 '16 17:04 jamesdixon

Same issue here. I had to move the require until after express starts. Works then, otherwise I get this error.

timmeade avatar Aug 19 '16 12:08 timmeade

Same here, it just works after another require.

neves avatar Sep 07 '17 14:09 neves