Module not found, searching in an incorrect directory

After installing nodemon globally with the command npm i -g nodemon, it was installed in the global path:

C:\Users\John\AppData\Roaming

I then initiated a new project using npm init -y and created an index.js entry point with the following content:

{
  "name": "scp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1",
    "jsonwebtoken": "^8.5.1"
  }
}

However, when I tried starting the app using nodemon, I encountered the error message:

Error: Cannot find module 'C:\Program Files\nodejs\node_modules\nodemon\bin\nodemon.js'

Realizing that the path is incorrect, I attempted to reset it by running:

npm config set prefix C:\Users\John\AppData\Roaming
npm config get prefix

This returned:

C:\Users\John\AppData\Roaming

Despite this adjustment, the same error persisted when trying to start the app using nodemon:

Error: Cannot find module 'C:\Program Files\nodejs\node_modules\nodemon\bin\nodemon.js'

How can I resolve this issue?

Answer №1

Would you be willing to give nodemon a shot by incorporating it into your existing package.json file using the following command:

npm install --save nodemon

Afterwards, update your start script to:

"start": "nodemon index.js"

Keep your fingers crossed and hopefully this solution will do the trick for you.

Answer №2

Encountering a similar error can occur when using the "&" symbol in a path name. While npm will install packages correctly in the 'foobar&foobar/myApp/node_modules' folder, the file paths to some packages will be incorrect. To resolve this issue, simply rename the folder containing the "&" symbol (foobar&foobar --> foobarAndfoobar) and then execute the following commands where your node_modules are located:

rm -rf node_modules
npm install

This solution should help alleviate any frustration for others facing the same problem.

Answer №3

To streamline your development process, consider adding nodemon as a dev dependency and specifying it in the scripts section of your project's package.json. This alleviates the need to install it globally whenever it's required.

For example, in your package.json:

 "scripts": {
    "start": "nodemon index.js"
  },

Regarding your original issue... The problem may stem from the configuration of your npm/node setup. It is recommended to utilize tools like NVM which manage multiple instances and configurations locally. This practice is highly recommended for any developer working with node applications.

Take a look at NVM here: https://github.com/coreybutler/nvm-windows Try using a local version of npm to troubleshoot the source of the issue. Keep in mind that conflicts may arise between your global node installation and NVM (such as bin paths), so consider exclusively using npm/nodes from NVM and removing the global one.

Answer №4

Make sure to review all your folders for any occurrences of "&". Change the name and nodemon should function properly. I spent an hour and a half troubleshooting this issue.

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

Developing a production build of a React app on a high-end machine is taking an unexpectedly

Our team is facing challenges with the performance of our production build, as it is taking approximately 20 minutes to complete. We initially suspected that the issue might be related to our local machine's capabilities, so we decided to run the buil ...

The Art of Structuring MongoDB Schema

I have been diving into MongoDB through their online Webinar hosted on their platform and I am currently working on connecting Products to a Category (for example, associating iPhones with the Mobiles Category which falls under Electronics). However, as I ...

Extracting a nested property from a JSON object without relying on the lodash get method

We have a sample json data: { "name": "app", "version": "0.1.0", "description": "Sample description", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { "type": "git", "url": "so ...

Stop processing the current websocket connection once a new websocket request is received

Currently, I am utilizing the npm ws module (or its wrapper named isomorphic-ws) for establishing a websocket connection. NPM Module: isomorphic-ws This setup allows me to retrieve an array of data from a websocket++ server situated on the same local mac ...

Step-by-step guide for sending data using module.exports in a node.js application

Currently, I am working on implementing a feature that will allow users to input data and store it in a database collection. The technologies I am using for this project are Node.js, MongoDB, Mongoose, Express.js, and AJAX. My goal is to capture user inpu ...

Function not executing on button press in React.js

I am trying to trigger a function upon clicking a button, but unfortunately, nothing is happening. Despite adding a console.warn() statement inside the function, it doesn't seem to be logging anything. I've looked through similar Stack Overflow s ...

Problem with npm link <package>

Operating System: Windows 7, 64-bit Npm Version: 3.10.10 Node Version: 6.9.4 Hello, We are currently encountering issues with a custom npm package that we have published on our registry and installed in our Angular applications. Below are the di ...

What is the best way to deliver static HTML files in Nest.js?

I am encountering an issue with loading JS files from a static /dist folder located outside of my Nest project. While the index.html file loads successfully, any JS file results in a 404 error. In another Node/Express.js project, I am able to serve these ...

Filtering relations in TypeORM can be achieved by using various query criteria

Hello, I have a couple of questions regarding TypeORM in TypeScript. Using Find() Only: I have two tables in my database - Users and Sessions. I am interested in retrieving a specific User along with all their Sessions where the sessions_deleted_at column ...

Communicating between a Python Client and a nodeJS Server using Socket.IO

I am attempting to transmit data from my Raspberry Pi (using Python 2.7.9) to my NodeJS server with socket.io. My objective is to continuously send multiple values from my Pi through a websocket connection to my local Node Server, which will then display ...

When using npm or yarn, running the command `node-gyp rebuild` may result in an error with an exit status

Having an issue while using yarn on Windows. I've tried versions 12.13.1, *10.16.0 (currently using a 64-bit executable), and 9.11.2. The error message I'm getting is: Error C:\XXX\node_modules\node-expat: Command failed. Exit cod ...

Integrating Octokit middleware in Next.js for enhanced functionality

Currently, I am in the process of honing my skills by creating a GitHub app. In Octokit, there is a feature called createNodeMiddleware that caught my attention. However, integrating it with next.js seems to pose some challenges. My main issue right now re ...

Solution for fixing the error: MongooseError [OverwriteModelError]: It is not possible to overwrite the `User` model after it has been compiled in

I am new to working with the MERN stack and currently attempting to create an exercise tracker app following a tutorial on YouTube. However, I am encountering the Mongoose: OverwriteModelError when running the server and cannot seem to identify where I am ...

Encountering problem with Node 7.7.3 - Error: Path parameter needs to be a valid string

Working on updating my application to be compatible with Node -v 7.7.3. However, when trying to execute the grunt task dom_munger as shown below: dom_munger:{ read: { options: { read:[ {selector:'script[data-concat!="false"]&apos ...

Encountering a 404 error when trying to access a private repository following the addition of a personal access token to the

Uncertain about the reason behind this issue. Receiving a Request failed \"404 Not Found\"" I have generated the token in my github settings, configured the proper repository permissions for the token, and then included it in my . ...

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", ...

Delay the rendering of the fetching component until the fetch operation is completed

My task is to retrieve data for SSR and send that data from the Client. const MyPage = ({ myFetch1, myFetch2, myFetch3, }) => { const dispatch = useDispatch(); dispatch(doSomething1(myFetch1)); dispatch(doSomething2(myFetch2)); dispatch(do ...

What are the steps to redirect from a nested route back to the top route using Node.js with Express?

Is there a way to redirect from a nested route to a top route in Express.js? In the following code snippet, how can we make the callback for the route /toproute/nested redirect to /profile instead of /toproute/profile? // app.js const express = require(& ...

Obtain the position from the split() function in a Node event stream

I am currently working on a Node script utilizing the event-stream module to process a file line-by-line using the split() method. Is there a way for me to access the line number in the output generated by this process? var stream = fs.createReadStream( ...

Node Express: Detecting Status 304 Based on ETag Matching

Is there a way for me to determine if the response status will be 304 (Not-Modified) due to the etag value in the If-None-Match header? I am looking to log this data, however, I am unable to retrieve it on my node server. It appears that Express handles th ...