Linux node.js 找不到模块 xml2js

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5047542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:54:56  来源:igfitidea点击:

node.js cannot find module xml2js

linuxnode.jsinclude

提问by taran gill

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.

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

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.)

我已经确保 xml2js 和 express 所在的路径 (/usr/local/lib/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)

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.

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

回答by DaveJ

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

尝试将其安装在项目中而不是全局包目录中。

If you're using a package.jsonto manage dependancies then you can just run npm bundlein 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).

如果您使用 apackage.json来管理依赖项,那么您可以npm bundle在项目目录中运行,然后require.paths.unshift('./node_modules')在应用程序文件的顶部添加。在我看来,这是所有项目的最佳实践(尤其是考虑到节点开发的速度)。

回答by spmason

回答by stigi

Node 0.4 looks in ./node_modulesfor 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

节点 0.4 查找./node_modules模块。对我来说,将模块目录链接到我的项目目录确实有帮助ln -s /usr/local/lib/node node_modules

回答by zcopley

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

我想简单的答案是 xml2js 和 xml2js-xpat 的当前包被破坏了。

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

我最终使用 node-xml 代替。我希望 xml2js 不是我尝试安装的第一个 npm 模块。

回答by goncalossilva

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_modulesto the require before requiring any module:

正如 spmason 所提到的,Node 改变了模块的解析方式。我遇到了与您相同的问题,并通过全局安装所有模块 ( --global) 并/usr/local/lib/node_modules在需要任何模块之前添加到 require 来解决它:

require.paths.push('/usr/local/lib/node_modules');
require('blah'); // it works!

回答by eshi

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

只是 make ln -s /usr/local/lib/node /usr/local/lib/node_modules,但在将 的内容node_modules移入原始节点库之前node- 它帮助了我:)

回答by Hanxue

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

不再对节点 v0.8.1 及更高版本有效。而不是使用的require.paths.push,你可以设置环境变量NODE_PATH

export NODE_PATH=/usr/local/lib/node_modules

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

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

export NODE_PATH=~/.npm

回答by Chad Campbell

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

个人发现XML2JS模块需要在本地通过npm安装。虽然我只在 Windows 上尝试过这个,但我在这里写了一篇博文