this post was submitted on 29 Nov 2023
5 points (85.7% liked)

JavaScript

1700 readers
59 users here now

founded 1 year ago
MODERATORS
 

Title. I'm trying to compile this but I can't seem to do so with node.js.

Thanks in advance.

top 11 comments
sorted by: hot top controversial new old
[–] shnizmuffin@lemmy.inbutts.lol 5 points 9 months ago (1 children)

Short answer: don't. Just serve the content using nginx and point a normal web browser at it.

Long answer: Apache Cordova.

[–] GustavoM@lemmy.world 1 points 9 months ago

Oh boy.

Thank you nonetheless.

[–] Macil@programming.dev 4 points 9 months ago

You can use Electron or Tauri to make an executable out of a web page.

[–] blackstampede@sh.itjust.works 2 points 9 months ago (1 children)

That's not how JavaScript works. You have to run it with node.js or in a browser to get it to do stuff. Are you asking how to bundle JavaScript?

[–] GustavoM@lemmy.world 1 points 9 months ago* (last edited 9 months ago) (2 children)

Frankly, I know little to none regarding js, but I suppose I'm trying to "bundle" everything into a single binary so it can run in CLI, or in an external window if the former is not possible.

[–] JakenVeina@lemm.ee 4 points 9 months ago (1 children)

JavaScript does not compile to binary. It's a scripting language that is run by an interpreter, which may or may not compile it on the fly to run alongside whatever else the interpreter is doing.

The most obvious JS interpreter is a browser, which runs JS alongside an HTML/CSS rendering engine, do draw stuff for you.

Node.js is a JS interpreter that runs JS inside/alongside an HTTP server, mainly in the interest of of web developers wanting to share JS code between both the server and client.

Electron is a JS interpreter that essentially launches a trimmed-down version of Chrome inside a window on your OS desktop, whuch became popular among web developers already familiar with JS wanting to or being forced to expand into desktop app development.

It looks like you're essentially trying to create a screensaver? HTML/CSS/JS is simply not the tool for that.

[–] GustavoM@lemmy.world 2 points 9 months ago (1 children)

Thank you for your explanation. Not "create", but use that specific package in the OP as a command to run it as a "lightweight screensaver" of sorts. And I honestly thought it was possible considering nexe and pkg exists and both can "compile" .js files into a binary. And the possibility of displaying pictures in true color (even in the CLI).

[–] blackstampede@sh.itjust.works 3 points 9 months ago

I think generally when a tool says that it's compiling JavaScript into a binary, it's just packaging it with a lightweight browser basically. Python has kind of the same thing - they have compilers that build executables, but really they're just packaging a Python interpreter with your script.

[–] moreeni@lemm.ee 0 points 9 months ago* (last edited 9 months ago)

JS is an interpeted language, you can't bundle stuff into a single binary. A "single binary" will require an interpreter to be bundled too, like Electton does

[–] Shaner@programming.dev 1 points 9 months ago (1 children)

You could do it pretty easily in Go.

Basically just use embedfs to add your javascript and HTML.

https://pkg.go.dev/embed

Here's a complete working example with a http server from that page:

package main

import (
	"embed"
	"log"
	"net/http"
)

//go:embed internal/embedtest/testdata/*.txt
var content embed.FS

func main() {
	mutex := http.NewServeMux()
	mutex.Handle("/", http.FileServer(http.FS(content)))
	err := http.ListenAndServe(":8080", mutex)
	if err != nil {
		log.Fatal(err)
	}
}
[–] Shaner@programming.dev 1 points 9 months ago

I see you want a screen saver and this just runs an app to view in your browser. There are other methods but they are a bit trickier. It's not too hard to add an embedded interpreter but you'd probably have to do some porting to get it drawing to a native canvas.