How to build/uses a native node package with an Atom package?


#1

My goal is to call OS X Launch Services native methods from my Atom package.

To do this I’ve wrapped a small portion of the OS X Launch Services API in a node package here:

I’m sure there are some issues with it, my first native package, but it general seems to work. I can build it and the tests run. I’ve tried to use the same structure as https://github.com/atom/node-spellchecker while also following the nan boilerplate https://github.com/fcanas/node-native-boilerplate.

Now that I have this proof of concept working, I’m not sure what the next step is. I just tried installing this package into my Atom package like this:

npm install FoldingText/launch-services and get an error “Error: Cannot find module ‘nan’”.

Could someone give me an overview of how to use native models from Atom packages. I’ve seen https://github.com/atom/electron/blob/master/docs/tutorial/using-native-node-modules.md but I think I need a higher level overview first. In particular I’m not sure where the compiling is supposed to happen (at release time or at install time?) and in general I’d love any tips on how to manage native models that you can provide.

Thanks,
Jesse


#2

I got things working by moving nan from a “devDependencies” to “dependencies”. Now when I do npm install FoldingText/launch-services the module installs and builds. At that point if I try to load my package it fails (built with wrong node I think), but if I delete node_modules and then do apm install my module loads and I can call the native extensions. Yeah!

Next question…

It seems that when I include a native module in my package then the native parts are built on the end users machine when they install my package. Will this always work? Does haven’t node and Atom installed also mean that the user has a compiler available to do the work of compiling the native modules? Or will there be cases where to use my Atom package they first need to go install a compiler?

Thanks,
Jesse


#3

Hmm, AFAIK native modules used by Atom are included in the bundled versions, so users don’t need any tools to have it working. But for packages, that’s another story, that’s why I dropped node-oniguruma in atom-color-highlight cause there was too much issues with nodegyp (and I’m not really familiar with it).


#4

@abe To bad, but thanks for the tip.


#5

@abe Do you remember if you ever tried node-pre-gyp? It tries to solve the problem by building the binaries ahead of time, then users of the module only need download the precompiled binaries.


#6

Nope, I didn’t even know about that ^^’. Isn’t that something you need to run in the package that actually defines the native modules?
As I said I’m not really familiar with node-gyp, and all I was doing was using node-oniguruma as dependency.


#7

Yeah you are right. I forgot you were using a third party package, I’m trying to create my own.