简体   繁体   English

npm读取package.json之后,什么将运行Electron?

[英]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 . 我才刚刚开始了解JavaScript,HTML和Electron的全部工作原理,并且我想知道electron .运行原理electron . in the "scripts" -> "start" of package.json , because I can't tell what does and that kind of wizardry makes me nervous. package.json的“脚本”->“开始”中,因为我不知道该怎么做,这种巫术使我感到紧张。

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 . 根据npm的手册页, npm start所做的是读取package.json ,查看“ scripts”->“ start”-> some_script下的脚本,然后运行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. 有时, some_script类似于node foobar.js ,这对我来说很有意义,因为我可以从命令行运行它。 NodeJS is executing foobar.js. NodeJS正在执行foobar.js。 However, in the case of the electron-api-demos , some_script is electron . 但是,对于electron-api-demossome_scriptelectron .

You can download and run electron-api-demos via 您可以通过以下方式下载并运行electron-api-demos

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 . 为了设法弄清楚正在运行的electron . , I've run it in the node shell, and I've tried running node main.js . ,我已经在node shell中运行了它,并且尝试运行node main.js I've even tried opening up the node shell and running 我什至尝试打开node外壳并运行

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

(which is exactly the output of npm start ). (这正是npm start的输出)。 None of them worked, because none of them started up the Electron application. 他们都不工作,因为他们都没有启动Electron应用程序。 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 ? 所以我想我的问题是:是否存在一个命令(我可以在命令行上使用)来启动此Electron应用程序,而不使用npm If not, what is npm calling to start up this Electron app? 如果没有,那么npm调用什么来启动这个Electron应用程序?

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 . 我很抱歉以前没有问过这个问题,但是我发现的所有消息来源似乎都没有进一步详细介绍npm start运行时到底要完成什么以及如何执行electron . . Thank you for your time! 感谢您的时间!

Command line interfaces installed with npm are put in the node_modules/.bin/ directory. 与npm一起安装的命令行界面位于node_modules/.bin/目录中。 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). 您不能仅从命令行运行它们,因为该目录不在您的PATH (除非您将该目录放在其中,或者将其全局安装)。

So, if you want to run electron without npm start , you can run ./node_modules/.bin/electron . 因此,如果要在不npm start情况下运行电子,则可以运行./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 . 由于这是一个有点冗长,较新版本的npm提供的命令npx到没有运行的东西./node_modules/.bin/一部分,所以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. 由于npm脚本经常使用您已安装的软件包,因此在运行命令之前,它们会自动将node_modules/.bin/添加到PATH 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. npx也可以做其他一些很酷的事情-npm上有一篇有关它的博客文章

When you run npm start , it by default run command corresponding "start" key of script property of package.json like 当您运行npm start时 ,默认情况下,它将运行package.json 脚本属性的“ start”键对应的命令

"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. 同样,当您运行npm run launch时 ,它将触发package.json文件的脚本属性的“启动”键对应的命令。 like run electron main.js command and your application will launched. 像运行电子main.js命令,您的应用程序将启动。

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. 因此,如果您想像电子main.js一样直接运行电子应用程序,然后使用命令npm installelectron -g全局安装电子模块,然后只需运行电子main.js命令即可启动应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM