简体   繁体   中英

node.js cannot find module xml2js

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.

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 改变了模块的解析方式,这似乎破坏了 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 posthere

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.

尝试使用-g标志安装 npm 包以使其成为全局

npm install -g package_name

只需制作ln -s /usr/local/lib/node /usr/local/lib/node_modules ,但在将node_modules的内容移动到原始节点库node - 它帮助了我:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM