Monday, 15 May 2017

"require.resolve is not a function" in babel-node eval mode

I'm trying to write a Vim command to follow a require/import reference (respecting module-resolver aliases) in js/ES6 files.

If I type babel-node and drop into REPL mode, I can do something like:

> require.resolve('~');
/properly/resolved/path

However, if I try to eval the same expression (I need to eval to specify the path from Vim during runtime) I get:

$ babel-node -e 'console.log(require.resolve("~"))'
[eval]:1
console.log(require.resolve("./lib/webapp/lib"));
                    ^

TypeError: require.resolve is not a function
    at [eval]:1:-41
    at ContextifyScript.Script.runInThisContext (vm.js:23:33)
    at Object.runInThisContext (vm.js:95:38)
    at _eval (/usr/local/lib/node_modules/babel-cli/lib/_babel-node.js:99:23)
    at Object.<anonymous> (/usr/local/lib/node_modules/babel-cli/lib/_babel-node.js:119:16)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)

It looks like the tilde is properly resolved (~ points to ./lib/webapp/lib) but is then inserted into the command, and babel tries to execute the function after it (not finding it for some reason).

Am I doing something wrong here? Is this a bug?



via sdeleon28

No comments:

Post a Comment