title: "Error: Cannot find module 'less'" Node.js module loading preference/order/cache?
body: <p>Here's the situation…  So I've created a Node.js module that acts as an interface to some Node.js template engines, <a href="https://github.com/viatropos/shift.js" rel="noreferrer">Shift.js</a>.  That is included inside another Node.js module, <a href="https://github.com/viatropos/design.io" rel="noreferrer">Design.io</a> (it's specified Design.io's package.json <code>dependencies</code> block).  Design.io watches files for changes.</p>
  
  <p>Then you build an app and <code>require('design.io')</code>.  You also install, say, Less and Stylus into your project.</p>
  
  <p>This way, when you save a <code>.less</code> or <code>.styl</code> file, a chain of methods are called:</p>
  
  <ol>
  <li><code>require("design.io")</code> gets notified of the filesystem event.  Design.io then calls</li>
  <li><code>require('shift').render(string, extension, callback)</code>.  Shift.js then calls</li>
  <li><code>require(moduleFor(extension)) # require("less")</code>.  The string is compiled.</li>
  </ol>
  
  <p>In my app (current working directory) I have installed less and stylus:</p>
  
  <pre><code>npm install less stylus
  </code></pre>
  
  <p>The problem I'm having is, in <em>step 3</em> which is called from within the Shift.js module, I get errors like this:</p>
  
  <pre><code>Error: Cannot find module 'less'
      at Function._resolveFilename (module.js:334:11)
      at Function._load (module.js:279:25)
      at Module.require (module.js:357:17)
      at require (module.js:368:17)
      at Less.engine (/Users/viatropos/Documents/git/plugins/design.io/node_modules/shift/lib/shift/less.js:6:14)
      at Less.render (/Users/viatropos/Documents/git/plugins/design.io/node_modules/shift/lib/shift/less.js:18:21)
      at /Users/viatropos/Documents/git/plugins/design.io/node_modules/shift/lib/shift.js:69:23
      at /Users/viatropos/Documents/git/plugins/design.io/node_modules/async/lib/async.js:118:13
      at Object.forEachSeries (/Users/viatropos/Documents/git/plugins/design.io/node_modules/async/lib/async.js:134:9)
      at Object.render (/Users/viatropos/Documents/git/plugins/design.io/node_modules/shift/lib/shift.js:78:31)
  </code></pre>
  
  <p>My question is, why is this happening?  I thought that you could dynamically require libraries from a module as long as they were installed <em>somewhere</em>…  What am I missing?</p>
  
  <p>The goal would be that libraries like Shift.js wouldn't have to define every single dependency in <code>package.json</code>…  For an "interface to the template engines" library, that would require too many dependencies that the app would probably never be using.</p>
  
  <p>Thanks for the help, hope that was somewhat clear.</p>