node module version conflict when installing modules for electron

前端 未结 3 1571
被撕碎了的回忆
被撕碎了的回忆 2020-12-05 07:17

I\'m trying to make an Electron application (https://electron.atom.io/) that reads data from my serial port. I\'m new to web technologies in general, I know some javascript,

相关标签:
3条回答
  • 2020-12-05 07:27

    Create file .npmrc with content:

    runtime = electron
    target = 1.7.5
    target_arch = x64
    disturl = https://atom.io/download/atom-shell
    export npm_config_runtime=electron
    export npm_config_build_from_source=true
    

    Open another terminal and run npm install [yourpackage]

    Keep in mind, some new packages would be installed with highest electron version (target), so save yourself some headache/backache and update your target = with the current version stated on npm or github page.

    0 讨论(0)
  • 2020-12-05 07:37

    When this type of version mismatch occurs, you can either choose an electron distribution with the target Node version or rebuild the npm package. Since Electron's distribution has skipped Node v7.0.0 which is configured with NODE_MODULE_VERSION 51 (and jumped to v7.4.0), you would have to rebuild the serialport package.

    In your app's directory (where package.json is located at),

    1. Install electron-rebuild

    npm install --save-dev electron-rebuild


    2. Rebuild

    ./node_modules/.bin/electron-rebuild



    Or, even a better option - set environment variables from the first place.

    # Electron's version.
    export npm_config_target=1.6.1
    # The architecture of Electron, can be ia32 or x64.
    export npm_config_arch=x64
    export npm_config_target_arch=x64
    # Download headers for Electron.
    export npm_config_disturl=https://atom.io/download/electron
    # Tell node-pre-gyp that we are building for Electron.
    export npm_config_runtime=electron
    # Tell node-pre-gyp to build module from source code.
    export npm_config_build_from_source=true
    # Install all dependencies, and store cache to ~/.electron-gyp.
    HOME=~/.electron-gyp npm install
    

    Take a look at the Electron's documentation page for using native Node modules. https://electron.atom.io/docs/tutorial/using-native-node-modules/

    0 讨论(0)
  • 2020-12-05 07:41

    electron-rebuild on postinstall.

    Depending on what you're doing, you can use electron-rebuild to rebuild serialport to the version of electron you have installed.

    To do so:

    npm install --save-dev electron-rebuild
    
    $(npm bin)/electron-rebuild                 # Mac and Linux.
    
    .\node_modules\.bin\electron-rebuild.cmd    # Windows.
    

    Because I kept forgetting to do this after doing an npm install (and to help others that downloaded the project), I added the following two scripts to package.json:

    "scripts": {
      "start": "electron .",
    
      "postinstall": "electron-rebuild",    
      "electron-rebuild": "electron-rebuild"
    },
    

    The postinstall will automatically run after doing a npm install so after the typical install finishes you'll see a console log message with electron-rebuild and it will automatically rebuild serialport, and any other native library you have, to the electron version. This means that you shouldn't even have to think about running electron-rebuild going forward.

    0 讨论(0)
提交回复
热议问题