Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require in non-babel env needs .default suffix #12

Closed
Khasanboy opened this issue Apr 6, 2017 · 3 comments
Closed

Require in non-babel env needs .default suffix #12

Khasanboy opened this issue Apr 6, 2017 · 3 comments

Comments

@Khasanboy
Copy link

I am trying to create application using Electron. In the beginning I was getting problem with node Module versions that was expecting 51 but required 53. I installed Electron 1.5.1 and that error is fixed but now I am getting error that says:

TypeError: NFC is not a constructor

I just used your example and error is coming from this line:

const nfc = new NFC();

did I miss something or is it about node native modules ?

Thanks in advance

@pokusew
Copy link
Owner

pokusew commented Apr 6, 2017

Hi @Khasanboy,

I addressed the problem. It is caused by Babel export default transform behaviour (see http:https://stackoverflow.com/a/33705077).

If you use Babel transpiler (with ES6 modules support) for you app code too, you can import it like import NFC from 'nfc-pcsc'.

Otherwise you must use const NFC = require('nfc-pcsc').default. (add .default).

I know, that's not very intuitive.

Thank you very much for drawing my attention to this problem. I'll prepare a solution (maybe using babel-plugin-add-module-exports and add info to docs.


To this problem:

I am trying to create application using Electron. In the beginning I was getting problem with node Module versions that was expecting 51 but required 53. I installed Electron 1.5.1 and that error is fixed...

Node Native modules must be built with the correct version of Node.js, in which they will be used.

So, if you install this library with npm, it is built with your machine's Node.js version. But then, you are using the library in Electron and Electron uses its own version of Node.js internally.

So after installing a library with Node Native Module, you must rebuild the source with correct Electron headers.
(for example you can run this node-gyp command: cd node_modules/nfc-pcsc && node-gyp rebuild --target=[electronVersion] --arch=[arch] --dist-url=https://atom.io/download/atom-shell)

The usage with Electron is described in README FAQs section Can I use this library in my Electron app?.
There is also a link to Using Native Node Modules guide in Electron documentation to fully understand the problematic.


Hope it helps.

@Khasanboy
Copy link
Author

Thanks, This fixed the problem

@pokusew pokusew modified the milestone: 0.6.0 May 28, 2017
@pokusew pokusew changed the title NFC is not a constructor Require in non-babel env needs .default suffix May 28, 2017
pokusew added a commit that referenced this issue May 28, 2017
Remove unused babel presets
Add .travis.yml
BC: Remove default export and move exports to index.js (see #12)
@pokusew
Copy link
Owner

pokusew commented Jun 2, 2017

Solved, but introduced a breaking change, see How do I require/import this library? in README.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants