简体   繁体   English

vscode npm nodejs:找不到模块'serialport'错误

[英]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:答案当然在手册中:

  • Local install (default): puts stuff in./node_modules of the current package root.本地安装(默认):将东西放入当前 package 根目录的 ./node_modules 中。
  • Global install (with -g): puts stuff in /usr/local or wherever node is installed.全局安装(使用 -g):将内容放在 /usr/local 或安装节点的任何位置。
  • Install it locally if you're going to require() it.如果您要 require() 它,请在本地安装它。
  • Install it globally if you're going to run it on the command line.如果要在命令行上运行它,请全局安装它。
  • If you need both, then install it in both places, or use npm link.如果两者都需要,则在两个地方都安装,或者使用 npm 链接。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM