socket.io: Cannot Resolve 'uws', Update README or "package.json"?

Note: for support questions, please use one of these channels: stackoverflow or slack

You want to:

  • report a bug
  • request a feature

Current behaviour

I downloaded the example project webpack-build-server from socket.io’s master branch, run npm install to install the dependencies, and finally run npm run build to try to bundle the server, but webpack gives me the following errors:

ERROR in ./~/engine.io/lib/server.js
Module not found: Error: Can't resolve 'uws' in '/Users/ZehuaChen/Downloads/socket.io-master/examples/webpack-build-server/node_modules/engine.io/lib'
 @ ./~/engine.io/lib/server.js 107:27-41
 @ ./~/engine.io/lib/engine.io.js
 @ ./~/socket.io/lib/index.js
 @ ./lib/index.js

Steps to reproduce (if the current behaviour is a bug)

  1. Download socket.io’s master branch
  2. Go to examples/webpack-build-server;
  3. Run npm install;
  4. Run npm run build;

Note: the best way to get a quick answer is to provide a failing test case, by forking the following fiddle for example.

Expected behaviour

A server.js file should be produced in dist folder.

Setup

  • OS: macOS Hight SIerra
  • browser: Safari
  • socket.io version: 2.0.3

Other information (e.g. stacktraces, related issues, suggestions how to fix)

These are what’s printed in my terminal

> webpack-build-server@1.0.0 build /Users/ZehuaChen/Downloads/socket.io-master/examples/webpack-build-server
> webpack --config ./support/webpack.config.js

Hash: 997a1fd1ee0b1b064485
Version: webpack 2.7.0
Time: 468ms
    Asset    Size  Chunks                    Chunk Names
server.js  401 kB       0  [emitted]  [big]  main
   [0] ./~/debug/src/index.js 263 bytes {0} [built]
   [7] ./~/socket.io-parser/index.js 8.04 kB {0} [built]
  [16] ./~/engine.io/lib/socket.js 11.6 kB {0} [built]
  [17] ./~/engine.io/lib/transports/index.js 509 bytes {0} [built]
  [19] ./~/socket.io-parser/is-buffer.js 712 bytes {0} [built]
  [20] ./~/socket.io/lib/namespace.js 5.81 kB {0} [built]
  [31] ./~/socket.io/lib/index.js 12.9 kB {0} [built]
  [32] ./lib/index.js 474 bytes {0} [built]
  [43] ./~/engine.io/lib/engine.io.js 2.38 kB {0} [built]
  [44] ./~/engine.io/lib/server.js 15.1 kB {0} [built]
  [58] ./~/socket.io-adapter/index.js 5.44 kB {0} [built]
  [59] ./~/socket.io-client/package.json 3.19 kB {0} [built]
  [61] ./~/socket.io/lib 160 bytes {0} [built]
  [62] ./~/socket.io/lib/client.js 5.9 kB {0} [built]
  [63] ./~/socket.io/lib/parent-namespace.js 917 bytes {0} [built]
    + 59 hidden modules

WARNING in ./~/ws/lib/Validation.js
Module not found: Error: Can't resolve 'utf-8-validate' in '/Users/ZehuaChen/Downloads/socket.io-master/examples/webpack-build-server/node_modules/ws/lib'
 @ ./~/ws/lib/Validation.js 10:22-47
 @ ./~/ws/lib/Receiver.js
 @ ./~/ws/index.js
 @ ./~/engine.io/lib/server.js
 @ ./~/engine.io/lib/engine.io.js
 @ ./~/socket.io/lib/index.js
 @ ./lib/index.js

WARNING in ./~/ws/lib/BufferUtil.js
Module not found: Error: Can't resolve 'bufferutil' in '/Users/ZehuaChen/Downloads/socket.io-master/examples/webpack-build-server/node_modules/ws/lib'
 @ ./~/ws/lib/BufferUtil.js 35:21-42
 @ ./~/ws/lib/Receiver.js
 @ ./~/ws/index.js
 @ ./~/engine.io/lib/server.js
 @ ./~/engine.io/lib/engine.io.js
 @ ./~/socket.io/lib/index.js
 @ ./lib/index.js

WARNING in ./~/socket.io/lib/index.js
113:11-32 Critical dependency: the request of a dependency is an expression

ERROR in ./~/engine.io/lib/server.js
Module not found: Error: Can't resolve 'uws' in '/Users/ZehuaChen/Downloads/socket.io-master/examples/webpack-build-server/node_modules/engine.io/lib'
 @ ./~/engine.io/lib/server.js 107:27-41
 @ ./~/engine.io/lib/engine.io.js
 @ ./~/socket.io/lib/index.js
 @ ./lib/index.js

npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! webpack-build-server@1.0.0 build: `webpack --config ./support/webpack.config.js`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the webpack-build-server@1.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/ZehuaChen/.npm/_logs/2018-05-17T21_30_47_624Z-debug.log

Update

I install uws package manually and it seems to work. Can someone update the README file or package.json since it’s really hard for a beginner like me to figure this out.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 11
  • Comments: 20

Most upvoted comments

It doesn’t stop you from npm start, just ignore the error.

If you are compiling for node only, you should add the option then the error will disappear. You should add to your configuration file:

{
        ...
        target: 'node'
       ...
}

Otherwise, you are compiling for web only, then no option needs to be passed (default=‘web’) (or target=‘web’) but you should exclude ‘uws’ from being bundled, since it doesn’t work in the browser. In this case you should rather add:

{
        ...
        externals: {
            uws: "uws"
        },
       ...
}

So I just had this same issue, but for a very different reason. Currently when I install the latest socket.io server package from npm uws will not be found.

This is happening because the publisher of uws has recently uploaded an empty package deleting all of the actual code D:

You can fix this by installing the last non destroyed release

yarn add uws@10.148.1 npm install uws@10.148.1

Link to empty / deprecated package: https://www.npmjs.com/package/uws

Insightful background info from reddit: https://www.reddit.com/r/node/comments/91kgte/uws_has_been_deprecated/

I’m going to post this in the top few results that come up when searching for this issue as it took me a while to figure out what was up and hopefully this will save someone out there some time!

Webpack / front-end folks, if you just need SocketIO in your client-side app to connect to some WebSocket server, you can just use this: https://github.com/socketio/socket.io-client

Solved my Module not found: Error: Can't resolve 'uws' issue!

@btxtiger it’s because you must not bundle your dependencies when working with node (will be installed with package.json). It’s the default behaviour for webpack, because it bundles your project for the web.

Add this to your conf, it will exclude your dependencies by reading your package.json

const nodeExternals = require('webpack-node-externals');

module.exports = {
...
mode: 'development',
...
externals: [nodeExternals({
               modulesFromFile: true
          })];
...
}

This way you will get (almost) a publishable package

@btxtiger You can do that locally. Just do not publish it on NPM as it breaks the dependency rules. uws is compiled on the fly when using npm i because it’s a C++ module wrapped in Node.js. So it cannot be bundled like standard js and must rather be installed (which triggers compiliaton for your OS). That’s why you should always use the NPM dependency system, then you don’t have to ask yourself that question.

If you still want to do this, try to exclude uws:

{
        ...
        externals: {
            uws: "uws"
        },
       ...
}

I did a lot of research now and still did not understand the issue and how to solve it. I’m having a single project, using socket.io and building with webpack always fails with the same issue.

ERROR in ./node_modules/engine.io/lib/server.js
Module not found: Error: Can't resolve 'uws' in 'xxxxxxxx/node_modules/engine.io/lib'
 @ ./node_modules/engine.io/lib/server.js 107:27-41
 @ ./node_modules/engine.io/lib/engine.io.js
 @ ./node_modules/socket.io/lib/index.js

package.json

"dependencies": {
      "express": "^4.16.3",
      "socket.io": "^2.1.1"
   },
   "devDependencies": {
      "tslint": "^5.11.0",
      "typescript": "2.9.2",
      "uglifyjs-webpack-plugin": "^1.2.7",
      "webpack": "^4.16.4",
      "webpack-cli": "^3.1.0"
   },

webpack.config.js

const path = require('path');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

module.exports = {
   target: 'node',
   mode: 'development',
   entry: './dist/index.js',
   output: {
      filename: 'index.js',
      path: path.resolve(__dirname, 'dist/00-bundle')
   },
   optimization: {
      minimizer: [
         new UglifyJsPlugin()
      ]
   }
};

@ianpaschal Ok, then maybe you are importing a project into another ?

Project_1

  • Index.js: Import Socket.IO

Project_2

  • Index.js: Import Project_1

Webpack: Compile Project_2 -> Module not found in Project_2

And this may happen because the dependency is looked up in Project_2 but Project_1 is holding it. A solution to this is either to install the missing dependency like you did. Or publish your Project_1 then import it as a dependency into Project_2.

By doing this I am able to reproduce the errors you provided above