Error: Cannot read property 'replace' of undefined when building iOS Cordova

Phoenix214 picture Phoenix214 · Feb 20, 2017 · Viewed 27.4k times · Source

I created a cordova project using cordova create project hello com.hello Hello.

And added iOS platform using cordova platform add iOS. And tried to do cordova run ios after cordova build ios.

But it shows me this error(I used --d/ --verbose to get the details).

/usr/bin/codesign --force --sign - --timestamp=none /Volumes/Untitled/Plot/PlotReel/platforms/ios/build/emulator/PlotReel.app /Volumes/Untitled/Plot/PlotReel/platforms/ios/build/emulator/PlotReel.app: replacing existing signature

** BUILD SUCCEEDED **

No scripts found for hook "before_deploy". Error: TypeError: Cannot read property 'replace' of undefined

at remove (/Volumes/Untitled/Plot/test/platforms/ios/cordova/node_modules/ios-sim/src/lib.js:282:70)
at Array.forEach (native)
at Object.getdevicetypes (/Volumes/Untitled/Plot/test/platforms/ios/cordova/node_modules/ios-sim/src/lib.js:292:22)
at Object.listEmulatorImages [as run] (/Volumes/Untitled/Plot/test/platforms/ios/cordova/lib/list-emulator-images:34:29)
at deployToSim (/Volumes/Untitled/Plot/test/platforms/ios/cordova/lib/run.js:146:50)
at /Volumes/Untitled/Plot/test/platforms/ios/cordova/lib/run.js:88:20
at _fulfilled (/Volumes/Untitled/Plot/test/platforms/ios/cordova/node_modules/q/q.js:834:54)
at self.promiseDispatch.done (/Volumes/Untitled/Plot/test/platforms/ios/cordova/node_modules/q/q.js:863:30)
at Promise.promise.promiseDispatch (/Volumes/Untitled/Plot/test/platforms/ios/cordova/node_modules/q/q.js:796:13)
at /Volumes/Untitled/Plot/test/platforms/ios/cordova/node_modules/q/q.js:604:44

I have tried uninstalling and installing cordova again, but the problem is still remaining.

Please help me.

Answer

Tadej picture Tadej · Jun 9, 2017

New solution

This issue is fixed in the latest version of the "ios-sim" package (so now this is probably the easier solution - compared to the old one which is listed below). In order to update the "ios-sim" package to the latest version run this in your terminal/cmd:

cd platforms/ios/cordova/node_modules/
sudo npm install -g ios-sim@latest

Old solution

The problem is that the name_id_map[deviceName] returns undefined for "iPad Pro (12.9-inch)" and "iPad Pro (10.5-inch)".

You can check it with a console.log('name_id_map[ deviceName ]: ' + name_id_map[ deviceName ]);.

I fixed this bug by adding an if statements which checks if the device is defined in "platforms/ios/cordova/node_modules/ios-sim/src/lib.js:282".

I replaced this:

list = [];
        var remove = function(runtime) {
            // remove "iOS" prefix in runtime, remove prefix "com.apple.CoreSimulator.SimDeviceType." in id
            list.push(util.format('%s, %s', name_id_map[ deviceName ].replace(/^com.apple.CoreSimulator.SimDeviceType./, ''), runtime.replace(/^iOS /, '')));
        };

with this:

list = [];
        var remove = function(runtime) {
            // remove "iOS" prefix in runtime, remove prefix "com.apple.CoreSimulator.SimDeviceType." in id
            if (name_id_map[deviceName] && runtime) {
                list.push(util.format('%s, %s', name_id_map[deviceName].replace(/^com.apple.CoreSimulator.SimDeviceType./, ''), runtime.replace(/^iOS /, '')));
            }
        };

The "iPad Pro (10.5-inch)" simulator won't be on the list (but it doesn't probably work anyway - didn't check).

Bug report on github: https://github.com/phonegap/ios-sim/issues/210