JamesEggers
JamesEggers

Reputation: 12935

Command Not Found when writing a Node Shell Script

I am attempting to create a shell script using Node.js similar to packages like express and mocha. My initial test reported command not found so I backed the script to the following:

#!/usr/bin/env node

console.log("printing stuff");

The above should just output "printing stuff" when ran; however, that too doesn't work.

I'm attempting to execute the script via ./script in the directory of the file in order to ensure it's not a pathing issue. Attempting to execute under a privileged state (i.e. sudo) yields the same results/error message.

Any idea what I need to do?

Upvotes: 1

Views: 2942

Answers (2)

JamesEggers
JamesEggers

Reputation: 12935

Figured out my issue. I made the shell script but forgot to set execute permissions onto the file. doing a chmod a+x script resolved the issue.

Upvotes: 2

Peter Lyons
Peter Lyons

Reputation: 146014

When you shebang (#!) /usr/bin/env node, you are saying "look for a program called node on my PATH". If doing node -v works, as @Ryan says, the shebang should work fine. Any chance you have a shell alias or shell function named node? That could be giving you the illusion that your PATH is correct even when it is not. But essentially, just make sure the directory where the node binary lives (the bin directory under your node install) is on your PATH environment variable, and things really should work at that point.

export PATH=/usr/local/node/bin:$PATH

Upvotes: 2

Related Questions