188

What is node-gyp and why does it use my system files to build Node.js packages?

If I build a Node project that uses node-gyp internally then I just tar that project and move it to a different system, untar it and try to use it, will that work?

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
selftaught91
  • 7,013
  • 3
  • 20
  • 26
  • 21
    It's described on its github page: https://github.com/nodejs/node-gyp. Basically, `node-gyp` is like `make`. It's a tool used to control the compilation process of C++ projects. Only, it's designed specifically for node.js addons (modules written in C++). So moving to a different system may work if it uses the same CPU. Moving to a different OS or CPU (for example from x86 to ARM) won't work. For Linux, moving to a different distro of different version of the same distro may or may not work. I'm not 100% sure if moving to a different version of node.js would work – slebetman Sep 28 '16 at 08:04
  • 6
    When I run `yarn why node-gyp` it comes back with: because `node-sass`, so I'm sure it has other uses, but it may be in your project if you or someone you love has been diagnosed with `sass`. – Kzqai Mar 25 '20 at 19:53
  • @slebetman moving to different node version won't work, not sure if that's 100% true, yet from my experience, you need to remove old node-gyp after upgrading nodejs version and during modules install it's re-build automatically in the background. – Lukas Liesis Apr 18 '20 at 12:17
  • 1
    Why is it always broken? – Alper Oct 08 '21 at 16:33

1 Answers1

203

node-gyp is a tool which compiles Node.js Addons. Node.js Addons are native Node.js Modules, written in C or C++, which therefore need to be compiled on your machine. After they are compiled with tools like node-gyp, their functionality can be accessed via require(), just as any other Node.js Module.

If you do what you suggested the module won't work, you will need to compile it/build it with node-gyp on the system you moved the program to.

node-gyp: https://github.com/nodejs/node-gyp

Node.js Addons: https://nodejs.org/api/addons.html

Yves M.
  • 29,855
  • 23
  • 108
  • 144
vuza
  • 2,494
  • 2
  • 12
  • 12
  • 66
    gotta admit, i see a module no npmjs that uses node-gyp I usually run 10 miles away – PirateApp Sep 05 '18 at 13:43
  • 106
    It's exciting that you nowadays can clone a (what seems) relatively simple web app written in NodeJS, and all of a sudden you must download software to compile native coden written in C and C++. The best part is when you run into problems, and you know exactly what do with your web design skills to solve this. – Jim Aho Dec 18 '18 at 13:23
  • so you can basically use node-gyp to run ANY c/c++ code? is this transformed to javascript or executed as it is and connected through a binding? – Mr-Programs Jan 13 '19 at 23:43
  • 4
    @Mr-Programs It is connected through a binding, which also needs to be written by you – vuza Jan 16 '19 at 08:43
  • 2
    @JimAho Ideally, and often, the code is just black-boxed. The good is that you can use fast and well supported tools to do specific things like parse xml, and not have to wait for a js port which might be slower or more broken. That said, I prefer native js whenever possible, but I'm not certain that position is well founded. Also, C/C++ should be very portable for any case that is not purposefully OS dependent (like notifications). Furthermore, bug prone packages don't see wide adoption regardless of the source of said bugs. – Adam Tolley Aug 29 '19 at 19:56
  • 24
    FYI, I believe "gyp" is short for "generate your projects" – Tom Oct 22 '19 at 22:00
  • 1
    @PirateApp I do not understand, can you please explain in other words? Your comment is high in rep, so I want to know it - you do not like `node-gyp` and run away? – Timo May 14 '21 at 18:08
  • node-gyp creates native dependencies and making it work on an external environment like docker or a virtual machine seems like a pain, take bcrypt.js it certainly works much faster if you use the native one but due to it being a pain, i switched to bcryptjs that doesnt require node-gyp – PirateApp May 15 '21 at 04:03
  • Good answer. Still one thing bothers me: why the ealier Java says 'write once run everywhere', but the nodejs still uses C/C++ add-ons? – jiajianrong Mar 03 '22 at 13:17
  • 2
    @jiajianrong - Well, you sort of answered your own question, **_Java_**'s claim was 'write once run everywhere'. **_JavaScript_**, i.e. the "js" in "node.js" never made such a claim. JavaScript was initially designed to add interactivity to a specific browser (i.e. Netscape) but became the defacto language for all _browser_ activity. nodejs brought that language to environments other than browsers, and in doing so, it is clearly going to have to interact with those additional environments. node-gyp is an attempt to provide some cross-environment compatibility. – MetaSean Jun 02 '22 at 23:18