开发者

node.js cannot find module xml2js

开发者 https://www.devze.com 2023-02-12 13:34 出处:网络
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:

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 'xm开发者_JS百科l2js'
    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.


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


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!


Node 0.4 changed how modules are resolved and it appears that this breaks xml2js.


Node 0.4 looks in ./node_modules for modules. For me it did help to just link the modules directory to my project directory with ln -s /usr/local/lib/node node_modules


Personally, I found that the XML2JS module needs to be installed via npm locally. While I've only tried this on Windows, I've written a blog post here


Try installing it in the project instead of the global package directory.

If you're using a package.json to manage dependancies then you can just run npm bundle in the project directory and then add require.paths.unshift('./node_modules') at the top of your app file. In my opinion this is the best practice for all projects (especially considering the speed that the development of node takes place).


I guess the simple answer is the current packages for xml2js and xml2js-xpat are busted.

I ended up using node-xml instead. I wish xml2js hadn't been my the first npm module I tried to install.


just make ln -s /usr/local/lib/node /usr/local/lib/node_modules, but before move the content of node_modules into the original node library node - it helped me :)


Try to install the npm package with the -g flag to make it global

npm install -g package_name
0

精彩评论

暂无评论...
验证码 换一张
取 消