+-
node.js-运行简单的nodejs脚本的无效ELF标头
我正在尝试在Ubuntu 14.04上运行一个简单的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');

我已经通过found here指令安装了NodeJ.这是我从该链接中摘录的内容:

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文件:

user@host:/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

我已经卸载并重新安装了node以及npm,但是它仍然无法正常工作.我正在运行Ubuntu 14.04 LTS 64位.

更多信息:

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

user@host:/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

显然,这是相同的错误,但堆栈跟踪不同.

是什么赋予了?

最佳答案
抱歉,这很尴尬(实际上有点令人发指).

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.

点击查看更多相关文章

转载注明原文:node.js-运行简单的nodejs脚本的无效ELF标头 - 乐贴网