In English
I'm getting some unexpected results when I try to load a module using require
in the Node.js shell to play around with it (as opposed to writing a script and running that with Node). In a script, for example, I can do:
var _ = require('grunt');
grunt.registerTask(/* ...*/);
And this works fine. But when I try to do this in the Node.js shell, it first can't find the module unless I specify the node_modules
directory, and then calling one of the modules' methods only works once before it stops.
In Tech-Speake
In current directory, I have the following subdirectory:
- node_modules
- lodash
- grunt
Now I want to play around with the Node.js shell, using one of the installed libraries:
$ node
> var _ = require('lodash');
undefined
> _ = require('./node_modules/lodash');
// Long output of function list
Now if I try to use lodash, it works once and then stops and I have to import it again:
_.reduce(/* ... */); //Works
_.reduce(/* ... */); // TypeError: Cannot call method 'reduce' of undefined