Cordova after_prepare hook succeeds in Android but fails in iOS

I have written a after_prepare hook for my Cordova build which removes the node_modules folder from the final build:

#!/usr/bin/env node

 * The node modules we want to remove from the build prior to building
 * @type {Array}
var foldersToRemove = ["platforms/android/assets/www/node_modules", "platforms/ios/www/node_modules"];

var fse = require('fs-extra');
var path = require('path');
var rootdir = process.argv[2];

foldersToRemove.forEach(function(folder) {

    var rmFolder = path.join(rootdir, folder);

    fse.remove(rmFolder, function(err) {
        if (err) {
            return console.error(err);
        } else {
            console.log("fse folder removed success!")


This works for me when i run cordova prepare android -d in in the CLI but upon switching to iOS it fails with the following error:

env: node\r: No such file or directory
Hook failed with error code 127:

I have tried with just the reference to the ios platform folder and it issues the same error message.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

You need to use a text editor like NotePad++

On NotePad++, you will follow these steps:

  • Open the file thats giving you issues
  • Go to ‘Find’ option and select the ‘Replace’ tab
  • You should find this string : ‘\r\n’
  • Replace it with \n
  • Make sure the ‘wrap around’ option is selected.
  • In the search mode, select ‘Extended’, and then go ahead and replace

  • Save the file and perform the iOS build again

Solution 2

Try this in the terminal on OSX:

tr -d '\r' < FILE_NAME > FILE_NAME

Solution 3

This error can be caused while trying to build your Ionic or Cordova application under OSX and it is most likely due to the line ending format in the file in question.
Try this

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply