v icon indicating copy to clipboard operation
v copied to clipboard

"Hello world" does not work in browser with the "js_browser" backend

Open justinas opened this issue 1 month ago • 0 comments

Describe the bug

After compiling a simple v file with the js_browser backend, and loading this script in an HTML page, an error is seen in the browser's console:

Uncaught ReferenceError: require is not defined

Reproduction Steps

$ cat hello.v
fn main() {
    println("hello world")
}
$ v -backend js_browser -o hello.js hello.v
$ cat index.html
<script src="./hello.js"></script>
$ firefox index.html

Expected Behavior

Browser's console to display "hello world"

Current Behavior

Browser's console shows an error:

Uncaught ReferenceError: require is not defined
    <anonymous> file:///home/justinas/vlang-testcases/hello.js:7

Possible Solution

All 4 JS backend "flavors" produce the same JS code:

$ v -backend js -o hello.js hello.v && sha256sum hello.js
91247919bfaa2cd4eca44de0bffa7c9dabb6449412de97e3c996a91f36c6387e  hello.js
$ v -backend js_browser -o hello.js hello.v && sha256sum hello.js
91247919bfaa2cd4eca44de0bffa7c9dabb6449412de97e3c996a91f36c6387e  hello.js
$ v -backend js_node -o hello.js hello.v && sha256sum hello.js
91247919bfaa2cd4eca44de0bffa7c9dabb6449412de97e3c996a91f36c6387e  hello.js
$ v -backend js_freestanding -o hello.js hello.v && sha256sum hello.js
91247919bfaa2cd4eca44de0bffa7c9dabb6449412de97e3c996a91f36c6387e  hello.js

The resulting code relies on NodeJS-esque require() as well as Node modules such as os. In my eyes, js_browser backend should produce code that is able to run in any modern browser.

Additional Information/Context

No response

V version

V 0.4.6 4a7c70c

Environment details (OS name and version, etc.)

$ v doctor
V full version: V 0.4.6 4a7c70c
OS: linux, Linux version 6.6.33 (nixbld@localhost) (gcc (GCC) 13.2.0, GNU ld (GNU Binutils) 2.41) #1-NixOS SMP PREEMPT_DYNAMIC Wed Jun 12 09:13:03 UTC 2024 (chroot)
Processor: 16 cpus, 64bit, little endian, AMD Ryzen 7 PRO 4750U with Radeon Graphics

getwd: /home/justinas/vlang-testcases
vexe: /home/justinas/vlang-box/compiler/v
vexe mtime: 2024-06-28 20:18:31

vroot: OK, value: /home/justinas/vlang-box/compiler
VMODULES: OK, value: /home/justinas/.vmodules
VTMP: OK, value: /tmp/v_1000

Git version: git version 2.43.0
Git vroot status: weekly.2024.26-14-g4a7c70c9
.git/config present: true

CC version: gcc (GCC) 13.2.0
thirdparty/tcc status: thirdparty-linux-amd64 40e5cbb5

[!NOTE] You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote. Other reactions and those to comments will not be taken into account.

justinas avatar Jun 28 '24 21:06 justinas