Making .js file executable through chmod +x

nodejs
environment

#1

Hi!
I’m going through the tutorial, and I need to make an executable .js file. It is stated the the first line of the .js file should be !#/usr/bin/env node and then I should issue the command chmod +x file.js. When I do that, there is respond: bash: file.js: command not found
what am I doing wrong?
thanks!


#2

To clarify, does the error appear after you run chmod, or when you try to run it using ./file.js. In either case, it’s likely you’re either using the wrong filename or you’re in the wrong directory. First, check that the file with all of your JavaScript is called file.js (or if that’s a placeholder, whatever the name of the file should be). Then, find the file in the file tree to the left. If it’s in a folder, use the cd command in Bash like cd folder. You may have to do this a few times (you can also do cd folder/subfolder if there are multiple subfolders), but once you’re in the folder with the JS file, you can run chmod from there. If you’re unsure how to navigate the directory structure, run ls to list the folders and files in the directory, so you can match them up to the file tree.


#3

Thanks for the reply!
The error appeared when I ran file.js. I navigated to the folder of the file to issue chmod +x file.js command, and I recieved no error. Then I ran ./file.js and this is what I have now: ./file.js: line 1: !#/usr/bin/env: No such file or directory
I also tried !#/usr/bin, without env - the same error. I navigated through the environment to ensure there is the usr/bin/env path. I also tried sudo chmod +x file.js, but nothing changed.
Any ideas what it might be?
Thanks!


#4

I’m not sure if this will help, but you might try running which node in order to get the Node binary location, then replace your shebang with that.


#5

tried it, got the same error…