[英]vscode npm nodejs: cannot find module 'serialport' error
I started using vscode (on windows10) a few months ago to develop nodejs server rest services and everything ran smoothly.几个月前我开始使用vscode(在windows10上)开发nodejs服务器rest服务,一切运行顺利。 Later, I started to use vscode also for my ESP32 C++ program using PlatformIO and I'm very pleased I did.
后来,我也开始在我的 ESP32 C++ 程序中使用 vscode,我很高兴我做到了。 The problem is that now that I returned to my nodejs program, I run into errors, mainly because nodejs cannot find the 'serialport' module.
问题是,现在我回到我的 nodejs 程序,我遇到了错误,主要是因为 nodejs 找不到'serialport'模块。 I suspect the problem has to do with the paths and the particular way the 'serialport' module has been implemented (as a C++ source code, as I read somewhere???).
我怀疑问题与路径和实现“串行端口”模块的特定方式有关(作为 C++ 源代码,正如我在某处读到的???)。 The /server/node_modules directory contains a directory '@serialport'.
/server/node_modules 目录包含一个目录“@serialport”。 The 'index.js' program starts running but I get the following error.
“index.js”程序开始运行,但出现以下错误。 Please help.
请帮忙。
console.log("server running");
var serialPORT = 'COM10'; // WINDOWS 10 VERSION
//var serialPORT = '/dev/ttyS0'; // LINUX SERVER
var serialport = require("serialport");
console.log("serialport module loaded"); // NEVER REACHES HERE
var port = new serialport(serialPORT, { baudRate:38400, }, (err)=>{
if(err) console.log("Serialport Error, line #11", err.message);
else console.log("\r\n\nserial port /dev/serial0 on pins #8: TXD, #10 RXD is open\r\n\n");
return;
}
);
const Readline = serialport.parsers.Readline;
const parser = new Readline();
port.pipe(parser);
parser.on('data', onData);
// REST OF SERVER ...
The printout and error are as follows:打印输出和错误如下:
server running
internal/modules/cjs/loader.js:883
throw err;
^
Error: Cannot find module 'serialport'
Require stack:
- C:\Users\Panos\Documents\node_js projects\server\index.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
at Function.Module._load (internal/modules/cjs/loader.js:725:27)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (C:\Users\Panos\Documents\node_js projects\server\index.js:8:18)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'C:\\Users\\Panos\\Documents\\node_js projects\\server\\index.js'
]
}
The package.json is as follows: package.json如下:
{
"name": "server",
"version": "1.0.0",
"description": "node.js version of esp32 web server running on raspberry pi 4",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "panos",
"license": "ISC",
"dependencies": {
"url": "^0.11.0"
}
}
You haven't installed the package because it isn't listed in the package.json.您尚未安装 package,因为它未在 package.json 中列出。 Run this to install serial port with the cmd console in your project directory
运行此命令以在项目目录中使用 cmd 控制台安装串行端口
npm i serialport
The answer is in the manual of course:答案当然在手册中:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.