2016-10-28 46 views
1

我试图在Ubuntu 14.04运行一个简单的脚本的NodeJS的大门,其内容如下:无效的ELF头了运行简单的脚本的NodeJS

http = require('http'); 

http.createServer(function(req, res){ 
    res.writeHead(200, {'Content-Type': 'text/plain' }); 
    res.end('Hello World \n'); 
}).listen(80, '127.0.0.1'); 

我已经通过了安装的NodeJS说明found here。下面是我从该链接遵循的摘录:

Option 2: Install Node.js with Ubuntu Package Manager

To install Node.js, type the following command in your terminal:

sudo apt-get install nodejs

Then install the Node package manager, npm:

sudo apt-get install npm

Create a symbolic link for node, as many Node.js tools use this name to execute.

sudo ln -s /usr/bin/nodejs /usr/bin/node

我这样做,证明这两个软件包安装:

enter image description here

但我不能用我的.node档案,而不要抱怨与此错误:

[email protected]:/var/www/html/nodetest$ node test.node 

module.js:356 
    Module._extensions[extension](this, filename); 
          ^
Error: /var/www/html/nodetest/test.node: invalid ELF header 
    at Module.load (module.js:356:32) 
    at Function.Module._load (module.js:312:12) 
    at Function.Module.runMain (module.js:497:10) 
    at startup (node.js:119:16) 
    at node.js:945:3 

我已经卸载和重新安装节点以及NPM,但它仍然无法正常工作...我运行Ubuntu 14.04 LTS 64位。

更多信息:

我决定从源代码和https://github.com/nodejs/node.git克隆master,建设它,然后运行相同的脚本之后打造,这是我得到的错误:

[email protected]:/var/www/html/nodetest$ ~/node/node test.node 
module.js:600 
    return process.dlopen(module, path._makeLong(filename)); 
       ^

Error: /var/www/html/nodetest/test.node: invalid ELF header 
    at Object.Module._extensions..node (module.js:600:18) 
    at Module.load (module.js:490:32) 
    at tryModuleLoad (module.js:449:12) 
    at Function.Module._load (module.js:441:3) 
    at Module.runMain (module.js:607:10) 
    at run (bootstrap_node.js:414:7) 
    at startup (bootstrap_node.js:139:9) 
    at bootstrap_node.js:529:3 

这是相同的错误,显然,与不同的堆栈跟踪。

什么给?

+0

控制台输出引述看起来比图片要好得多,因为仅仅反差太大介绍(和你没有虚拟机下运行它W/O副本 - 我可以看到可用的膏状物)。 – agg3l

+0

只是一个疯狂的猜测 - 不能是关于你的脚本错误的文件编码node.js无法解释?像UTF8 + BOM或UTF16 ... – agg3l

+0

我只是在我的Pi上运行相同的步骤运行Raspbian Jessie,同样的事情发生。我还没有检查文件的编码,但是nodejs需要什么编码? – Brandon

回答

2

韦尔普,这是令人尴尬的(实际上,有点令人懊恼)。

Invalid ELF header

只是NodeJS的一种奇特的方式,告诉我文件的扩展名不正确。

我跑

mv test.node test.js

重命名的文件,现在它的工作原理。这是一个很糟糕的错误消息,因此浪费了两天时间。从robertklep在评论

进一步解释:

The file extension.node has special meaning for Node, as it's supposed to be used for native (compiled) addons. The "ELF header" part refers to how the dynamic loader (process.dlopen() in the stack trace of the error) identifies that a file is a compiled addon.Since you were feeding it a JS file, the identification failed, so "invalid ELF header". That doesn't say anything about the JS file, which was working just fine after you renamed it.

+0

文件扩展名'.node'对Node有特殊意义,因为它应该用于本机(编译)插件。 – robertklep

+0

@robertklep太棒了。这是有道理的。所以如果我的理解正确,它告诉我,如果我想运行一个'.node',那么我的ELF头部不正确。并非文件本身格式不正确 – Brandon

+0

“ELF标题”部分是指动态加载程序('process。dlopen()在错误的堆栈跟踪中)标识一个文件是一个编译的插件。由于您正在给它提供一个JS文件,所以标识失败,因此_“无效的ELF标头”_。这并没有提到任何有关JS文件的内容,在重新命名后,它很好地工作:D – robertklep