37

Quite simply, I'm attempting to automate running a nodejs script using cron, however the script itself doesn't seem to be able to run the file. My script is simple:

#!/usr/bin/env node
node /var/node/assets/js/update.js

However, in running this, it returns that the beginning of the pathing is incorrect:

/home/dev/update.sh:2
node /var/node/assets/js/update.js
      ^^^
SyntaxError: Unexpected token var
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3

Is there something actually wrong with the bash, or does node have a specific way of doing this? I used /bin/env so that I could have the proper form of "node" regardless of version.

3 Answers 3

65

It looks like you are trying to run node from within node. The error message came from node and it looks like node was trying to run the command /var/node/assets/js/update.js.

I would make the shebang line specify bash rather than node.

The top line

#!/usr/bin/env node

means that what follows should be JavaScript code, not bash.

Sign up to request clarification or add additional context in comments.

5 Comments

Curses! Was just about to type the same. An other option is also to include the javascript directly inside that file.
Hm, in that sense though, wouldn't it mean that if I just provided the path it would work? Because when I only leave the path, it errors with such: pastie.org/pastes/7899197/text
I'm sorry. I don't follow. I see the error, but what exactly do you do to cause the error?
A bit late to tha party, but I guess... since this is a node environment, you can simply require your code. Never did that and not tested. @Rogue
@MaximilianHaindl 10 years later and I believe you'd be correct for both time periods. That said, I think for this task I would prefer calling into my node application through bash, as opposed to spreading out my node application in various places like cron.
22

You are already running node on the first line in an unmodified environment.

then on the second line you supply the command node /var/node/assets/js/update.js to that node process.

How about this:

#!/usr/bin/bash
node /var/node/assets/js/update.js

3 Comments

Nope, this seems to stall when running (regardless of which script I run)
How is this answer different from what @Ray is saying? What was your solution in the end?
I somewhat gave up on using the env node variable, and just made my script using bash instead. I merely ticked Ray's since his answer was first :)
-6

How about this?

#!/bin/bash

node /var/node/assets/js/update.js

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.