Help requiring an npm module in node.js

It seems to me that I missed some very simple ones here ...

So, I am installing the npm library with npm install somelib . And from what I read, I would just have to do

 var somelib = require('somelib'); 

But he cannot find anything. I have a node_modules directory in the root of my application, but it does not seem to pick it up.

I tried require.paths.push('node_modules') , but that does not help. The only thing that works is:

 require.paths.unshift('.'); var somelib = require('node_modules/somelib/lib/somelib'); 

Which makes me feel like it is much more than I need to download the npm library. What am I doing wrong here? I thought installing modules in an application means I don't need futz with environment variables or roads?

+8
javascript npm
source share
2 answers

It is possible that somelib does not have a main file defined in their package.json , or that it is referenced incorrectly. If somelib does not have main but has directories.lib , then you can do require('somelib/thefile.js') .

If somelib is written in coffeescript and your application is not, you first need require('coffee-script') .

Update: since js2coffee is coffeescript, I'm going to do the latter with you.

+4
source share

Having a specific module name instead of "somelib" may help ... but check the package.json package file. Display require.paths and compare. Read in a node modular system

+1
source share

All Articles