lovasoa/highs-js

Examples misleading with async requires

lequant40 opened this issue · 6 comments

Hello (again :-)) !

The examples of usage provided are not working by copy/pasting the code.

For instance, when running the first example (with node 12.19 but other versions are similar):

const highs = await require("highs")();

Node.js complains:

> [const highs = await require("highs")();
              ^^^^^

SyntaxError: await is only valid in async function
]

Actually, in order to use this syntax, it is mandatory to wrap the await inside an async function, for instance like:

var highclient = async function() {
  const highs = await require("highs")();
  
  const PROBLEM = `Maximize
   obj: x1 + 2 x2 + 3 x3 + x4
  Subject To
   c1: - x1 + x2 + x3 + 10 x4 <= 20
   c2: x1 - 3 x2 + x3 <= 30
   c3: x2 - 3.5 x4 = 0
  Bounds
   0 <= x1 <= 40
   2 <= x4 <= 3
  End`;
  
  const sol = highs.solve(PROBLEM);
  
  // Do something with sol

};

Would it be possible to better document this ?

Cheers,

Roman

Top level await does work in node. You have to use a "modern" environment, with node v14+ and using es modules.

https://v8.dev/features/top-level-await

I actually tried with v14.16.1 and I still have the same error.

I will try with Node 16.

Make sure your file is an es module

Actually, same issue with v16.0.0.

Please note that I am simply using the node command prompt, no fancy stuff.

Welcome to Node.js v16.0.0.
Type ".help" for more information.
> const highs = await require("highs")();
const highs = await require("highs")();
              ^^^^^

Uncaught:
SyntaxError: await is only valid in async functions and the top level bodies of modules

What I am trying to say here is that a copy/paste of the README example does not work.

So, I would suggest to re-write the README example with a code working with a simple copy/paste.
Otherwise, I think some beginners might skip the usage of this very interesting package !

It does work. As the node error (and me 😛 ) is telling you, you are not using an es module.
You probably named your file .js instead of .mjs.

I saw you updated the README!
The example is now working off the shelf, great!!
( Although the assert is KO on my terminal :-( )

Re the initial error (not about .js or .mjs file, but about the Node.js command line with direct input), Google pointed me in the right direction: at this date, using await in Node.js REPL is not supported by default but this support can be enabled via a specific node command line flag -> https://nodejs.org/api/cli.html#cli_experimental_repl_await

(This flag might be activated by default in your environment, so that you did not understand my issue.)

So, this is working (from Node.js v10 actually):

root@c584006313df:/# node --experimental-repl-await
Welcome to Node.js v14.16.1.
Type ".help" for more information.
> const highs = await require("highs")();
undefined

But this is not working (and was my initial issue):

root@c584006313df:/# node
Welcome to Node.js v14.16.1.
Type ".help" for more information.
> const highs = await require("highs")();
const highs = await require("highs")();
              ^^^^^

Uncaught SyntaxError: await is only valid in async function