Jahmar Lawrence
Jahmar Lawrence

Reputation: 103

ERROR MODULE NOT FOUND whenever I try to run 'npm run dev' using Next.js

Whenever I run npm run dev I get this error.

I've tried to delete node_modules and reinstall it using npm install but that didn't work. Then I tried to delete both node_modules and package-lock.json and reinstall them, that didn't work. I even tried upgrading Node to the latest version, but I'm still getting this issue.

I appreciate any help I get. Thank you in advance.

Error: Cannot find module 'C:\Users\jaydev\Desktop\ecommerce\fashionsite\node_modules\postcss\lib\parser'
    at createEsmNotFoundErr (node:internal/modules/cjs/loader:952:15)
    at finalizeEsmResolution (node:internal/modules/cjs/loader:945:15)
    at resolveExports (node:internal/modules/cjs/loader:473:14)
    at Function.Module._findPath (node:internal/modules/cjs/loader:513:31)
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:911:27)
    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.552 (C:\Users\jaydev\Desktop\ecommerce\fashionsite\node_modules\next\dist\compiled\postcss-scss\scss-syntax.js:1:11590)
    at __webpack_require__ (C:\Users\jaydev\Desktop\ecommerce\fashionsite\node_modules\next\dist\compiled\postcss-scss\scss-syntax.js:1:11735)
    at Object.560 (C:\Users\jaydev\Desktop\ecommerce\fashionsite\node_modules\next\dist\compiled\postcss-scss\scss-syntax.js:1:400)
    at __webpack_require__ (C:\Users\jaydev\Desktop\ecommerce\fashionsite\node_modules\next\dist\compiled\postcss-scss\scss-syntax.js:1:11735)
    at Object.290 (C:\Users\jaydev\Desktop\ecommerce\fashionsite\node_modules\next\dist\compiled\postcss-scss\scss-syntax.js:1:260)
    at __webpack_require__ (C:\Users\jaydev\Desktop\ecommerce\fashionsite\node_modules\next\dist\compiled\postcss-scss\scss-syntax.js:1:11735)
    at Object.632 (C:\Users\jaydev\Desktop\ecommerce\fashionsite\node_modules\next\dist\compiled\postcss-scss\scss-syntax.js:1:3733)
    at __webpack_require__ (C:\Users\jaydev\Desktop\ecommerce\fashionsite\node_modules\next\dist\compiled\postcss-scss\scss-syntax.js:1:11735) {
  code: 'MODULE_NOT_FOUND',
  path: 'C:\\Users\\jaydev\\Desktop\\ecommerce\\fashionsite\\node_modules\\postcss\\package.json'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] dev: `next dev`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\jaydev\AppData\Roaming\npm-cache\_logs\2020-12-25T07_06_40_981Z-debug.log

package.json: { "name": "fashionsite", "version": "0.1.0", "private": true, "scripts": { "dev": "next dev", "build": "next build", "start": "next start" }, "dependencies": { "next": "10.0.4", "react": "17.0.1", "react-dom": "17.0.1" } }

Upvotes: 8

Views: 37798

Answers (12)

pete
pete

Reputation: 85

I had similar problem, when I typed (npm run dev), it showed me:

[email protected] dev next dev.

I used node version 14.something. I updated my node version to latest, today latest version is 18.15.0 LTS

then I typed (npm run dev) and it is working for me.

Upvotes: 0

Virgilio Lino
Virgilio Lino

Reputation: 91

I had the same problem and I found figured out the solution by following the linked explanation: https://nextjs.org/docs/messages/module-not-found.

The swr module has to be installed using a package manager.

When using npm: npm install swr
When using yarn: yarn add swr

where swr is the missing module. In my case as my missing module was graphql:

 yarn add graphql

did the trick

Upvotes: 0

pdutra145
pdutra145

Reputation: 9

For me deleting next.config.js and running npm run dev worked.

Upvotes: 0

John Camagong
John Camagong

Reputation: 1

My solution to that is updating my node version. I got the same error in 10.18. version and I upgraded to the latest and it works fine already, maybe it will works for you too.

Hope it helps this issue. it also works in 17.3.0 but right this moment the latest is 18.12.0.

Upvotes: 0

Daniel Jonsson
Daniel Jonsson

Reputation: 3893

I also got this error:

$ npm run start

> [email protected] start
> next start

ready - started server on 0.0.0.0:3000, url: http://localhost:3000
info - [next-export-optimize-images]: Configuration was not loaded. (This is optional.)
Error: Cannot find module '/home/matachi/proj/kalyna/.next/server/pages/index.js'
Require stack:
- /home/matachi/proj/kalyna/node_modules/next/dist/server/require.js
- /home/matachi/proj/kalyna/node_modules/next/dist/server/next-server.js
- /home/matachi/proj/kalyna/node_modules/next/dist/server/next.js
- /home/matachi/proj/kalyna/node_modules/next/dist/server/lib/start-server.js
- /home/matachi/proj/kalyna/node_modules/next/dist/cli/next-start.js
- /home/matachi/proj/kalyna/node_modules/next/dist/lib/commands.js
- /home/matachi/proj/kalyna/node_modules/next/dist/bin/next
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.mod._resolveFilename (/home/matachi/proj/kalyna/node_modules/next/dist/build/webpack/require-hook.js:27:32)
    at Function.mod._resolveFilename (/home/matachi/proj/kalyna/node_modules/next/dist/build/webpack/require-hook.js:27:32)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.requirePage (/home/matachi/proj/kalyna/node_modules/next/dist/server/require.js:58:12)
    at /home/matachi/proj/kalyna/node_modules/next/dist/server/load-components.js:61:73
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Object.loadComponents (/home/matachi/proj/kalyna/node_modules/next/dist/server/load-components.js:61:26) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/home/matachi/proj/kalyna/node_modules/next/dist/server/require.js',
    '/home/matachi/proj/kalyna/node_modules/next/dist/server/next-server.js',
    '/home/matachi/proj/kalyna/node_modules/next/dist/server/next.js',
    '/home/matachi/proj/kalyna/node_modules/next/dist/server/lib/start-server.js',
    '/home/matachi/proj/kalyna/node_modules/next/dist/cli/next-start.js',
    '/home/matachi/proj/kalyna/node_modules/next/dist/lib/commands.js',
    '/home/matachi/proj/kalyna/node_modules/next/dist/bin/next'
  ]
}

I fixed it by deleting the .next directory and restarting the dev server:

$ rm -rf .next 
$ npm run dev

> [email protected] dev
> next dev

ready - started server on 0.0.0.0:3000, url: http://localhost:3000
info - [next-export-optimize-images]: Configuration was not loaded. (This is optional.)
event - compiled client and server successfully in 9.9s (173 modules)
wait  - compiling / (client and server)...
event - compiled client and server successfully in 1809 ms (300 modules)

Upvotes: 0

minasrc
minasrc

Reputation: 114

In my case it turned out to be a problem with windows permissions. My project was on Desktop and changing its location to a directory with more permissions helped.

Upvotes: 2

dev__rezo
dev__rezo

Reputation: 61

To get rid of this error what you have to do... just update your node.js version Hopefully, it will help you.

Upvotes: 1

Amyra Athifah
Amyra Athifah

Reputation: 1

make sure u already update/upgrade

sudo apt update
sudo apt upgrade

after u run this command

curl -fsSL https://deb.nodesource.com/setup_17.x | sudo -E bash 
sudo apt-get install -y nodejs

sudo apt install npm

use latest node js repo avoid using sudo apt install nodejs..outdated..

Upvotes: 0

leul--Dev
leul--Dev

Reputation: 1

Make sure you are in the right directory when using npm run dev. You can also try switching to yarn.

I found this article helpful ->

https://hidayatabisena.medium.com/how-to-solve-npm-err-code-elifcycle-when-running-npm-run-dev-on-your-next-js-project-4794226d040f

Upvotes: -2

JoKer
JoKer

Reputation: 1

move next.config.js file from you project folder then execute npm run dev.

or

comment the contents of next.config.js.

any of the above methods will help run npm run dev.

Upvotes: 0

Hughes
Hughes

Reputation: 414

I had a similar issue and found that when I was running npm install after removing node_modules it wasn't correctly installing all the dependencies because my node version had switched back to an older version.

Make sure your environment is configured correctly first. For me: nvm use 16.0.0 and then npm install

Also, when you run npm install are there any messages that come up in the command line saying saying a dependency isn't met or something like that, might give you some hints of where to look.

Upvotes: 0

Abbas Hosseini
Abbas Hosseini

Reputation: 1643

  1. stop your dev server (ctrl+c)
  2. delete .next folder in your project's root folder.
  3. delete node_modules folder and package-lock.json file too.
  4. run npm cache clean --force
  5. run npm install
  6. run npm run dev

If the above didn't work then open your file explorer and see if the specified file exists on the specified path or not: C:\Users\jaydev\Desktop\ecommerce\fashionsite\node_modules\postcss\lib\parser.js

if it exists try moving your project's folder to a path other than desktop due to windows permission problems and let me know if the problem is resolved.

Upvotes: 23

Related Questions