If a node module is written in C, and I install it

2019-09-09 04:26发布

I frequently hear from experienced node developers that checking in node_modules is good practice. But most developers write on Mac/Darwin x64, but deploy on Linux x64.

If a node module happens to be written in C, and I install it on OS X, wouldn't I have to rebuild it on Linux?

2条回答
在下西门庆
2楼-- · 2019-09-09 04:44

The answer is: it depends on the package

  • Most packages do require reinstall, as node gyp the compiler does not cross compile by default - thanks @tkone.

  • Some packages like node-sass dynamically download prebuilt binaries for the relevant platform (previously node-sass used to include binaries for all platforms, however this has changed recently).

查看更多
一纸荒年 Trace。
3楼-- · 2019-09-09 05:00

The now current node-sass 3.4.2 doesn't include the binaries in the originally downloaded npm package (as found in the npm package cache under ~/.npm).

What it does is that its install.js script will download the platform specific binary and store it under vendor/{platform}-{arch}-{process.versions.module}.node. Once installed, the install.js script normally isn't invoked again by npm.

So when you check in node_modules, it will contain the binary only for your initial platform.

For the fun of it I moved away the downloaded binary, which should be the same as someone else checking out your node_modules on a different platform. When then run, node-sass is so smart to detect that the required binary doesn't exist. It exits gracefully, recommending to run npm rebuild node-sass (also hinting that this is usually necessary when you change the node version). This will download the binary for the current platform, and then all is fine.

However, that's totally specific to node-sass, other packages with binaries may behave entirely different.

(See Node.js release versions for an explanation of process.versions.modules aka. NODE_MODULES_VERSION that determines the last number in the binary download URL. The binary name is constructed in function getBinaryName() in ~/.npm/node-sass/{version}/package.tgz/lib/extension.js )

查看更多
登录 后发表回答