2016-10-28 77 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