3
votes

I recently started building iOS apps using Cordova and I’ve hit a roadblock. After installing nodeJS, git and cordova (with sudo), I created my first app in the Documents folder of my user account.

The first run went perfectly. Everything worked, and adding my dev account to Xcode helped my app to run on the device. I decided I’d spice things up a bit by adding a “before_prepare” hook called 001_c.js in <appName>/hooks/before_prepare folder. This is how the hook begins:

//This is where nodeJS exists
#!/usr/local/bin node

console.log("Changing config");

var fs = require('fs');
var path = require('path');

var rootdir = process.argv[2];

//and so on

When I now build the app, I get this error:

pc295786:master kellster$ cordova build ios Running command: /Users/kellster/documents/apps/master/hooks/before_prepare/001_c.js /Users/kellster/documents/apps/master Error: spawn EACCES at exports._errnoException (util.js:746:11) at ChildProcess.spawn (child_process.js:1155:11) at Object.exports.spawn (child_process.js:988:9) at Object.exports.spawn (/usr/local/lib/node_modules/cordova/node_modules/cordova-lib/src/cordova/superspawn.js:104:31) at runScriptViaChildProcessSpawn (/usr/local/lib/node_modules/cordova/node_modules/cordova-lib/src/hooks/HooksRunner.js:188:23) at runScript (/usr/local/lib/node_modules/cordova/node_modules/cordova-lib/src/hooks/HooksRunner.js:131:16) at /usr/local/lib/node_modules/cordova/node_modules/cordova-lib/src/hooks/HooksRunner.js:114:20 at _fulfilled (/usr/local/lib/node_modules/cordova/node_modules/q/q.js:787:54) at self.promiseDispatch.done (/usr/local/lib/node_modules/cordova/node_modules/q/q.js:816:30) at Promise.promise.promiseDispatch (/usr/local/lib/node_modules/cordova/node_modules/q/q.js:749:13)

To troubleshoot, I changed the first line of the hook script from

#!/usr/local/bin node

to

#! node

(Because this worked on Windows. node was in global scope). But, this resulted in an ENOENT error:

pc295786:master kellster $ cordova build ios Running command: /Users/kellster/documents/apps/master/hooks/before_prepare/001_c.js /Users/kellster/documents/apps/master Error: Hook failed with error code ENOENT: /Users/kellster/documents/apps/master/hooks/before_prepare/001_c.js at /usr/local/lib/node_modules/cordova/node_modules/cordova-lib/src/hooks/HooksRunner.js:194:23 at _rejected (/usr/local/lib/node_modules/cordova/node_modules/q/q.js:797:24) at /usr/local/lib/node_modules/cordova/node_modules/q/q.js:823:30 at Promise.when (/usr/local/lib/node_modules/cordova/node_modules/q/q.js:1035:31) at Promise.promise.promiseDispatch (/usr/local/lib/node_modules/cordova/node_modules/q/q.js:741:41) at /usr/local/lib/node_modules/cordova/node_modules/q/q.js:557:44 at flush (/usr/local/lib/node_modules/cordova/node_modules/q/q.js:108:17) at process._tickCallback (node.js:355:11) at Function.Module.runMain (module.js:503:11) at startup (node.js:129:16)

How do I get this to build? I’d appreciate any suggestions I could try.


Some things to note:

  1. (In my desperation, ) I tried running the following commands, because the "EACCES" type of error. But none of them seemed to have any kind of effect on the result. The build was still failing.
sudo chmod 777 “/Users/kellster/documents/apps/master"
sudo chmod 777 “/usr/local/lib”
sudo chmod 777 "/usr/local/bin/"
chmod 777 "/Users/kellster/documents/apps/master/platforms/"
sudo chmod a+rwx "/Users/kellster/Documents/apps/Master/hooks/before_prepare/001_c.js"
sudo chmod a+rwx "/Users/kellster/Documents/apps/Master/"
sudo chown -R kellster /usr/local/lib/node_modules/cordova

2) Node is installed in

/usr/local/bin

1
does sudo cordova build ios work? That'll at least rule out access permissions as the the cause...DaveAlden
Nope @DaveAlden. It does not. Tried that as wellkrishwader

1 Answers

11
votes

Old answer: #!/usr/local/bin node -> better #!/usr/bin/env node

Updated answer: do not use spaces for referencing node executable instead use:

     #!/usr/local/bin/node 

Also chmod your script: For example:

      chmod 777 hooks/before_prepare/onde.js 

where onde.js it is your script. If you're on Mac(my case) or Linux, then your .js must on chmod 777 to avoid EACCES errors.