capacitor icon indicating copy to clipboard operation
capacitor copied to clipboard

Automatically added npm script build:nativescript does not work on Windows

Open Ionaru opened this issue 1 year ago • 0 comments

First reported here: https://github.com/NativeScript/capacitor-docs/issues/14

When following the Nativescript for Capacitor installation instructions, the postinstall step for npm i @nativescript/capacitor adds an npm script that does not work on Windows (and maybe other platforms?).

Running npm run build:mobile further along in the instructions, it will error.

> [email protected] build:nativescript
> node --experimental-import-meta-resolve node_modules/.bin/build-nativescript

C:\Users\NAME\Projects\PROJECT\node_modules\.bin\build-nativescript:2
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
          ^^^^^^^

SyntaxError: missing ) after argument list
    at internalCompileFunction (node:internal/vm:77:18)
    at wrapSafe (node:internal/modules/cjs/loader:1288:20)
    at Module._compile (node:internal/modules/cjs/loader:1340:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
    at node:internal/main/run_main_module:28:49

Node.js v20.10.0
ERROR: "build:nativescript" exited with 1.

This is caused by the npm script that looks like this:

"build:nativescript": "node --experimental-import-meta-resolve node_modules/.bin/build-nativescript",

It tries to call node_modules/.bin/build-nativescript using Node, but node_modules/.bin/build-nativescript is a shell script.

The line that causes the problem: https://github.com/NativeScript/capacitor/blob/203b85f65af1c7e2a6bb16f3de2d369c9f4bda44/src/postinstall.mts#L76

The npm script should be changed to call node_modules/.bin/build-nativescript as a regular shell script, or directly call node_modules/@nativescript/capacitor/bin/build-nativescript.mjs using this:

"build:nativescript": "node --experimental-import-meta-resolve node_modules/@nativescript/capacitor/bin/build-nativescript.mjs",

( The correct command errors with another message, but that is a separate issue )

Ionaru avatar Mar 27 '24 13:03 Ionaru