node.jsnpmwebsocketuwebsockets

Compilation of µWebSockets has failed and there is no pre-compiled binary


I am trying to run a local websocket server using nodejs and uws on a linux machine. the npm install uws runs without any error and I can see a uws named folder inside my node_modules. But when I try to create websocket server using following code. It gives me error

/home/suresh/browserstack/node_modules/uws/uws.js:40
            throw new Error('Compilation of µWebSockets has failed and there is no pre-compiled binary ' +
            ^

Error: Compilation of µWebSockets has failed and there is no pre-compiled binary available for your system. Please install a supported C++11 compiler and reinstall the module 'uws'.
    at e (/home/suresh/browserstack/node_modules/uws/uws.js:40:19)
    at Object.<anonymous> (/home/suresh/browserstack/node_modules/uws/uws.js:44:3)
    at Module._compile (module.js:577:32)
    at Object.Module._extensions..js (module.js:586:10)
    at Module.load (module.js:494:32)
    at tryModuleLoad (module.js:453:12)
    at Function.Module._load (module.js:445:3)
    at Module.require (module.js:504:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/home/suresh/browserstack/ws/index.js:1:95)

Here is my js code:

const WebSocket = require('uws').Server;
var http = require('http');

var server = http.createServer(function(request, response) {});

var wss = new WebSocket({
  server: server,
  path: '/wss'
});

var connCounter = 1;
wss.on('connection', function(ws){
  console.log('new connection');

  ws.id = connCounter;
  ws.on('message', function(message){

  });

  ws.on('close', clearWS);
  ws.on('end', clearWS);
  ws.on('disconnect', clearWS);
  ws.on('error', clearWS);

  function clearWS(e){
    console.log('in clearWS', e);
  }
})

function sendBuffer(ws, buffer){
  if (ws && ws.readyState == 1)
    ws.send(buffer);
}

I looked at /home/suresh/browserstack/node_modules/uws/uws.js:40 and found that a native function is failing due to missing uws_linux_48 file. Below is the added printed error logs statements:

Error: Cannot find module './uws_linux_48'
    at Function.Module._resolveFilename (module.js:476:15)
    at Function.Module._load (module.js:424:25)
    at Module.require (module.js:504:17)
    at require (internal/module.js:20:19)
    at e (/home/suresh/browserstack/node_modules/uws/uws.js:29:20)
    at Object.<anonymous> (/home/suresh/browserstack/node_modules/uws/uws.js:45:3)
    at Module._compile (module.js:577:32)
    at Object.Module._extensions..js (module.js:586:10)
    at Module.load (module.js:494:32)
    at tryModuleLoad (module.js:453:12) code: 'MODULE_NOT_FOUND' }

I searched on the internet and found some github issues and comments but they didn't help.

Here is my system information:

gcc - version 5.5.0
node - v6.14.3
npm - 3.10.10

Tons of thanks in advance


Solution

  • Installing Latest nodejs and npm on Ubuntu with PPA, solved this problem.

    Earlier version

    node - v6.14.3
    npm - 3.10.10
    

    Latest version which solved the problem-

    node - v10.6.0
    npm - 6.1.0
    

    Refer - How to Install Latest Node.js and NPM on Ubuntu with PPA