Incorrect NODE_MODULE_VERSION when using ava

强颜欢笑 提交于 2020-12-13 03:49:25

问题


I write an Electron app that uses nodegit. For my test part I use ava in combination with Spectron to test my app. All of my tests work - including functions which use nodegit in my app.

In addition to the tests described above I made also a pure non-Electron test file in which I import nodegit directly.

 import * as nodegit from 'nodegit';

Executing this test now via ava returns this:

node_modules\.pnpm\nodegit@0.27.0\node_modules\nodegit\build\Release\nodegit.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 82. This version of Node.js requires
NODE_MODULE_VERSION 83. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
    at Module._extensions..node (internal/modules/cjs/loader.js:1122:18)

Where exactly does version 82 come from? I only have nodejs 14.15.0 installed, which uses version 83 as expected. Why does node think the version is a mismatch where it works actually in my app? This is how my package.json looks like:

  "devDependencies": {
    "ava": "^3.13.0",
  },
  "scripts": {
    "ava": "node_modules/.bin/ava",
     ...
  },
  "ava": {
    "files": [
      "*.ts"
    ],
    "extensions": [
      "ts"
    ],
    "require": [
      "ts-node/register"
    ],
    "nodeArguments": [
      "--napi-modules",
      "--experimental-modules"
    ]
  },

I built nodegit myself and in the config.gypi file it even refers to:

    "node_module_version": 83,

I made a super simple reproducible example: https://github.com/Githubber2021/node_module_version-issue

% node --version
14.15.0
% npm install
% npm run ava
... error

Can anyone explain me if this a bug or where version 82 comes from?


回答1:


This could be one of two things:

  1. The native dependency you're loading provides prebuilt binaries via prebuild -- This is probably the case
  2. The native dependency you're loading is downloaded as source code and building the binary is up to you.

According to nodegit's README,

"NodeGit will work on most systems out-of-the-box without any native dependencies."

But it looks like you need at minimum nodegit@0.27.x to get prebuilt binaries from use Node 14. Source

So the 82 you're seeing comes from the ABI version that the prebuilt binary was compiled against. Since you're already using nodegit@0.27.x, then somehow you've ended up with the prebuilt binaries for the wrong ABI.

Here's a repo I use that lists out all the various binaries for different versions, so you can see how this might happen: https://github.com/lovell/sharp/tree/v0.25.3

I develop in Electron and for Electron I use a command like this to get the correct version of Electron to run npm rebuild against.

npm rebuild --runtime=electron --target=8.5.3 --disturl=https://atom.io/download/atom-shell

I don't know exactly what the equivalent is for plain node, but let me know if this gets you closer and if so I'll see what I can find.




回答2:


Hey) I think I can solve your problem, just try this:

"engines": {
  "node": ">=14.0.0"
},

to your package.json and remove node_modules and do npm i in your project directory. It should help



来源:https://stackoverflow.com/questions/64625765/incorrect-node-module-version-when-using-ava

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