如何在Node上安装串口模块?

How to install serial port module on Node?

我在 Node 上安装串行模块时遇到问题。我正在为橙色 pi 零 H2 使用 Raspbian 服务器。

root@OrangePI:/home/node# npm install serialport

return

npm WARN package.json one@1.0.0 No description
npm WARN package.json one@1.0.0 No repository field.
npm WARN package.json one@1.0.0 No README data
npm WARN engine serialport@8.0.6: wanted: {"node":">=8.6.0"} (current:     {"node":"4.9.1","npm":"2.15.11"})
npm WARN engine @serialport/parser-ready@8.0.6: wanted: {"node":">=8.6.0"} (current: {"node":"4.9.1","npm":"2.15.11"})
npm WARN engine @serialport/binding-mock@8.0.6: wanted: {"node":">=8.6.0"} (current: {"node":"4.9.1","npm":"2.15.11"})
npm WARN engine @serialport/parser-byte-length@8.0.6: wanted: {"node":">=8.6.0"} (current: {"node":"4.9.1","npm":"2.15.11"})
npm WARN engine @serialport/stream@8.0.6: wanted: {"node":">=8.6.0"} (current: {"node":"4.9.1","npm":"2.15.11"})
npm WARN engine @serialport/bindings@8.0.6: wanted: {"node":">=8.6.0"} (current: {"node":"4.9.1","npm":"2.15.11"})
npm WARN engine @serialport/parser-readline@8.0.6: wanted: {"node":">=8.6.0"} (current: {"node":"4.9.1","npm":"2.15.11"})
npm WARN engine @serialport/parser-regex@8.0.6: wanted: {"node":">=8.6.0"} (current: {"node":"4.9.1","npm":"2.15.11"})
npm WARN engine @serialport/parser-delimiter@8.0.6: wanted: {"node":">=8.6.0"} (current: {"node":"4.9.1","npm":"2.15.11"})
npm WARN engine @serialport/parser-cctalk@8.0.6: wanted: {"node":">=8.6.0"} (current: {"node":"4.9.1","npm":"2.15.11"})
npm WARN engine @serialport/binding-abstract@8.0.6: wanted: {"node":">=8.6.0"} (current: {"node":"4.9.1","npm":"2.15.11"})
npm WARN engine @serialport/binding-abstract@8.0.6: wanted: {"node":">=8.6.0"} (current: {"node":"4.9.1","npm":"2.15.11"})
npm WARN engine prebuild-install@5.3.3: wanted: {"node":">=6"} (current: {"node":"4.9.1","npm":"2.15.11"})
npm WARN engine expand-template@2.0.3: wanted: {"node":">=6"} (current: {"node":"4.9.1","npm":"2.15.11"})
npm WARN engine decompress-response@4.2.1: wanted: {"node":">=8"} (current: {"node":"4.9.1","npm":"2.15.11"})
npm WARN engine mimic-response@2.0.0: wanted: {"node":">=8"} (current: {"node":"4.9.1","npm":"2.15.11"})
npm WARN engine readable-stream@3.4.0: wanted: {"node":">= 6"} (current: {"node":"4.9.1","npm":"2.15.11"})

...

> @serialport/bindings@8.0.6 install /home/node/node_modules/serialport/node_modules/@serialport/bindings > prebuild-install --tag-prefix @serialport/bindings@ || node-gyp rebuild

/home/node/node_modules/serialport/node_modules/@serialport/bindings/node_modules/prebuild-install/node_modules/simple-get/index.js:18
const { hostname, port, protocol, auth, path } = url.parse(opts.url) // eslint-disable-line node/no-deprecated-api
      ^
SyntaxError: Unexpected token {
     at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/home/node/node_modules/serialport/node_modules/@serialport/bindings/node_modules/prebuild-install/download.js:3:11)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)

我对linux经验不多……但这是我的印象还是声明的常量真的没有命名?

make: Entering directory 
'/home/node/node_modules/serialport/node_modules/@serialport/bindings/build'
    CXX(target) Release/obj.target/bindings/src/serialport.o
make: g++: Command not found
bindings.target.mk:97: recipe for target 'Release/obj.target/bindings/src/serialport.o' failed
make: *** [Release/obj.target/bindings/src/serialport.o] Error 127
make: Leaving directory '/home/node/node_modules/serialport/node_modules/@serialport/bindings/build'
gyp ERR! build error
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/opt/node/lib/node_modules/npm/node_modules/node-        gyp/lib/build.js:276:23)
gyp ERR! stack     at emitTwo (events.js:87:13)
gyp ERR! stack     at ChildProcess.emit (events.js:172:7)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:211:12)
gyp ERR! System Linux 3.4.39
gyp ERR! command "/opt/node/bin/node" "/opt/node/lib/node_modules/npm/node_modules/node-    gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /home/node/node_modules/serialport/node_modules/@serialport/bindings
gyp ERR! node -v v4.9.1
gyp ERR! node-gyp -v v3.4.0
gyp ERR! not ok
npm ERR! Linux 3.4.39
npm ERR! argv "/opt/node/bin/node" "/opt/node/bin/npm" "install" "serialport"
npm ERR! node v4.9.1
npm ERR! npm  v2.15.11
npm ERR! code ELIFECYCLE

npm ERR! @serialport/bindings@8.0.6 install: `prebuild-install --tag-prefix @serialport/bindings@ ||     node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the @serialport/bindings@8.0.6 install script 'prebuild-install --tag-prefix     @serialport/bindings@ || node-gyp rebuild'.
npm ERR! This is most likely a problem with the @serialport/bindings package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     prebuild-install --tag-prefix @serialport/bindings@ || node-gyp rebuild
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs @serialport/bindings
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!
npm ERR!     npm owner ls @serialport/bindings
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /home/node/npm-debug.log

日志文件的最后一部分

5807 info install prebuild-install@5.3.3
5808 info postinstall prebuild-install@5.3.3
5809 verbose unlock done using /root/.npm/_locks/prebuild-install-da7fa1c0b3187458.lock for /home/node/node_modules/serialport/node_modules/@serialport/bindings/node_modules/prebuild-install
5810 verbose about to build /home/node/node_modules/serialport/node_modules/@serialport/bindings
5811 info build /home/node/node_modules/serialport/node_modules/@serialport/bindings
5812 info linkStuff @serialport/bindings@8.0.6
5813 silly linkStuff @serialport/bindings@8.0.6 has /home/node/node_modules/serialport/node_modules     as its parent node_modules
5814 verbose linkBins @serialport/bindings@8.0.6
5815 verbose linkMans @serialport/bindings@8.0.6
5816 verbose rebuildBundles @serialport/bindings@8.0.6
5817 verbose rebuildBundles [ '.bin', '@serialport', 'bindings', 'nan', 'prebuild-install' ]
5818 info install @serialport/bindings@8.0.6
5819 verbose unsafe-perm in lifecycle true
5820 info @serialport/bindings@8.0.6 Failed to exec install script
5821 verbose unlock done using /root/.npm/_locks/serialport-bindings-291f31ff80e6691d.lock for     /home/node/node_modules/serialport/node_modules/@serialport/bindings
5822 verbose about to build /home/node/node_modules/serialport
5823 verbose unlock done using /root/.npm/_locks/serialport-a4250c37e3372508.lock for     /home/node/node_modules/serialport
5824 verbose stack Error: @serialport/bindings@8.0.6 install: `prebuild-install --tag-prefix     @serialport/bindings@ || node-gyp rebuild`
5824 verbose stack Exit status 1
5824 verbose stack     at EventEmitter.<anonymous> (/opt/node/lib/node_modules/npm/lib/utils/lifecycle.js:217:16)
5824 verbose stack     at emitTwo (events.js:87:13)
5824 verbose stack     at EventEmitter.emit (events.js:172:7)
5824 verbose stack     at ChildProcess.<anonymous>     (/opt/node/lib/node_modules/npm/lib/utils/spawn.js:24:14)
5824 verbose stack     at emitTwo (events.js:87:13)
5824 verbose stack     at ChildProcess.emit (events.js:172:7)
5824 verbose stack     at maybeClose (internal/child_process.js:862:16)
5824 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:222:5)
5825 verbose pkgid @serialport/bindings@8.0.6
5826 verbose cwd /home/node
5827 error Linux 3.4.39
5828 error argv "/opt/node/bin/node" "/opt/node/bin/npm" "install" "--unsafe-perm" "serialport"
5829 error node v4.9.1
5830 error npm  v2.15.11
5831 error code ELIFECYCLE
5832 error @serialport/bindings@8.0.6 install: `prebuild-install --tag-prefix @serialport/bindings@     || node-gyp rebuild`
5832 error Exit status 1
5833 error Failed at the @serialport/bindings@8.0.6 install script 'prebuild-install --tag-prefix     @serialport/bindings@ || node-gyp rebuild'.
5833 error This is most likely a problem with the @serialport/bindings package,
5833 error not with npm itself.
5833 error Tell the author that this fails on your system:
5833 error     prebuild-install --tag-prefix @serialport/bindings@ || node-gyp rebuild
5833 error You can get information on how to open an issue for this project with:
5833 error     npm bugs @serialport/bindings
5833 error Or if that isn't available, you can get their info via:
5833 error
5833 error     npm owner ls @serialport/bindings
5833 error There is likely additional logging output above.
5834 verbose exit [ 1, true ]

这个版本默认没有安装make, 欢迎提出任何建议,感谢您的宝贵时间。

更新你的 nodejs

npm WARN engine serialport@8.0.6: wanted: {"node":">=8.6.0"} (current: {"node":"4.9.1","npm":"2.15.11"})

您当前的版本不支持 ES6 语法

const { hostname, port, protocol, auth, path } = url.parse(opts.url)
      ^
SyntaxError: Unexpected token {

我遇到了类似的问题。我的解决方案:我更具体地了解了我使用的是哪个版本的 nodejs。我有最新版本(在撰写本文时)13.8,我遇到了与 OP 相同的问题。

所以我安装了 nvm 并开始使用它来选择我的 nodejs。

$ nvm install --lts 
$ nvm use --lts
Now using node v12.16.0

当前的长期支持版本(在撰写本文时)是 12.6。我为应用程序重做了 npm install,之后我的串口就没有问题了。从理论上讲,我本可以一直使用 nvm 安装不同版本的 nodejs,直到我得到一个有效的版本。