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

Created on 17 May 2018  ·  20Comments  ·  Source: socketio/socket.io

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

You want to:

  • [x] 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

````

[email protected] 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! [email protected] build: webpack --config ./support/webpack.config.js
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the [email protected] 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.

Most helpful comment

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 [email protected]
npm install [email protected]

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!

All 20 comments

I am also getting the same warnings:

WARNING in ./~/ws/lib/BufferUtil.js
Module not found: Error: Can't resolve 'bufferutil' in '/Users/Ian/Code/ianpaschal/forge-server/node_modules/ws/lib'
 @ ./~/ws/lib/BufferUtil.js 35:21-42
 @ ./~/ws/lib/Sender.js
 @ ./~/ws/index.js
 @ ./~/engine.io/lib/server.js
 @ ./~/engine.io/lib/engine.io.js
 @ ./~/socket.io/lib/index.js
 @ ./src/server.js

WARNING in ./~/ws/lib/Validation.js
Module not found: Error: Can't resolve 'utf-8-validate' in '/Users/Ian/Code/ianpaschal/forge-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
 @ ./src/server.js

WARNING in ./~/socket.io/lib/index.js
113:11-32 Critical dependency: the request of a dependency is an expression
// package.json
"webpack": "^2.7.0",
"socket.io": "^2.1.0",

Did you manage to solve it? Have the same issue..

Nope. It's only a warning so there's no effect other than ugly console output, but it's still there.

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"
        },
       ...
}

@pirix-gh This doesn't seem to be the issue.

From my config:

const Webpack = require( "webpack" );

module.exports = {
    target: "node",
    node: {
        __dirname: true,
        __filename: true,
    },
    entry: {
        main: "./src/server.js",
    },
    plugins: [
        new Webpack.EnvironmentPlugin( [
            "NODE_ENV",
        ] ),
        new Webpack.IgnorePlugin( /uws/ )
    ],
};

@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

Thanks for explaining why it went wrong, and providing an alternate approach!

@Zehua-Chen Pleasure, hope it fixed your problem. I know how annoying it can be to publish all the time so... For this case of split projects, you can use during your development:

npx @babel/node src/index.js or
node -r @babel/register src/index.js

or depending on your version

npx babel-node src/index.js or
node -r babel-register src/index.js

It will resolve the dependencies properly & run, but remember it is not suitable for production.
If you are working in production you should publish your packages separately & use npm instead.

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()
      ]
   }
};

@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

@pirix-gh Thanks! Okay, that makes sense. I was hoping being able to create a full bundle that just needs to be pushed to the server without need to install/update modules via package.json. I'm still not sure if this really is not possible, except for that error with socket.io it seems to work.

@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"
        },
       ...
}

@pirix-gh Great, thanks very much for explaination. I moved to an optimized package.json which excludes all dev dependencies while building with webpack, and always runs npm install before starting. Seems to be the best solution for now.

I didn't have time to debug it fully, but when upgrading from karma^2.0.0 to karma^3.0.0 I got this error on my webpack build:

Module not found: Error: Can't resolve 'uws'

So keeping Karma at 2.0.0 was the solution for me.

I'm getting this issue whenever I import or require socket.io. I've tried manually installing uws and Node still can't resolve uws.

I am getting this issue as well, I am not clear on how to solve it.

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!

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 [email protected]
npm install [email protected]

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!

For me, --target node over the CLI or config was the key and solved all my issues.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shashuec picture shashuec  ·  4Comments

Aweather picture Aweather  ·  4Comments

kootoopas picture kootoopas  ·  4Comments

MyMomSaysIAmSpecial picture MyMomSaysIAmSpecial  ·  4Comments

dmuth picture dmuth  ·  3Comments