3

I'm just starting to learn about how JavaScript, HTML, and Electron all work, and I want to know what runs electron . in the "scripts" -> "start" of package.json, because I can't tell what does and that kind of wizardry makes me nervous.

According to the man pages for npm, what npm start does is that it reads the package.json, looks at the script under "scripts" -> "start" -> some_script, and then runs some_script. Sometimes, some_script is something like node foobar.js, which makes sense to me, since I can run that from the command line. NodeJS is executing foobar.js. However, in the case of the electron-api-demos, some_script is electron .

You can download and run electron-api-demos via

git clone https://github.com/electron/electron-api-demos
cd electron-api-demos/
npm install && npm start

In order to try to figure out what is running electron ., I've run it in the node shell, and I've tried running node main.js. I've even tried opening up the node shell and running

electron-api-demos@2.0.2 start $DIR/electron-api-demos
electron .

(which is exactly the output of npm start). None of them worked, because none of them started up the Electron application. At this point I'm very puzzled at how, exactly, the start script gets executed at all.

So I guess my question is: does there exist a command (that I can use on the command line) to start up this Electron application, without using npm? If not, what is npm calling to start up this Electron app?

I apologize if this question has been asked before, but I all the sources I found didn't seem to go into any further detail about what, exactly, is done when npm start is run and how it executes electron . . Thank you for your time!

logitropicity
  • 105
  • 1
  • 7
  • It is run in your operating system's shell ("executing environment"), probably bash in Linux/Mac and whatever passes for "DOS" in Windows nowadays. If the former, try ./electron . instead - the reason why just "electron ." doesn't work has to do with the PATH environment variable, which tells the operating system where executable programs may be found. When npm run scripts are executed, they assume the current working directory, but your shell does not. – Stoffe Mar 12 '19 at 06:17
  • To add on to that, when the script is "node somescript.js", node is an application that is on your PATH, and therefore found, and - somewhat counter-intuitive perhaps - node knows how to look in the current directory for the actual script to run. Don't actually know why current directory is not on PATH by default, we all just learn that it isn't ;) – Stoffe Mar 12 '19 at 06:26
  • @Stoffe Thanks for the help, but the funny thing is, when you download the git repository, there isn't a script called "electron" lying around. If I run ./electron . instead, it just says that it couldn't find any script. – logitropicity Mar 12 '19 at 16:03
  • Yeah, forgot about the node_modules/.bin/ part of the explanation, but I see you got some good answers already! – Stoffe Mar 14 '19 at 07:38

2 Answers2

4

Command line interfaces installed with npm are put in the node_modules/.bin/ directory. You can't just run them from the command line because that directory isn't in your PATH (unless you put it there, or you installed it globally).

So, if you want to run electron without npm start, you can run ./node_modules/.bin/electron .. Since this is a bit verbose, newer versions of npm provide the command npx to run things without the ./node_modules/.bin/ part, so npx electron . also works.

Since npm scripts often use the packages you've installed, they automatically add node_modules/.bin/ to the PATH before running your command. As a result, the start script can just reference electron directly.

npx can do some other cool things too – npm has a blog post about it.

Half
  • 5,078
  • 2
  • 10
  • 20
  • Works like a charm. Now, at least I can trace the execution of scripts a bit better. – logitropicity Mar 12 '19 at 16:10
  • it always tells me "`Electron failed to install correctly, please delete node_modules/electron and try installing again...`" when i try to run `npx electron` from one of my projects' directories ?? also, would you mind checking out [my question over here??](https://stackoverflow.com/q/59316390/7543162) i'd really appreciate that <3 – oldboy Dec 29 '19 at 02:41
2

When you run npm start , it by default run command corresponding "start" key of script property of package.json like

"script":{
   "start": "ng serve",
    "launch":"electron main.js" or "electron ."  // main.js located in the same dir
     "test": " ng test" 
 }

same when you run npm run launch it will trigger the command corresponding of the "launch" key of script property of package.json file. like run electron main.js command and your application will launched.

so if you want to run the your electron application directly like electron main.js then install the electron module globally using command npm install electron -g then simply run the electron main.js command and your application will start.

Sunny Goel
  • 1,982
  • 2
  • 15
  • 21
  • Thanks for the help, but if I did install electron globally, my follow up question would be: where is the electron script in my system? Where was it before I installed it globally? (I suspect if it were installed globally, it'd be somewhere in /usr/share/). So this post, while relevant, doesn't exactly answer my question. – logitropicity Mar 12 '19 at 16:18
  • see when you install globally it will be present in " c:/users/User_name/AppData/Roaming/npm/node_modules" and it is available for every project in your system and when you install locally it will save in your current working project node_modules folder which only available for that project not for all. – Sunny Goel Mar 12 '19 at 17:12