问题
I'm working on a library for node.js that is build primarily as a native module. As such, when people try to include it in node-webkit projects, they have to recompile it using nw-gyp
instead of node-gyp
. I know we can detect node-webkit specifically when our built code runs using something like this:
try {
isNodeWebkit = (typeof require('nw.gui') !== "undefined");
} catch(e) {
isNodeWebkit = false;
}
However, I would like to detect this inside of our install script (run by npm install
). Alternatively, we look in our own package.json
, but is there maybe a way to look at the root project's package.json
? That way we could at least look at some property, maybe engine
or something?
回答1:
In order to look into your own package.json you can do something like this
gui = require(nw.gui);
myPackageJSONFile = gui.App.manifest; // this will get the package.json file
I hope this helps.
回答2:
I ended up writing a module to do this: https://github.com/maxkorp/which-native-nodish (also supports atom-shell and the renamed nw.js)
The gist is that you start at the parent directory to the module, and keep going up as long as you are a child of a node_modules folder which is a child of a folder with a package.json. Once at the root level, check the engines property in the package.json for an atom-shell, node-webkit or nwjs property. Not guaranteed to work (The farthest ancestor project must specify if its using a node-ish engine in this way), but it's better than nothing, and the only out of the box solution I've seen.
回答3:
Simply as this:
isNodeWebkit = (typeof process == "object");
来源:https://stackoverflow.com/questions/27154941/detecting-node-webkit-during-npm-install