Home > Mobile >  vscode npm nodejs: cannot find module 'serialport' error
vscode npm nodejs: cannot find module 'serialport' error

Time:12-12

I started using vscode (on windows10) a few months ago to develop nodejs server rest services and everything ran smoothly. Later, I started to use vscode also for my ESP32 C program using PlatformIO and I'm very pleased I did. The problem is that now that I returned to my nodejs program, I run into errors, mainly because nodejs cannot find the 'serialport' module. 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 ???). The /server/node_modules directory contains a directory '@serialport'. The 'index.js' program starts running but I get the following error. 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:

{
  "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"
  }
}

CodePudding user response:

You haven't installed the package because it isn't listed in the package.json. Run this to install serial port with the cmd console in your project directory

npm i serialport

CodePudding user response:

The answer is in the manual of course:

  • Local install (default): puts stuff in ./node_modules of the current package root.
  • Global install (with -g): puts stuff in /usr/local or wherever node is installed.
  • Install it locally if you're going to require() it.
  • 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.
  • Related