简体   繁体   English

Electron JS:在内部/以编程方式使用npm

[英]Electron js: use npm internally/programmatically

I wrote an app with Electron js that encapsulate Ionic apps for editing them live. 我用Electron js编写了一个应用程序,其中封装了用于实时编辑它们的Ionic应用程序。 So i use a child process to cd {ionic app path} && ionic serve / npm install / npm update for serving and updating packages of the live Ionic app in my Electron container. 因此,我使用一个子进程来cd {ionic app path} && ionic serve / npm install / npm update以服务和更新我的Electron容器中实时Ionic应用程序的软件包。 No problems with this technique on my side. 我这一方面的技术没有问题。 But when i package my app and use an installer to test it on a clean machine, npm cannot be executed because of nodejs that is not installed on it. 但是,当我打包我的应用程序并使用安装程序在干净的计算机上对其进行测试时,由于未安装nodejs,因此无法执行npm。

First I was thinking of including a nodejs installer into my main app installer but this does not seem to me that is the good way of doing it. 首先,我考虑将nodejs安装程序包含到我的主应用程序安装程序中,但是在我看来,这不是实现此目的的好方法。

And after digging on stackoverflow I've found this thread: Install programmatically a NPM package providing its version that explain how to use npm directly in my code with require("npm"); 在研究了stackoverflow之后,我发现了这个线程:以编程方式安装一个NPM软件包,提供其版本该版本说明如何在我的代码中使用require("npm");直接使用npm require("npm"); and that's worked but i was not able to tell npm.install() in which folder i want to run this command what was possible with child process. 那是可行的,但是我无法告诉npm.install()我想在哪个文件夹中运行此命令,子进程可能会发生什么。

I has tried to read the lib but this not seams to be possible: https://github.com/npm/npm/blob/latest/lib/install.js 我已经尝试阅读lib了,但是这不可能: https : //github.com/npm/npm/blob/latest/lib/install.js

Do you have any idea what I can do to solve this problem ? 您是否知道我可以解决这个问题?

So I've found the answer after digging into this code https://github.com/npm/npm/blob/latest/lib/install.js Simply use npm like this : 因此,在深入研究这段代码后,我找到了答案https://github.com/npm/npm/blob/latest/lib/install.js只需使用npm像这样:

npm.load({}, function (err) {
  npm.commands.install(HERE_A_PATH, [], function(er, data){
    //callback here
  });
  npm.on("log", function (msg) {
    console.log(msg + '');
  });
});

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

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