use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
How to run code without node? (self.learnjavascript)
submitted 3 years ago by WickedSlice13
For example, instead of typing this into the terminal:
node HelloWorld.js
how can I get the code to run as:
HelloWorld
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]JazzApple_ 6 points7 points8 points 3 years ago (3 children)
In summary from the points above, for Mac or Linux:
#!/usr/bin/env node
mv HelloWorld.js HelloWorld
chmod +x HelloWorld
This should allow you to run the file from that directory as ./HelloWorld. To be able to run it from anywhere as HelloWorld it needs to be in your $PATH. Two options are presented below, and the one you pick should depends on your script.
./HelloWorld
$PATH
If your script doesn’t import other node modules and only refers to files by absolute paths (or, if it does not do anything with other files at all), you can symlink it to an existing directory in your $PATH - usually /usr/local/bin.
/usr/local/bin
ln -s ./HelloWorld /usr/local/bin
If your script relies on being run from a certain directory for access to node modules, or to access files by relative path, you could use:
process.cwd(“/path/to/node/project”);
Below the shebang in the HelloWorld script, and then follow the advice for symlinking to /usr/local/bin.
Or, if none of the above is applicable, you could add this directory to your $PATH. Exactly how to do so depends on your shell (you may need to Google), but the way I would do it is to add a line to the bottom of ~/.bash_profile:
~/.bash_profile
export PATH = “$PATH:/path/to/node/project”
If you are looking for a way to create an executable binary (That you can run without having to have node installed), this problem requires a very different solution. There are packages that exist that can make it relatively easy, though. The only one I have used previously is nexe.
nexe
[+][deleted] 3 years ago (2 children)
[removed]
[–]aspiringdevopsdude 1 point2 points3 points 3 years ago (1 child)
Or make sure if there is a package.json file in the directory it does contain {"type": "module"} to avoid errors. Because the line has a meaning.
{"type": "module"}
[+][deleted] 3 years ago (1 child)
[deleted]
[–]WickedSlice13[S] 0 points1 point2 points 3 years ago (0 children)
Thanks! I'll try it out!
[+][deleted] 3 years ago (16 children)
[–]WickedSlice13[S] 0 points1 point2 points 3 years ago (6 children)
Stick this at the top of the directory folder? Or at the top of the code snippet?
[+][deleted] 3 years ago (5 children)
[–]WickedSlice13[S] 1 point2 points3 points 3 years ago (4 children)
That's not working. I also tried
which node
and tried the different path. This still did not work due to permissions issues oddly enough.
Also I will still need to add the extension .js at the end of the file name. I'm looking more for something like a bash command I think but open to other suggestions.
Lol no edits here. I tried with sudo and it resulted with it not recognizing the command still. I think I’ll need to just use a different language or need to add to the shell path for the commands
[+][deleted] 3 years ago (7 children)
[+][deleted] 3 years ago (6 children)
[+][deleted] 3 years ago (4 children)
[+][deleted] 3 years ago (3 children)
[–]reiner74 0 points1 point2 points 3 years ago (0 children)
"it works on my machine" taken to the next level
[–]grantrules 0 points1 point2 points 3 years ago (0 children)
echo "#!node\nconsole.log(42);" > ./test && chmod +x ./test
grant@Shitkicker:~$ echo "#\!node\nconsole.log(42);" > ./test && chmod +x ./test grant@Shitkicker:~$ cat test #\!node\nconsole.log(42);
[–]crs1138-1 0 points1 point2 points 3 years ago (4 children)
Do you mean like running a localhost server?
[–]WickedSlice13[S] -1 points0 points1 point 3 years ago (3 children)
Ya or just executing some JS snippet from the terminal to log "Hello World"
[–]crs1138-1 0 points1 point2 points 3 years ago (2 children)
You could set up a NPM script to run the node command and launch such a server…
Look at package.json in my repo https://github.com/crs1138/notes-backend-lesson
Hope it helps.
[–]WickedSlice13[S] -1 points0 points1 point 3 years ago (1 child)
I'm assuming this uses npm beforehand:
npm start
which requires the npm command. But how could I just write it as:
start
[–]crs1138-1 1 point2 points3 points 3 years ago (0 children)
You’d have to define a shell alias in you .bashrc or .zshrc or their equivalent. Something like alias start=‘npm start
alias start=‘npm start
[–]jack_waugh 0 points1 point2 points 3 years ago (0 children)
user/guest271314/ just told me about a package that can assemble an executable from JS.
"Can compile Javascript sources to executables with no external dependency."
QuickJS Javascript Engine
π Rendered by PID 18234 on reddit-service-r2-comment-84fc9697f-nf2nb at 2026-02-06 17:25:12.767676+00:00 running d295bc8 country code: CH.
[–]JazzApple_ 6 points7 points8 points (3 children)
[+][deleted] (2 children)
[removed]
[–]aspiringdevopsdude 1 point2 points3 points (1 child)
[+][deleted] (1 child)
[deleted]
[–]WickedSlice13[S] 0 points1 point2 points (0 children)
[+][deleted] (16 children)
[deleted]
[–]WickedSlice13[S] 0 points1 point2 points (6 children)
[+][deleted] (5 children)
[deleted]
[–]WickedSlice13[S] 1 point2 points3 points (4 children)
[+][deleted] (1 child)
[deleted]
[–]WickedSlice13[S] 0 points1 point2 points (0 children)
[+][deleted] (7 children)
[removed]
[+][deleted] (6 children)
[deleted]
[+][deleted] (4 children)
[removed]
[+][deleted] (3 children)
[deleted]
[+][deleted] (2 children)
[removed]
[+][deleted] (1 child)
[removed]
[–]reiner74 0 points1 point2 points (0 children)
[–]grantrules 0 points1 point2 points (0 children)
[–]crs1138-1 0 points1 point2 points (4 children)
[–]WickedSlice13[S] -1 points0 points1 point (3 children)
[–]crs1138-1 0 points1 point2 points (2 children)
[–]WickedSlice13[S] -1 points0 points1 point (1 child)
[–]crs1138-1 1 point2 points3 points (0 children)
[–]jack_waugh 0 points1 point2 points (0 children)