电子+反应(Webpack)中使用的Node Serialport错误

时间:2018-09-19 09:25:36

标签: javascript node.js webpack electron node-serialport

我正在尝试使用电子构建一个跨平台的应用程序并进行反应。该应用程序将用于使用Node Serialport与串行设备进行通信。并在开始时告诉您:我是node.js的新手,包括电子和react.js)

我遵循了各种指南,以重建困难的电子节点串行端口。最后,我确定可以正确重建它。

当我使用Patryk at Stackoverflow中的解决方法并放入

<script>
  const sp = require('serialport');
  var sPort = sp('/dev/ttyACM0', {baudRate: 115200})
  sPort.write(new Uint8Array([0xff, 0x41, 0x01, 0x00, 0x42, 0xee]))
</script>

进入index.hmtl,我可以与我的串行设备通信。但是我不知道如何在其他.js文件中使用串行连接,例如App.js

当我放

var SerialPort = require('serialport');

var SerialPort = require( "electron" ).remote.require( "serialport" );

进入我的.js文件中,出现以下错误/警告:

来自npm run dev的控制台输出

09:48:02 react.1      |  Compiling...
09:48:04 react.1      |  Compiled with warnings.
09:48:04 react.1      |  ./src/App.js
09:48:04 react.1      |    Line 7:  'SerialPort' is assigned a value but never used  no-unused-vars
09:48:04 react.1      |  ./node_modules/bindings/bindings.js
09:48:04 react.1      |  81:22-40 Critical dependency: the request of a dependency is an expression
09:48:04 react.1      |  ./node_modules/bindings/bindings.js
09:48:04 react.1      |  81:43-53 Critical dependency: the request of a dependency is an expression
09:48:04 react.1      |  Search for the keywords to learn more about each warning.
09:48:04 react.1      |  To ignore, add // eslint-disable-next-line to the line before.

电子窗口中的错误消息:

TypeError: exists is not a function
Function.getRoot
node_modules/bindings/bindings.js:158
  155 |   // Avoids an infinite loop in rare cases, like the REPL
  156 |   dir = process.cwd()
  157 | }
> 158 | if (exists(join(dir, 'package.json')) || exists(join(dir, 'node_modules'))) {
  159 |   // Found the 'package.json' file or 'node_modules' dir; we're done
  160 |   return dir
  161 | }
View compiled
bindings
node_modules/bindings/bindings.js:60
  57 | 
  58 | // Get the module root
  59 | if (!opts.module_root) {
> 60 |   opts.module_root = exports.getRoot(exports.getFileName())
  61 | }
  62 | 
  63 | // Ensure the given bindings name ends with .node
View compiled
./node_modules/@serialport/bindings/lib/linux.js
node_modules/@serialport/bindings/lib/linux.js:1
> 1 | const binding = require('bindings')('bindings.node')
  2 | const AbstractBinding = require('@serialport/binding-abstract')
  3 | const linuxList = require('./linux-list')
  4 | const Poller = require('./poller')
View compiled
__webpack_require__
/home/bas/Documents/Projects/MicroGator/Code/GatorRemoteElectron/webpack/bootstrap 89408b2d7db2f964ac18:678
  675 | };
  676 | 
  677 | // Execute the module function
> 678 | modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId));
  679 | 
  680 | // Flag the module as loaded
  681 | module.l = true;
View compiled
fn
/home/bas/Documents/Projects/MicroGator/Code/GatorRemoteElectron/webpack/bootstrap 89408b2d7db2f964ac18:88
  85 |      console.warn("[HMR] unexpected require(" + request + ") from disposed module " + moduleId);
  86 |      hotCurrentParents = [];
  87 |  }
> 88 |  return __webpack_require__(request);
  89 | };
  90 | var ObjectFactory = function ObjectFactory(name) {
  91 |  return {
View compiled
(anonymous function)
node_modules/@serialport/bindings/lib/index.js:14
  11 |     break
  12 |   default:
  13 |     debug('loading LinuxBinding')
> 14 |     module.exports = require('./linux')
  15 | }
  16 | 
  17 | 
View compiled
./node_modules/@serialport/bindings/lib/index.js
http://localhost:5000/static/js/bundle.js:18558:30
__webpack_require__
/home/bas/Documents/Projects/MicroGator/Code/GatorRemoteElectron/webpack/bootstrap 89408b2d7db2f964ac18:678
  675 | };
  676 | 
  677 | // Execute the module function
> 678 | modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId));
  679 | 
  680 | // Flag the module as loaded
  681 | module.l = true;
View compiled
fn
/home/bas/Documents/Projects/MicroGator/Code/GatorRemoteElectron/webpack/bootstrap 89408b2d7db2f964ac18:88
  85 |      console.warn("[HMR] unexpected require(" + request + ") from disposed module " + moduleId);
  86 |      hotCurrentParents = [];
  87 |  }
> 88 |  return __webpack_require__(request);
  89 | };
  90 | var ObjectFactory = function ObjectFactory(name) {
  91 |  return {
View compiled
./node_modules/serialport/lib/index.js
node_modules/serialport/lib/index.js:2
  1 | const SerialPort = require('@serialport/stream')
> 2 | const Binding = require('@serialport/bindings')
  3 | const parsers = require('./parsers')
  4 | 
  5 | /**
View compiled
__webpack_require__
/home/bas/Documents/Projects/MicroGator/Code/GatorRemoteElectron/webpack/bootstrap 89408b2d7db2f964ac18:678
  675 | };
  676 | 
  677 | // Execute the module function
> 678 | modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId));
  679 | 
  680 | // Flag the module as loaded
  681 | module.l = true;
View compiled
fn
/home/bas/Documents/Projects/MicroGator/Code/GatorRemoteElectron/webpack/bootstrap 89408b2d7db2f964ac18:88
  85 |      console.warn("[HMR] unexpected require(" + request + ") from disposed module " + moduleId);
  86 |      hotCurrentParents = [];
  87 |  }
> 88 |  return __webpack_require__(request);
  89 | };
  90 | var ObjectFactory = function ObjectFactory(name) {
  91 |  return {
View compiled
./src/App.js
src/App.js:7
  4 | 
  5 | import 'typeface-roboto'
  6 | 
>  7 | var SerialPort = require('serialport');;
  8 | 
  9 | class App extends Component {
  10 |   render() {

我的App.js看起来像这样:

import React, { Component } from 'react';
import TargetTabs from './components/TargetTabs'
import './App.css';

import 'typeface-roboto'

var SerialPort = require('serialport');

class App extends Component {
  render() {
    return (
    <div>
      <TargetTabs />
    </div>
    );
  }
}

export default App;

因此,如果您能为我指明正确的方向,以便使用我的其他index.html文件中js中脚本块中的变量或解决警告/错误,以便我可以使用标准要求,我将不胜感激。

也许甚至都...:)

0 个答案:

没有答案