Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js cannot find module xml2js

Tags:

I made an application on my machine, and it works well. I uploaded it to the server, and it is crashing with the following error:

node.js:116
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
Error: Cannot find module 'xml2js'
    at Function._resolveFilename (module.js:289:11)
    at Function._load (module.js:241:25)
    at require (module.js:317:19)
    at Object.<anonymous> (/var/www/node/price/index.js:3:14)
    at Module._compile (module.js:373:26)
    at Object..js (module.js:379:10)
    at Module.load (module.js:305:31)
    at Function._load (module.js:271:10)
    at Array.<anonymous> (module.js:392:10)
    at EventEmitter._tickCallback (node.js:108:26)

This is how my app starts:

var express=require('express');
var http=require('http');
var xml2js = require('xml2js');
var sys = require('sys');
var util = require('util');

I have installed both express and xml2js using npm. I have the exact same version (v0.4.0) for node on my machine and my server.

I have made sure that the path wher xml2js and express reside (/usr/local/lib/node/) is included in the paths where node looks for modules. (I edited the file 'module.js' to print the paths where it is looking for modules.)

node.js:116
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
Error: Cannot find module 'xml2js', in paths: /root/.node_modules,/root/.node_libraries,/usr/local/lib/node,/var/www/node/price/node_modules,/var/www/node/node_modules,/var/www/node_modules,/var/node_modules,/node_modules
    at Function._resolveFilename (module.js:289:11)
    at Function._load (module.js:241:25)
    at require (module.js:317:19)
    at Object.<anonymous> (/var/www/node/price/index.js:3:14)
    at Module._compile (module.js:373:26)
    at Object..js (module.js:379:10)
    at Module.load (module.js:305:31)
    at Function._load (module.js:271:10)
    at Array.<anonymous> (module.js:392:10)
    at EventEmitter._tickCallback (node.js:108:26)

So what is wrong? I have the right path, the module is there. Why can't node find it? And the exact same code runs smoothly on my local machine. If it matters, my machine is a Mac, and the server runs CentOS.

like image 968
taran gill Avatar asked Feb 18 '11 23:02

taran gill


2 Answers

require.paths.push('/usr/local/lib/node_modules');

is no longer valid for node v0.8.1 and above. Instead of using require.paths.push, you can set the environment variable NODE_PATH

export NODE_PATH=/usr/local/lib/node_modules

or if you install npm modules in your home directory, then

export NODE_PATH=~/.npm
like image 147
Hanxue Avatar answered Dec 21 '22 22:12

Hanxue


As spmason mentioned, Node changed how modules are resolved. I've had the same issue as you and resolved it by installing all modules globally (--global) and adding /usr/local/lib/node_modules to the require before requiring any module:

require.paths.push('/usr/local/lib/node_modules');
require('blah'); // it works!
like image 37
goncalossilva Avatar answered Dec 21 '22 23:12

goncalossilva