Using electron as a npm dev dependency on Ubuntu

Deadly 提交于 2019-12-11 06:50:26

问题


On Ubuntu 17.10 I'm installing and running electron like this:

    ole@mki:~/angular-electron$ npm i --save-dev electron

    > electron@1.7.12 postinstall /home/ole/angular-electron/node_modules/electron
    > node install.js

    npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.1.3 (node_modules/fsevents):
    npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.1.3: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

    + electron@1.7.12
    updated 1 package in 19.956s
    ole@mki:~/angular-electron$ electron
    electron: command not found

Ubuntu cannot find the electron command after install. Thoughts?


回答1:


When running npm i --save-dev electron the package will be installed in /home/ole/angular-electron/node_modules/electron (as you can see). To execute the binary you need to run /home/ole/angular-electron/node_modules/.bin/electron or $(npm bin)/electron.

I propose that you'll add a script in your package.json to run electron, for example:

"scripts": {
  "start": "electron"
}

npm will then automatically look into node_modules/.bin.




回答2:


This might be a little bit confusing but in general, there are 2 ways how to run npm packages.

  1. You can install the package globally npm install your-package-name -g

  2. You can install the package locally npm install your-package-name and then run it from /node_modules/.bin/electron

If you install your package locally you have also two options how to run the package from command line:

  1. Directly from node_modules like this: ../node_modules/.bin/electron

  2. You can create a script command in your

package.json:

"scripts": {
  "your-script-name": "electron"
}

then if you run npm run your-script-name npm will first look in the .bin directory and if it finds electron it will run it. Otherwise it will look at your global dependencies.

However, it is important not to forget that this command:

"your-script-name": "do-something && electron"

will also run electron globally. If you want to run electron locally you can either split the command into two separate commands or change electron to npm run electron like this:

"your-script-name": "do-something && npm run electron"


来源:https://stackoverflow.com/questions/48594344/using-electron-as-a-npm-dev-dependency-on-ubuntu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!