简体   繁体   English

如何在 react js (reactjs + electron) 组件中使用原生 node_modules?

[英]How to use native node_modules in react js (reactjs + electron) components?

I am trying to use native node_module ie "@pokusew/pcsclite" in reactjs application but it is not working .我正在尝试在 reactjs 应用程序中使用本机 node_module 即“@pokusew/pcsclite”,但它不起作用。 Can anyone help in this ?任何人都可以帮忙吗? Below is the error :以下是错误:

Uncaught TypeError: exists is not a function
   at Function.getRoot (bindings.js:202)
   at bindings (bindings.js:82)
   at Object.<anonymous> (pcsclite.js:4)
   at Object../node_modules/pcsclite/lib/pcsclite.js (pcsclite.js:160)
   at __webpack_require__ (bootstrap:784)
   at fn (bootstrap:150)
   at Object../node_modules/pcsclite/index.js (index.js:1)
   at __webpack_require__ (bootstrap:784)
   at fn (bootstrap:150)
   at Module.<anonymous> (App.css?4433:45)
   at Module../src/App.js (App.js:85)
   at __webpack_require__ (bootstrap:784)

And below is the code that i am using in reactjs component where i am importing the pcsclite module in different ways one is by using import and another is by using require function下面是我在 reactjs 组件中使用的代码,我以不同的方式导入 pcsclite 模块,一种是使用 import,另一种是使用 require 函数

import pcsclite from '../node_modules/@pokusew/pcsclite'
var pcsc = pcsclite();
pcsc.on('reader', function(reader) {
    console.log('New reader detected', reader.name);
    reader.on('error', function(err) {
        console.log('Error(', reader.name, '):', err.message);
    });

    reader.on('status', function(status) {
        console.log('Status(', reader.name, '):', status);
        /* check what has changed */
        var changes = reader.state ^ status.state;
        console.log('changes(', changes, '):', status);
        if (changes) {
            if ((changes & reader.SCARD_STATE_EMPTY) && (status.state & reader.SCARD_STATE_EMPTY)) {
                console.log("card removed");/* card removed */
                reader.disconnect(reader.SCARD_LEAVE_CARD, function(err) {
                    if (err) {
                        console.log(err);
                    } else {
                        console.log('Disconnected');
                    }
                });
            } else if ((changes & reader.SCARD_STATE_PRESENT) && (status.state & reader.SCARD_STATE_PRESENT)) {
                console.log("card inserted");/* card inserted */
                reader.connect({ share_mode : reader.SCARD_SHARE_SHARED }, function(err, protocol) {
                    if (err) {
                        console.log(err);
                    } else {
                        console.log('Protocol(', reader.name, '):', protocol);
                        reader.transmit(new Buffer([0x00, 0xB0, 0x00, 0x00, 0x20]), 40, protocol, function(err, data) {
                            if (err) {
                                console.log(err);
                            } else {
                                console.log('Data received', data);
                               // reader.close();
                               // pcsc.close();
                            }
                        });
                    }
                });
            }
        }
    });

I found a solution for my above question.我为我的上述问题找到了解决方案。 i have imported my native node_module in reactjs by window object .我已经通过 window 对象在 reactjs 中导入了我的原生 node_module 。 Below is the solution.下面是解决方案。

const electron = window.require('electron');
const pcsclite = electron.remote.require('@pokusew/pcsclite');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何查看react-native node_modules更改 - How to watch react-native node_modules changes 如何在React Native中使用节点模块 - How to use node modules with React Native 使用Jest测试时,node_modules / react-native / Libraries / Modal / Modal.js中的错误 - Error in node_modules/react-native/Libraries/Modal/Modal.js while testing with Jest 过滤掉由node_modules中的组件引起的React警告 - Filter out React warnings caused by components in node_modules 如何在“传统网站”上使用 node_modules? - How to use node_modules on a "traditional website"? 使用React Native在node_modules中转译es6代码 - Transpiling es6 code in node_modules with React Native 如何修复无法从“node_modules\\react-navigation-stack\\lib\\module\\views\\StackView\\StackViewCard.js”解析“react-native-screens”? - How to fix Unable to resolve "react-native-screens" from "node_modules\react-navigation-stack\lib\module\views\StackView\StackViewCard.js"? 生产 Electron 是否需要 node_modules/ - Does Production Electron need node_modules/ 无法使用 electron 读取 /node_modules/bindings/bindings.js 中未定义的属性“模块”并安装串行端口 - Cannot read property 'modules' of undefined at /node_modules/bindings/bindings.js with electron and install serialport 如何将 node_modules 中的新组件导入到项目中? - How an I import new components from node_modules into the project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM