After npm reads package.json, what runs Electron?

问题: 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, becaus...

问题:

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!


回答1:

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.


回答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.

  • 发表于 2019-03-13 06:16
  • 阅读 ( 187 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除