question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Failed to Parse Native Modules

See original GitHub issue

I have a very simple vue CLI 3 based sample project which I added vue-cli-plugin-electron builder to. Everything starts up correctly. Then I proceeded to add a simple nAPI native module which I compiled fine. I used the instructions here.

I added the following to a simple background.js

const testAddon = require('./testaddon.node');
module.exports = testAddon;

If I run

npm run electron:serve

I get

/ Bundling main process…

ERROR Failed to compile with 1 errors 10:14:03 AM

error in ./src/testaddon.node

Module parse failed: Unexpected character ‘�’ (1:2) You may need an appropriate loader to handle this file type. (Source code omitted for this binary file)

@ ./src/background.js 11:18-45 @ multi ./src/background.js

ERROR Build failed with errors. npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! elec3@0.1.0 electron:serve: vue-cli-service electron:serve npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the elec3@0.1.0 electron:serve script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:

I have tried a variety of things including adding a vue.config.js as mentioned in your documentation to add it as an external.

module.exports = { pluginOptions: { electronBuilder: { // List native deps here if they don’t work externals: [‘testaddon.node’], // If you are using Yarn Workspaces, you may have multiple node_modules folders // List them all here so that VCP Electron Builder can find them nodeModulesPath: [‘…/…/node_modules’, ‘./node_modules’] } } }

Can you please let me know what configuration I am missing? It looks like from what I could tell webpack trying to do something with the binary C++ dll and failing. But from what I have read in your documentation it appears that this native module support should work. Any points would be helpful.

  • Windows 10 64 bit
  • node version: 12.6.0
  • npm version: 6.9.0
  • vue-cli-plugin-electron-builder version : 1.3.6
  • electron version: 5.0.0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
madhankkcommented, Jul 8, 2019

For people who have this same problem, what did work to make the .node file load was using this:

var myaddon = __non_webpack_require__('testaddon.node');

But I am not sure if this is standard and there is any other more ‘correct’ approach. Thanks for all the help.

1reaction
nklaymancommented, Jul 8, 2019

Try adding node-loader to the main process webpack config by setting your vue.config.js to:

module.exports = {
  pluginOptions: {
    electronBuilder: {
      chainWebpackMainProcess: config => {
        config.module.rule('node').test(/\.node$/).use('node-loader')
      },
    }
  }
}

The externals option is designed for native npm modules, not .node files, that is causing the error.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Module parse failed in native - reactjs
Your web pack config should look something like this to read .jsx files: module.exports = { entry: './app/assets/frontend/myFile.jsx', ...
Read more >
Android Native Modules
Please start by reading the Native Modules Intro for an intro to what ... promise.reject("Create Event error", "Error parsing date", e);.
Read more >
Errors | Node.js v19.3.0 Documentation
AssertionError s are a special class of error that can be triggered when Node.js detects an exceptional logic violation that should never occur....
Read more >
SyntaxError: JSON.parse: bad parsing - JavaScript | MDN
The JavaScript exceptions thrown by JSON.parse() occur when string failed to be parsed as JSON. Message. SyntaxError: JSON.parse: unterminated string literal ...
Read more >
Troubleshooting
Sometimes the error may look like this: Error: While trying to resolve module "@react-navigation/native" from file "/path/to/src/App.js", the package ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found