onebeyond / rascal

A config driven wrapper for amqp.node supporting multi-host connections, automatic error recovery, redelivery flood protection, transparent encryption / decryption and channel pooling.
MIT License
451 stars 69 forks source link

Webpack Module not found: Error: Can't resolve 'cluster' #162

Closed timolegros closed 3 years ago

timolegros commented 3 years ago

Hi!

I am working on a project that has uses Rascal and that project is a dependency of another project that is bundled with webpack. If I import Rascal anywhere in my project and then attempt to bundle with webpack it causes the following error:

Module not found: Error: Can't resolve 'cluster' in '/home/timolegros/Projects/rascal_test/node_modules/rascal/lib/counters'
resolve 'cluster' in '/home/timolegros/Projects/rascal_test/node_modules/rascal/lib/counters'
  Parsed request is a module
  using description file: /home/timolegros/Projects/rascal_test/node_modules/rascal/package.json (relative path: ./lib/counters)
    Field 'browser' doesn't contain a valid alias configuration
    resolve as module
      /home/timolegros/Projects/rascal_test/node_modules/rascal/lib/counters/node_modules doesn't exist or is not a directory
      /home/timolegros/Projects/rascal_test/node_modules/rascal/lib/node_modules doesn't exist or is not a directory
      looking for modules in /home/timolegros/Projects/rascal_test/node_modules/rascal/node_modules
        single file module
          using description file: /home/timolegros/Projects/rascal_test/node_modules/rascal/package.json (relative path: ./node_modules/cluster)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              /home/timolegros/Projects/rascal_test/node_modules/rascal/node_modules/cluster doesn't exist
            .js
              Field 'browser' doesn't contain a valid alias configuration
              /home/timolegros/Projects/rascal_test/node_modules/rascal/node_modules/cluster.js doesn't exist
            .json
              Field 'browser' doesn't contain a valid alias configuration
              /home/timolegros/Projects/rascal_test/node_modules/rascal/node_modules/cluster.json doesn't exist
            .wasm
              Field 'browser' doesn't contain a valid alias configuration
              /home/timolegros/Projects/rascal_test/node_modules/rascal/node_modules/cluster.wasm doesn't exist
        /home/timolegros/Projects/rascal_test/node_modules/rascal/node_modules/cluster doesn't exist
      /home/timolegros/Projects/rascal_test/node_modules/node_modules doesn't exist or is not a directory
      looking for modules in /home/timolegros/Projects/rascal_test/node_modules
        single file module
          using description file: /home/timolegros/Projects/rascal_test/package.json (relative path: ./node_modules/cluster)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              /home/timolegros/Projects/rascal_test/node_modules/cluster doesn't exist
            .js
              Field 'browser' doesn't contain a valid alias configuration
              /home/timolegros/Projects/rascal_test/node_modules/cluster.js doesn't exist
            .json
              Field 'browser' doesn't contain a valid alias configuration
              /home/timolegros/Projects/rascal_test/node_modules/cluster.json doesn't exist
            .wasm
              Field 'browser' doesn't contain a valid alias configuration
              /home/timolegros/Projects/rascal_test/node_modules/cluster.wasm doesn't exist
        /home/timolegros/Projects/rascal_test/node_modules/cluster doesn't exist
      /home/timolegros/Projects/node_modules doesn't exist or is not a directory
      /home/timolegros/node_modules doesn't exist or is not a directory
      /home/node_modules doesn't exist or is not a directory
      /node_modules doesn't exist or is not a directory
 @ ./node_modules/rascal/lib/amqp/Broker.js 14:24-69
 @ ./node_modules/rascal/index.js 4:15-43
 @ ./index.js 1:15-32

I have looked at some of the other issues for Rascal and it does seem that bundling with webpack should work. Is there a specific webpack configuration that is needed?

In order to replicate this error you can set up a project as follows:

package.json

{
  "name": "rascal_test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack --config webpack.config.js --progress"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "node-polyfill-webpack-plugin": "^1.1.4",
    "rascal": "^13.1.0",
    "webpack": "^5.45.1",
    "webpack-cli": "^4.7.2",
    "webpack-plugin": "^1.0.5"
  },
  "devDependencies": {
    "@types/rascal": "^10.0.3",
  }
}

webpack.config.js

const path = require('path');
const NodePolyfillPlugin = require("node-polyfill-webpack-plugin")

module.exports = {
  entry: "./index.js",
  output: {
    path: path.join(__dirname, '../build'),
    filename: 'js/[name].[hash:8].js',
  },
  plugins: [
    new NodePolyfillPlugin()
  ],
  mode: "production",
  resolve: {
    modules: ['node_modules'],
    fallback: {
      "cluster:": require.resolve("cluster"),
      "net": require.resolve("net"),
      "tls": require.resolve("tls")
    }
  }
};

index.js

const Rascal = require('rascal')

These should all be files in the same top-level directory so: + rascal-test-project +---+ package.json +---+ webpack.config.js +---+ index.js

I tried to add cluster as a fallback to avail. The error occurs on node versions 14.17.0 and 16.2.0 (haven't tested others) as well as on various webpack versions (4-5).

Thanks in advance for any help/suggestions!

Edit:

Apparently, this error is not specific to Rascal and is just an issue between cluster (a Rascal dependency/native node module) and webpack. I will leave this issue up for now and close it once I have a solution in case anyone else runs into the same error when using Rascal.

cressie176 commented 3 years ago

Hi @timolegros,

Thanks for your issues and subsequent edit. As you say it's likely to be an issue with webpack and cluster - which is part of the node core for forking node processes. Rascal uses it as an optional way to count message delivery attempts in a memory space that will survive if your application crashes.

Happy to keep this ticket open for now to help others, but I try to keep a low number of open issues, so will close after a couple of weeks if things go quiet.

timolegros commented 3 years ago

There doesn't seem to be a direct fix for this (please let me know if otherwise) so I just moved the module in my project that used Rascal out and into its own package.