JWally/jsLPSolver

Version 0.4.20 doesn't work in the browser

Closed this issue · 6 comments

I just upgraded from 0.4.5 to 0.4.20 and I get these errors using webpack:

./node_modules/javascript-lp-solver/src/External/lpsolve/main.js
Module not found: Error: Can't resolve 'child_process' in 'C:\Users\Daan\code\phantasmal-world\node_modules\javascript-lp-solver\src\External\lpsolve'
./node_modules/javascript-lp-solver/src/External/lpsolve/main.js
Module not found: Error: Can't resolve 'fs' in 'C:\Users\Daan\code\phantasmal-world\node_modules\javascript-lp-solver\src\External\lpsolve'

Is browser support no longer a feature?

Edit: 0.4.17 does work in the browser.

We're using it in this project: https://github.com/DaanVandenBosch/phantasmal-world (first line of this file: https://github.com/DaanVandenBosch/phantasmal-world/blob/master/src/hunt_optimizer/stores/HuntOptimizerStore.ts).

We use the package from npmjs (through yarn). And we use webpack to load it as a module. It's actually webpack that throws the error (before we can even load the module) because it can't find "child-process" and "fs", which are node built-in modules. The errors seem to stem from this file: https://github.com/JWally/jsLPSolver/blob/master/src/External/lpsolve/main.js

And 0.4.17 is the most recent version available on npmjs that does work and it's the version we're using at the moment.

I built out a better "test-suite" to make sure the library works in the browser.

1.) node server.js
2.) Aim a browser @ http://localhost:9000/test/test.html

However, I'm not sure if this helps you since it looks like webpack is what throws a fit when trying to compile everything...

Just curious, can you change what webpack compiles?

Right now, it looks like you're using /src/main.js.
What happens if you use /prod/solver.js?

What about this:

In your webpack config file, add the following:

const webpack = require('webpack'); //to access built-in plugins

module.exports = {
        "mode": "development",
        "plugins": [
            new webpack.IgnorePlugin(/(fs|child_process)/),
        ]
}

On my side, I've moved the 'fs' requirement inside the function that calls it so its no longer globally exposed and shouldn't cause issues in the browser.

@DaanVandenBosch , does this solve your issue?

I've actually applied a patch to mitigate this:

--- "a/node_modules/javascript-lp-solver/src/main.js"
+++ "b/node_modules/javascript-lp-solver/src/main.js"
@@ -25,7 +25,7 @@ var Constraint = expressions.Constraint;
 var Variable = expressions.Variable;
 var Numeral = expressions.Numeral;
 var Term = expressions.Term;
-var External = require("./External/main.js");
+var External = {};
 
 // Place everything under the Solver Name Space
 var Solver = function () {