尝试打开端口时Nodejs serialport错误

p8h8hvxi  于 2023-05-06  发布在  Node.js
关注(0)|答案(1)|浏览(350)

以下是环境详细信息:
package.json "serialport": "^11.0.0"
node -v v18.16.0
npm -v 9.5.1
index.js

const {SerialPort} = require('serialport');

const port = new SerialPort({
    path: '/dev/ttyUSB0',
    baudRate: 57600,
});

console(非sudo用户)

$ node index
node:internal/process/promises:288
            triggerUncaughtException(err, true /* fromPromise */);
            ^

Error: Error: Permission denied, cannot open /dev/ttyUSB0
Emitted 'error' event on SerialPort instance at:
    at SerialPort._error (/var/www/node-serialport/node_modules/@serialport/stream/dist/index.js:78:18)
    at /var/www/node-serialport/node_modules/@serialport/stream/dist/index.js:111:18

Node.js v18.16.0

console(with sudo user):

$ sudo node index
/var/www/node-serialport/node_modules/@serialport/stream/dist/index.js:71
        return (this.port?.isOpen ?? false) && !this.closing;
                          ^

SyntaxError: Unexpected token .
    at Module._compile (internal/modules/cjs/loader.js:723:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/var/www/node-serialport/node_modules/serialport/dist/serialport-mock.js:4:18)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)

相关问题