Home > Back-end >  node.js createserver module not found
node.js createserver module not found

Time:12-24

I am attempting to set up a basic web server but I am only just learning and I couldn't seem to find any answers to this kind of issue. I just get this error which I do not understand at all. This is the code:

const net = require('net');

const server = net.createserver((socket) => { 
    socket.on("data", (buffer) => {
        const requestString = buffer.toString('utf-8')

        console.log(requestString);
    })
})

server.listen(8000, () => console.log("Listening"))

Every time I enter node webserver.js into the terminal of visual studio I get the following error:

Error: Cannot find module 'C:\Users\callu\OneDrive\Documents\Computer Science Project Code\webserver.js'
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

The path for the file is as follows: C:\Users\callu\OneDrive\Documents\Computer Science Project Code\Server Side\webserver.js There are no other files in server side. I have attempted to repair node, then reinstalled node. I have also checked that there is a system variable with the correct path. I did node = C:\Program Files\nodejs If any more information is required to help with a solution I will provide all I can, thank you for your time - Salty.

OS: Windows 10 Home OS version: 20H2

NODE version: 16.13.1

Visual Studio Code version: 1.63.2

CodePudding user response:

You are running node webserver.js in your root project folder

 C:\Users\callu\OneDrive\Documents\Computer Science Project Code\

but you should do it in a nested server side folder

 C:\Users\callu\OneDrive\Documents\Computer Science Project Code\Server Side\

or provide a path to webserver.js file: node ".\Server Side\webserver.js"

  • Related