question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

INIT_CWD not always accurate in CLI

See original GitHub issue

Describe the bug Adding the platform results in a strange file-not-found error, although the file exists.

To Reproduce Steps to reproduce the behavior:

  1. Use rather recent Ionic templated app which generates web and apk builds nicely
  2. Run npx cap add @capacitor-community/electron
  3. Get error about missing configuration

Expected behavior It should add the capacitor-community electron platform

Screenshots

➜ npx cap add @capacitor-community/electron

> @capacitor-community/electron@1.3.2 capacitor:add
> node dist/cap-scripts.js add

⠋ Adding Electron platformError: capacitor.config.json does not exist, did you setup capacitor in your project root?
✖ Adding Electron platform: capacitor.config.json does not exist, did you setup capacitor in your project root?
(node:4565) UnhandledPromiseRejectionWarning: Error: capacitor.config.json does not exist, did you setup capacitor in your project root?
    at Object.n [as doAdd] (/home/riot/src/isomer/isomer_modules/leerstandsmelder/leerstandsmelder-2/frontend/leerstandsmelder-ionic-ng-autoentities/node_modules/@capacitor-community/electron/dist/cap-scripts.js:6:1016)
    at /home/riot/src/isomer/isomer_modules/leerstandsmelder/leerstandsmelder-2/frontend/leerstandsmelder-ionic-ng-autoentities/node_modules/@capacitor-community/electron/dist/cap-scripts.js:12:363
    at Object.g [as runTask] (/home/riot/src/isomer/isomer_modules/leerstandsmelder/leerstandsmelder-2/frontend/leerstandsmelder-ionic-ng-autoentities/node_modules/@capacitor-community/electron/dist/cap-scripts.js:2:1902)
    at i (/home/riot/src/isomer/isomer_modules/leerstandsmelder/leerstandsmelder-2/frontend/leerstandsmelder-ionic-ng-autoentities/node_modules/@capacitor-community/electron/dist/cap-scripts.js:12:311)
    at /home/riot/src/isomer/isomer_modules/leerstandsmelder/leerstandsmelder-2/frontend/leerstandsmelder-ionic-ng-autoentities/node_modules/@capacitor-community/electron/dist/cap-scripts.js:12:718
    at Object.parcelRequire.Focm../common (/home/riot/src/isomer/isomer_modules/leerstandsmelder/leerstandsmelder-2/frontend/leerstandsmelder-ionic-ng-autoentities/node_modules/@capacitor-community/electron/dist/cap-scripts.js:12:915)
    at f (/home/riot/src/isomer/isomer_modules/leerstandsmelder/leerstandsmelder-2/frontend/leerstandsmelder-ionic-ng-autoentities/node_modules/@capacitor-community/electron/dist/cap-scripts.js:1:468)
    at parcelRequire.FoEN (/home/riot/src/isomer/isomer_modules/leerstandsmelder/leerstandsmelder-2/frontend/leerstandsmelder-ionic-ng-autoentities/node_modules/@capacitor-community/electron/dist/cap-scripts.js:1:771)
    at Object.<anonymous> (/home/riot/src/isomer/isomer_modules/leerstandsmelder/leerstandsmelder-2/frontend/leerstandsmelder-ionic-ng-autoentities/node_modules/@capacitor-community/electron/dist/cap-scripts.js:1:1023)
    at Module._compile (internal/modules/cjs/loader.js:1015:30)
(node:4565) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:4565) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Desktop (please complete the following information):

  • OS: Debian Sid
  • Version:
    • npm: 7.3.0
    • ionic: 6.12.3

Additional context The capacitor configuration exists and has been used to add android and ios platforms already:

➜ cat capacitor.config.json 
{
  "appId": "de.foobar.app",
  "appName": "foobar",
  "bundledWebRuntime": false,
  "npmClient": "npm",
  "webDir": "www",
  "plugins": {
    "SplashScreen": {
      "launchShowDuration": 0
    }
  },
  "cordova": {}
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
cmladcommented, Jan 14, 2021

Facing exactly the same issue and the linked comment didn’t help.

The code [1] makes the search go 3 levels up, and so for me it’s looking for the config file in my home dir.

I am brand new to Node, but the workaround I found was to just set INIT_CWD to a dir 3 levels deep, in this case the electron dir that was already created:

export INIT_CWD=/home/XXX/src/YYY/node_modules/@capacitor-community/electron
npx cap add @capacitor-community/electron

Running from within a child dir didn’t work.

To repro on a brand new system:

# Install NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash

# Install latest Node (v15.5.1) and npm (0.37.2)
nvm install node

# Install Ionic (6.12.3)
npm install -g @ionic/cli

# Create an app
ionic start ZZZ sidemenu --type=react --capacitor

# Build (after CD-ing into the dir). I also tried ionic build, same result
npm run build

# Try to add electron and get same error as OP
npx cap add @capacitor-community/electron

One guess could be that my capacitor version is 2.4.6, but the docs seem to require 2.3.0 (Note: these instructions require a Capacitor version of ^2.3.0)

[1] https://github.com/capacitor-community/electron/blob/main/cap-scripts/common.ts#L49

2reactions
bnsouzacommented, Apr 14, 2021

Facing exactly the same issue and the linked comment didn’t help.

The code [1] makes the search go 3 levels up, and so for me it’s looking for the config file in my home dir.

[1] https://github.com/capacitor-community/electron/blob/main/cap-scripts/common.ts#L49

I have installed today the plugin and have the same error: capacitor.config.json does not exist.

  • OS: macOs Catalina 10.15.7.
  • npm: 7.3.0
  • node: v15.14.0

I saw this issue and this comment and tried to declare the INIT_CWD env but the error still there. So I debugged the code and saw the INIT_CWD was returning the webapp folder and not the node_modules/@capacitor-community/electron folder.

So I replaced function i(){const r=process.env.INIT_CWD;return e.join(r,"../","../","../")}

for function i(){const r=process.env.INIT_CWD;return e.join(r,"/")}

on node_modules/@capacitor-community/electron/dist/cap-scripts.js and it worked…

I don’t know if this is because of node/npm version or because of macOs… So I have changed the common.ts and made a PR…

I hope this can help others!

Read more comments on GitHub >

github_iconTop Results From Across the Web

npm-run-script
Description. This runs an arbitrary command from a package's "scripts" object. If no "command" is provided, it will list the available scripts.
Read more >
Questions & Answers | Yarn - Package Manager
A list of answers to commonly asked questions.
Read more >
How to reference a project root directory in an npm script
If you want your script to use different behavior based on what subdirectory you're in, you can use the INIT_CWD environment variable, which...
Read more >
Npm postinstall only on development - Stack Overflow
NPM sets an environment variable to "true" when install is run with --production. To only run the postinstall script if npm install was...
Read more >
Docker as a Tool Provider - Stephen Cleary
This is all good, until you want to make use of some nifty Linux tools. The thing is, Windows just doesn't work so...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found