Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot Resolve 'uws', Update README or "package.json"? #3249

Closed
1 of 2 tasks
Zehua-Chen opened this issue May 17, 2018 · 20 comments
Closed
1 of 2 tasks

Cannot Resolve 'uws', Update README or "package.json"? #3249

Zehua-Chen opened this issue May 17, 2018 · 20 comments

Comments

@Zehua-Chen
Copy link

Zehua-Chen commented May 17, 2018

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.

@Zehua-Chen Zehua-Chen changed the title Cannot Resolve 'uws' Cannot Resolve 'uws', Update README? May 18, 2018
@Zehua-Chen Zehua-Chen changed the title Cannot Resolve 'uws', Update README? Cannot Resolve 'uws', Update README or "package.json"? May 18, 2018
@ianpaschal
Copy link

ianpaschal commented May 18, 2018

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

@hackhat
Copy link

hackhat commented Jun 2, 2018

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

@ianpaschal
Copy link

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

@despagito
Copy link

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

@millsp
Copy link

millsp commented Jun 26, 2018

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

@ianpaschal
Copy link

@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/ )
	],
};

@millsp
Copy link

millsp commented Jul 21, 2018

@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

@Zehua-Chen
Copy link
Author

Zehua-Chen commented Jul 21, 2018

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

@millsp
Copy link

millsp commented Jul 21, 2018

@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.

@btxtiger
Copy link

btxtiger commented Aug 3, 2018

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

@millsp
Copy link

millsp commented Aug 4, 2018

@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
Copy link

btxtiger commented Aug 4, 2018

@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.

@millsp
Copy link

millsp commented Aug 5, 2018

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

@btxtiger
Copy link

btxtiger commented Aug 5, 2018

@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.

@michaelpwilson
Copy link

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.

@walkerreynolds16
Copy link

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.

@inciteio
Copy link

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

@alecjacobs
Copy link

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!

@codymikol
Copy link

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!

@FunctionDJ
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests