Skip to content
Advertisement

Invalid ELF header out of the gate running simple nodejs script

I’m trying to run a simple nodejs script on Ubuntu 14.04, the contents of which are as follows:

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');

I’ve installed NodeJs via the instructions found here. Here is the excerpt I followed from that link:

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

I did that, verified that both packages are installed:

enter image description here

But I can’t use my .node file without it complaining with this error:

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

I’ve uninstalled and re-installed node as well as npm but it still doesn’t work… I’m running Ubuntu 14.04 LTS 64 bit.

Further information:

I decided to build from source and after cloning master from https://github.com/nodejs/node.git and building it then running the same script, this is the error I get:

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

It’s the same error, obviously, with a different stack trace.

What gives?

Advertisement

Answer

Welp, this is embarrassing(and actually, slightly infuriating).

Invalid ELF header

is just a fancy way of NodeJS telling me that the file’s extension was incorrect.

I ran

mv test.node test.js

to rename the file and now it works. That was a pretty annoying waste of two days because of a bad error message.

Further explanation from robertklep in comments:

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.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement