node icon indicating copy to clipboard operation
node copied to clipboard

"is not a function" error points to the wrong code point

Open markushedlund opened this issue 11 years ago • 7 comments

Hi! Stumbled upon this minor issue where an "is not a function" error points to the wrong code point.

Test case:

// config.js
module.exports = function(path) {
    var config = require(path ? path.resolve(process.cwd(), path) : '../config');
    return config;
};
// index.js
var config = require('./config')('config');

Result:

config.js:4
    var config = require(path ? path.resolve(process.cwd(), path) : '../config
                                                     ^
TypeError: process.cwd is not a function
    at module.exports (config.js:4:54)

Expected (moved the arrow):

    var config = require(path ? path.resolve(process.cwd(), path) : '../config
                                     ^

Tested with:

  • nodejs v0.12.5
  • iojs v2.3.1

It really is a minor issue, but can be confusing when debugging.

markushedlund avatar Jul 01 '15 09:07 markushedlund

The error is that process.cwd is not a function and the arrow points at process.cwd, what is the problem?

petkaantonov avatar Jul 01 '15 09:07 petkaantonov

console.log(process.cwd); returns [Function: cwd]

console.log(path.resolve); returns undefined

markushedlund avatar Jul 01 '15 09:07 markushedlund

Since the message says process.cwd is not a function then it's either not a function or the bug is in v8.

petkaantonov avatar Jul 01 '15 09:07 petkaantonov

Probably an issue with v8, it sends the wrong error message in ternaries:

> var v = 'string';
undefined
> true ? v.resolve(process.cwd()) : false
TypeError: process.cwd is not a function
    at repl:1:26
    ...
> v.resolve(process.cwd())
TypeError: v.resolve is not a function
    at repl:1:3
    ...

brendanashworth avatar Jul 01 '15 09:07 brendanashworth

It seems correct in 4.5:

V8 version 4.5.0 (candidate) [console: dumb]
d8> var process = { cwd: function(){} }
undefined
d8> var v = 'string';
undefined
d8> true ? v.resolve(process.cwd()) : false
(d8):1: TypeError: v.resolve is not a function
true ? v.resolve(process.cwd()) : false
         ^
TypeError: v.resolve is not a function
    at (d8):1:10

d8> v.resolve(process.cwd())
(d8):1: TypeError: v.resolve is not a function
v.resolve(process.cwd())
  ^
TypeError: v.resolve is not a function
    at (d8):1:3

targos avatar Jul 01 '15 13:07 targos

Not sure if this is the same issue

thefourtheye avatar Jul 05 '15 22:07 thefourtheye

Seems to work with the v8 upgrade in 3.0.0:

brendanashworth in ~ $ node -v
v3.0.0
brendanashworth in ~ $ node
> var v = 'string'
undefined
> true ? v.resolve(process.cwd()) : false
TypeError: v.resolve is not a function
    at repl:1:10
    ...
brendanashworth in ~ $ node -v
v2.3.1
brendanashworth in ~ $ node
> var v = 'string'
undefined
> true ? v.resolve(process.cwd()) : false
TypeError: process.cwd is not a function
    at repl:1:26
    ...

Thanks for the bug report! I'll close this, please reopen / comment if it comes back.

brendanashworth avatar Aug 10 '15 05:08 brendanashworth