Electron-react-boilerplate: How to use relative paths with node-loader and native addon

Created on 29 Apr 2018  路  3Comments  路  Source: electron-react-boilerplate/electron-react-boilerplate

I have a native addon I am using that works great on my dev machine but fails on any other machine due to the webpack build using an absolute path to the native module instead of a relative one. Here is the error I get:

/main.prod.js:7543: Uncaught Error: Cannot open /Users/.../app/lib/main.node: Error: dlopen(/Users/.../app/lib/main.node, 1): image not found

In my main.dev.js I import the file like this: import main from './lib/main.node';
In webpack config I have added a module test for .node:
```export default {
externals: Object.keys(externals || {}),

module: {
rules: [{
test: /.jsx?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
cacheDirectory: true
}
}
}, {
test: /.node$/,
use: 'node-loader'
}]
},
```

How can I make sure that my main.node file gets packaged for the build and imported via relative path?

I took a look at the following issue and tried moving node-loader from ./package.json to ./app/package.json but that of course did not solve things :D
https://github.com/chentsulin/electron-react-boilerplate/issues/1323

Most helpful comment

Hope this helps someone in the future.
I was able to resolve my issues by switching to this loader: https://github.com/smt116/node-native-ext-loader

All 3 comments

I have tried using https://github.com/ushu/node-addon-loader, but I can't seem to get it to work. After packaging the app, I unpack the app.asar and I see <hex>.node is in there. However when I run the packaged app file, I get the following error:

.../Contents/Resources/app.asar/main.prod.js:7530: Uncaught Error: Cannot open <hex>.node: Error: dlopen(<hex>.node, 1): image not found

I'm using this rule, though if I remove the options param entirely, I get the same error.

{
      test: /\.node$/,
      use: {
        loader: 'node-addon-loader',
        options: {
          basePath: path.resolve(__dirname),
          rewritePath: './'
        }
      }
    }

Hope this helps someone in the future.
I was able to resolve my issues by switching to this loader: https://github.com/smt116/node-native-ext-loader

I setup like this, which can work, but i don't know why.
if (process.env.NODE_ENV === 'production') { config.module .rule('node') .test(/\.node$/) .use('native-ext-loader') .loader('native-ext-loader') .options({ rewritePath: process.resourcesPath }) .end() }else{ config.module .rule('node') .test(/\.node$/) .use('native-ext-loader') .loader('native-ext-loader') .options({ rewritePath: path.resolve(__dirname,"dist_electron") }) .end() }

Was this page helpful?
0 / 5 - 0 ratings