Module not found in Node.js Express

Having trouble locating a module in Node.js Express

Sample code provided below

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

Error encountered in the command prompt

C:\Users\user\Desktop\project>npm express -v

7.5.3

C:\Users\user\Desktop\project>npm utils -v

7.5.3

C:\Users\user\Desktop\project>node server

node:internal/modules/cjs/loader:928
  throw err;
  ^

Error: Unable to locate module './utils'
Require stack:
- C:\Users\user\Desktop\project\node_modules\qs\lib\stringify.js
- C:\Users\user\Desktop\project\node_modules\qs\lib\index.js
- C:\Users\user\Desktop\project\node_modules\express\lib\middleware\query.js
- C:\Users\user\Desktop\project\node_modules\express\lib\application.js
- C:\Users\user\Desktop\project\node_modules\express\lib\express.js
- C:\Users\user\Desktop\project\node_modules\express\index.js
- C:\Users\user\Desktop\project\server.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:925:15)
    at Function.Module._load (node:internal/modules/cjs/loader:769:27)
    at Module.require (node:internal/modules/cjs/loader:997:19)
    at require (node:internal/modules/cjs/helpers:92:18)
    at Object.<anonymous> (C:\Users\user\Desktop\project\node_modules\qs\lib\stringify.js:3:13)
    at Module._compile (node:internal/modules/cjs/loader:1108:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
    at Module.load (node:internal/modules/cjs/loader:973:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Module.require (node:internal/modules/cjs/loader:997:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'C:\\Users\\user\\Desktop\\project\\node_modules\\qs\\lib\\stringify.js',
    'C:\\Users\\user\\Desktop\\project\\node_modules\\qs\\lib\\index.js',
    'C:\\Users\\user\\Desktop\\project\\node_modules\\express\\lib\\middleware\\query.js',
    'C:\\Users\\user\\Desktop\\project\\node_modules\\express\\lib\\application.js',
    'C:\\Users\\user\\Desktop\\project\\node_modules\\express\\lib\\express.js',
    'C:\\Users\\user\\Desktop\\project\\node_modules\\express\\index.js',
    'C:\\Users\\user\\Desktop\\project\\server.js'
  ]
}

Answer №1

To begin, start by uninstalling the globally installed express package.

Next, create a package.json file using the command npm init. This file will contain information about your dependencies, scripts, package name, and other important data.

Then, proceed to install express by running the command npm i express.

Once installation is complete, you can run your server using node server.js or npm start if you have defined the start script in your package.json file.

Answer №2

I have found the solution

To resolve this issue, please download the necessary utilities from https://www.npmjs.com/package/utils

The following error has occurred:

Path: C:\Users\user\Desktop\project> node server

node:internal/modules/cjs/loader:928
  throw err;
  ^
Error: Module 'utils' not found
Require stack:
- C:\Users\user\Desktop\project\server.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:925:15)
    at Function.Module._load (node:internal/modules/cjs/loader:769:27)
    at Module.require (node:internal/modules/cjs/loader:997:19)
    at require (node:internal/modules/cjs/helpers:92:18)
    at Object.<anonymous> (C:\Users\user\Desktop\project\server.js:3:15)
    at Module._compile (node:internal/modules/cjs/loader:1108:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
    at Module.load (node:internal/modules/cjs/loader:973:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ 'C:\\Users\\user\\Desktop\\project\\server.js' ]
}

Execute the following command to install the required utilities:

C:\Users\user\Desktop\project> npm i utils

added 43 packages, removed 2 packages, changed 3 packages, and audited 110 packages in 16s
found 0 vulnerabilities
C:\Users\user\Desktop\project> node server
Example app listening at http://localhost:3000
^C

C:\Users\user\Desktop\project>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Redirecting with React Router outside of a route component

I am using React Router in my application to manage the routing functionalities. However, I have encountered an issue where I need to redirect the user from the Header component, which is not nested inside the Router component. As a result, I am unable t ...

What is the best way to dynamically adjust the top CSS of an element when it is positioned at the top or

Is there a way to dynamically set the top CSS when an element is over the top or bottom of a page? First, hover over the first cat image. It's working fine. http://image.ohozaa.com/i/480/7YpWei.jpg When I scroll the page to the bottom and hover ove ...

SonarQube flagging a suggestion to "eliminate this unnecessary assignment to a local variable"

Why am I encountering an error with SonarQube? How can I resolve it since the rule page does not offer a specific solution? The suggestion is to eliminate the unnecessary assignment to the local variable "validateAddressRequest". validateAddress() { ...

Utilizing SVG elements for interactive tooltips

Can the < use > element show the rect tooltip on mouseover in modern browsers? Refer to 15.2.1 The hint element. <svg id="schematic" version="1.2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <symbol i ...

How can you apply a class to a different element by hovering over one element?

Is there a way to darken the rest of the page when a user hovers over the menu bar on my website? I've been playing around with jQuery but can't seem to get it right. Any suggestions? I'm looking to add a class '.darken' to #conte ...

Creating a JavaScript array in Rails 4 based on current data without the need to reload the webpage

Currently in my rails 4 app, I am working on a unique tags validation using jquery validate to ensure that tags are not duplicated before they can be added to an item. The tag list structure is as follows: <div id="taglist"> <span class="label ...

Can any problems with jest packages be resolved?

I recently began testing my react.js project using react-testing-library and jest. However, when I try running the test with npm test, I encounter the following error: /Users/Tomascdmota/Downloads/Trust-Motores-main/node_modules/react-scripts/node_modules/ ...

Does Next js Backend support multithreading as a default feature?

As I begin my project, I am utilizing the built-in Node js server within Next js by running the next start command. However, I am uncertain as to whether it has multithreading capabilities. My inquiry is this: Would you suggest sticking with the built-in ...

I encountered a NextJS error while trying to implement getStaticProps(). Can someone help identify the issue at hand?

I'm encountering an issue while using getStaticProps(). I am working with nextjs and passing the returned value as props to a component. Despite trying various methods such as await and JSON.stringify(), nothing seems to be effective in resolving the ...

If you try to wrap an object with an anonymous function, you'll receive an error message

Consider the following straightforward example. (function() { var message = { display: function() { alert('hello'); } }; })(); When trying to implement message.display(); An error is triggered stating ReferenceError: message ...

encountering a glitch during the electron.js build process with nextjs

When attempting to build Electron.js with Next.js, I keep encountering this persistent error. I have updated my packages and reinstalled node modules multiple times, but I am still unable to resolve it. C:\Users\Himanshu\Desktop\claros& ...

Having trouble with installing yarn version 1.22

After initially having Yarn 1.22 installed, I decided to upgrade to Yarn 3. However, wanting to revert back to Yarn 1.22, I found that every time I use the command npm i -g <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ccb5adb ...

Unable to substitute 'npm run build' for 'webpack' command

I'm having trouble with npm run build not calling webpack as expected. I've modified the script in my package.json file, but it didn't work. I'm using Linux. Here is a snippet from my package.json file: { "name": "learn-webpack", ...

I am required to deliver a certificate to a secure HTTPS web address

I need to utilize a third-party API that necessitates sending a signed HTTPS request from my NodeJS express web server. The process involves using a specific certificate provided by the API. Unfortunately, I am facing some challenges with this integration ...

Ways to create a URL path that is not case-sensitive in NextJs using JavaScript

I am currently working on a project using NextJs and I have encountered an issue with URL case sensitivity. The paths fetched from an API all start with a capital letter, causing inconsistency in the URLs. For instance, www.mysite.com/About. I would like t ...

"Responding to an Ajax request with a .NET Core server by sending an xlsx

My web application exclusively supports .xlsx files. I have implemented a function in my controller that converts .xls files to .xlsx format successfully. When trying to open a .xls file, I send it via an Ajax request. However, the converted .xlsx file do ...

Jquery Ajax failing to retrieve a response

Here's the jQuery script I am using to fetch data from my server: $(".login_button").click(function () { var username = $(".username").val(); var userkey = $(".userkey").val(); $.ajax({ type: "GET", url: "http://192.168.0. ...

Aligning a navigation bar with a hamburger menu in the center

I recently implemented a hamburger menu with some cool animations into my site for mobile devices. Now, I am facing the challenge of centering the menu on desktop screens and it's proving to be tricky. The positioning is off, and traditional methods l ...

Discover the process of loading one controller from another controller in Angular JS

Is it possible to load an entire controller1 from a different controller2, not just a function? ...

When working with Angular 12, the target environment lacks support for dynamic import() syntax. Therefore, utilizing external type 'module' within a script is not feasible

My current issue involves using dynamic import code to bring in a js library during runtime: export class AuthService { constructor() { import('https://apis.google.com/js/platform.js').then(result => { console.log(resul ...