linux - node.js 找不到模块 xml2js

标签 linux node.js include

我在我的机器上做了一个应用程序,它运行良好。我将它上传到服务器,它崩溃并出现以下错误:

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)

这是我的应用程序的启动方式:

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

我已经使用 npm 安装了 express 和 xml2js。我的机器和服务器上的 Node 具有完全相同的版本 (v0.4.0)。

我已确保 xml2js 和 express 所在的路径 (/usr/local/lib/node/) 包含在 Node 查找模块的路径中。 (我编辑了文件“module.js”以打印它正在寻找模块的路径。)

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)

那么有什么问题吗?我有正确的路径,模块在那里。为什么 Node 找不到?完全相同的代码在我的本地机器上运行顺畅。如果有关系,我的机器是 Mac,服务器运行 CentOS。

最佳答案

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

对于 Node v0.8.1 及更高版本不再有效。您可以设置环境变量 NODE_PATH

,而不是使用 require.paths.push
export NODE_PATH=/usr/local/lib/node_modules

或者如果你在你的主目录中安装 npm 模块,那么

export NODE_PATH=~/.npm

关于linux - node.js 找不到模块 xml2js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5047542/

相关文章:

linux - float 除法 bash

mysql - Mocha 测试与数据库的连接

c - 无法编译包含问题的 C 代码

google-apps-script - 如何将文件包含到您的项目中

java - iReport 在 debian 上运行 java1.8

python - 从/proc/stat 计算 user、nice、sys、idle、iowait、irq 和 sirq

linux - 尝试从终端安装 .run 文件时没有任何反应

javascript - 快速 GET 路线不起作用

javascript - 设置 Sails js View 扩展不起作用

php - 使用带有多个包含的 php 读取和写入数据库