Running node from a bash script - node.js

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.

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.

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

How about this?
#!/bin/bash
node /var/node/assets/js/update.js

Related

Node.js on windows tries to run linux version of newman

I have newman (shell runner for postman tests) installed on Windows 10.
When i try to run a script via node which calls the newman module i get the following error:
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
^^^^^^^
SyntaxError: missing ) after argument list
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:599:28)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Module.require (module.js:579:17)
at require (internal/module.js:11:18)
at Object.<anonymous>
It is trying to run some bash code here which fails for obvious reasons.
I noticed that in the C:...\AppData\Roaming\npm folder there are two files for newman - a windows batch script and a linux bash script both called newman which contains the code mentioned above.
Why is it trying to run the linux version of newman on my windows machine? When i remove the bash script it says it cannot find a newman module. It doesn't even try to run the windows batch file instead, it is completely ignored.
Edit: Just reinstalled npm newman module just to be sure, didn't help.

Can't run node module geojson-polygon-labels on Windows

I've download the package from "https://github.com/andrewharvey/geojson-polygon-labels/", ran "npm-install" and the node-modules folder was successfully created:
folder - screenshot
However, when cd to bin folder and run "geojson-polygon-labels < v.geojson > v_label.geojson", it throws the eror: "not recognized as internal or external command, operable program or batch file".
I'm really confused at this point. On github it's not really well-documented how to use this tool, and I'm quite new to Node too.
Please help me. Thanks!
Edit: I've tried install it on Ubuntu (subsystem on Windows 10) then run "geojson-polygon-labels" from the bin folder. The same error occured.
Edit2: I figured out to how properly run the tool. On Ubuntu I ran:
./geojson-polygon-labels < polygon.geojson > labels.geojson
However, now a new error pops out:
/home/lamnguyen/geojson-polygon-labels-master/bin/geojson-polygon-labels:25
turf.featureEach(flatInputGeoJSON, (feature) => {
^
SyntaxError: Unexpected token >
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:902:3
What should I do now?
Edit 4: A friend of mine ran it on a "real" Ubuntu with a small sample file and it worked out. Maybe tomorrow I've give a shot :)
In this package I've set up the bin property in the package.json https://docs.npmjs.com/files/package.json#bin so when you run npm install it knows what scripts are executables. You might need to set up your PATH in your shell to the directory NPM installs the binaries to. Or use the full or relative path as you've found out.
The Unexpected Token > message seems like you're running an older version of node which doesn't support ES6 arrow functions, so try upgrading your version of node?

Arrow functions not working in node --harmony under Ubuntu

I'm trying to use arrow functions in node v0.10.33 under Ubuntu 14.04 (I'm running node with --harmony flag), but I'm getting this error:
console.log( [1,2,3,4].map(x => x*x) );
^
SyntaxError: Unexpected token >
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:906:3
This should work now in node v0.12.x, with the --harmony flag. Also note that you can get arrow functions in node using the babel package.
http://babeljs.io/docs/using-babel/
UPDATE:
As correctly indicated by Mike 'Pomax' Kamermans, the --harmony flag is not required when using Node.js >= 4.x. Please see https://nodejs.org/en/docs/es6/ for more information on the status of ES6 support in Node.js.
Node, even with the harmony flag, doesn't support the fat arrow yet. Source.
Edit: Fun little fact, ES6 support is one of the reasons Node got forked into io.js. Check out their page on ES6 features - they provide a flag for the arrow functions, but discourage using it.

Sudo node server return error, OsX

There is the case:
mac:~ username$: sudo node server
module.js:340
throw err;
^
Error: Cannot find module '/Users/username/server'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
So here is 2 questions:
How to specify modules folder in node.js(node on my machine is taking place on /usr/local/bin/node); may be u could give me a advice how I may organise my project files.
How could I prevent this type of errors in future? What could I read about it?
I think you don't get what exactly Node will do with your command.
With this command, Node is trying to load a script file (in your current folder) with no extension named server.
It looks like this file doesn't exist in your current directory.
Let's take a simple example: you have a file named app.js in your current folder which contains a simple output in the console.
// In the app.js file
console.log("Output in the console by NodeJS");
Now, run the command node app.js. You should have the output in your console.
Talking about organisation, it depends of your workflow and projects. You could find lot of informations about this by reading the source code of popular projects on Github.

NODE_PATH error with node.js while attempting to setup jsctags for vim

I am trying to setup doctorjs on my windows machine, to work with vim's tagbar, but I think this may be a node.js question more than anything else. I'm following this tutorial. Even after I set my NODE_PATH, I still get an error claiming that it needs to be set. What could be going wrong?
Here is a terminal log on my win7 machine:
C:\Windows\system32>set NODE_PATH=C:\Users\JG\Desktop\new\doctorjs\lib\jsctags
C:\Windows\system32>node.exe C:\Users\JG\Desktop\new\doctorjs\bin\jsctags.js -h
'node.exe' is not recognized as an internal or external command,
operable program or batch file.
C:\Windows\system32>cd c:\Users\JG\Desktop\new\doctorjs
c:\Users\JG\Desktop\new\doctorjs>node.exe C:\Users\JG\Desktop\new\doctorjs\bin\j
sctags.js -h
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: require.paths is removed. Use node_modules folders, or the NODE_PATH envi
ronment variable instead.
at Function.<anonymous> (module.js:376:11)
at Object.<anonymous> (C:\Users\JG\Desktop\new\doctorjs\bin\jsctags.js:41:8)
at Module._compile (module.js:432:26)
at Object..js (module.js:450:10)
at Module.load (module.js:351:31)
at Function._load (module.js:310:12)
at Array.0 (module.js:470:10)
at EventEmitter._tickCallback (node.js:192:40)
c:\Users\JG\Desktop\new\doctorjs>
In node.js 0.6.x the require.paths has been removed. It has been deprecated since 0.2.x if I recall. So the problem isn't the lack of a NODE_PATH environment variable, but that the package / app you are running isn't compatible with node 0.6.x. Normal solution would be to run this app in node.js 0.4.12. Unfortunately there is no supported version of 0.4.x for Windows. Best bet is to rewrite the application so that require.paths isn't used anymore.
Furthermore: Don't start the app like node.exe C:\Full\Path\Folder, because the working directory will be C:\. Therefore do something like:
C:\Full\Path\Folder> C:\node.js\bin\node.exe Folder.

Resources