How to require a wrapper C++ module like addon.node in atom packages or electorn?


#1

I wrap some C++ functions into a addon.node file by nan. It worked fine in the native nodejs platform. The wrapped C++ functions are official examples in nan
But when I write "require ‘./addon.node’ " in my atom package file, the atom goes wrong.
The bug infoformation is:
Atom Version: 1.9.8.
System: win 10 professional 64.

Stack Trace

Failed to load the hello package

At A dynamic link library (DLL) initialization routine failed.
?\C:\Users\lishe\github\hello\lib\addon.node

Error: A dynamic link library (DLL) initialization routine failed.
?\C:\Users\lishe\github\hello\lib\addon.node
at Error (native)
at process.module.(anonymous function) [as dlopen] (ELECTRON_ASAR.js:158:20)
at Object.Module._extensions…node (module.js:440:18)
at Object.module.(anonymous function) [as .node] (ELECTRON_ASAR.js:158:20)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (C:\Users\lishe\AppData\Local\atom\app-1.9.8\resources\app.asar\src\native-compile-cache.js:50:27)
at Object. (file:///C:/Users/lishe/github/hello/lib/hello.coffee:3:13)
at Object. (file:///C:/Users/lishe/github/hello/lib/hello.coffee:1:1)
at Module._compile (C:\Users\lishe\AppData\Local\atom\app-1.9.8\resources\app.asar\src\native-compile-cache.js:103:30)
at Object.defineProperty.value [as .coffee] (C:\Users\lishe\AppData\Local\atom\app-1.9.8\resources\app.asar\src\compile-cache.js:208:21)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (C:\Users\lishe\AppData\Local\atom\app-1.9.8\resources\app.asar\src\native-compile-cache.js:50:27)
at Package.module.exports.Package.requireMainModule (C:\Users\lishe\AppData\Local\atom\app-1.9.8\resources\app.asar\src\package.js:715:27)
at C:\Users\lishe\AppData\Local\atom\app-1.9.8\resources\app.asar\src\package.js:117:28
at Package.module.exports.Package.measure (C:\Users\lishe\AppData\Local\atom\app-1.9.8\resources\app.asar\src\package.js:92:15)
at Package.module.exports.Package.load (C:\Users\lishe\AppData\Local\atom\app-1.9.8\resources\app.asar\src\package.js:106:12)
at PackageManager.module.exports.PackageManager.loadPackage (C:\Users\lishe\AppData\Local\atom\app-1.9.8\resources\app.asar\src\package-manager.js:457:14)
at C:\Users\lishe\AppData\Local\atom\app-1.9.8\resources\app.asar\src\package-manager.js:402:19
at Config.module.exports.Config.transact (C:\Users\lishe\AppData\Local\atom\app-1.9.8\resources\app.asar\src\config.js:312:16)
at PackageManager.module.exports.PackageManager.loadPackages (C:\Users\lishe\AppData\Local\atom\app-1.9.8\resources\app.asar\src\package-manager.js:397:19)
at C:\Users\lishe\AppData\Local\atom\app-1.9.8\resources\app.asar\src\atom-environment.js:749:28

I’ve tried some other wrapped node files , they worked fine on the native platform but all of them crashed in the atom. I don’t understand this error, now that atom is based on the nodejs platform.

On Electron

I also tried in electorn script, I write “require(’./addon.node’)” in a simple hello app on electron . It also crashed .

Is there someone met this problem before ?


#2

If you want to access native code from Atom or Electron, you have to write a native Node module and then use the Node module the way you would any other. If you want more information on native Node modules in Electron (and therefore Atom) you can find it in the Electron documentation:

http://electron.atom.io/docs/tutorial/using-native-node-modules/


#3

Thanks. I’ll try it.