How to fix Next.js Vercel deployment module not found error

27,644

Solution 1

This error typically happens if you're accidentally committing node_modules to your project's Git Repostiory.

Could you try to do the following?

  1. Ensure all changes have been committed and you have a clean directory.
  2. Run rm -rf node_modules (or delete the folder on Windows).
  3. Run git add -A then git commit -m "Remove all module files".
  4. Add node_modules to your .gitignore file (and save).
  5. Run git add -A then git commit -m "Update ignored files".
  6. Verify your directory is completely clean via git status.
  7. Then, run git push. This deployment should work on Vercel.
  8. Finally, re-run npm i or yarn depending on your package manager to get your local copy working.

Solution 2

I had to edit my package.json to use the next binary that ships in the node_modules/next directory:

"scripts": {
  "start": "node_modules/next/dist/bin/next start -p $PORT"
}

Not the most elegant fix but it works.

Solution 3

I'm having this exact same issue. I think it may be an internal issue with Vercel's deployment infrastructure. Notice the line it is failing on:

Error: Cannot find module '../build/output/log' 20:43:24.967
Require stack: 20:43:24.967
- /vercel/5ccaedc9/node_modules/.bin/next 20:43:24.967

My issue started yesterday, quite unexpectedly -- i.e. with a very simple commit. In my case, previously successful deploys also fail. Likewise, deleting the project and starting over did not help. I am in communication with Vercel support but they have not yet acknowledged the problem is on their end yet or offered any kind of solution.

Solution 4

This answer worked for me: https://stackoverflow.com/a/55541435/3051080

TL;DR; update git cache:

git rm -r --cached .
git add --all .
git commit -a -m "Versioning untracked files"
git push origin master

Solution 5

I had the same issue. In my github desktop I noticed that a file that was capitalized in the editor was not in the github desktop. Fixed the spelling to match what was showing on github and the project built successfully.

Share:
27,644
Tom Wicks
Author by

Tom Wicks

I am a designer.

Updated on July 09, 2022

Comments

  • Tom Wicks
    Tom Wicks almost 2 years

    My next.js app works on my machine and was working when deployed on Vercel but now it fails when building on Vercel with the following error:

    I've tried deleting node_modules and running npm install a few times but with no joy.

    Any help would be hugely appreciated. Thank you!

    Running "npm run build" 20:43:24.926
    [email protected] build /vercel/5ccaedc9 20:43:24.926
    next build 20:43:24.967
    internal/modules/cjs/loader.js:983 20:43:24.967
    throw err; 20:43:24.967
    ^ 20:43:24.967
    Error: Cannot find module '../build/output/log' 20:43:24.967
    Require stack: 20:43:24.967
    - /vercel/5ccaedc9/node_modules/.bin/next 20:43:24.967
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:980:15) 20:43:24.967
    at Function.Module._load (internal/modules/cjs/loader.js:862:27) 20:43:24.967
    at Module.require (internal/modules/cjs/loader.js:1042:19) 20:43:24.967
    at require (internal/modules/cjs/helpers.js:77:18) 20:43:24.967
    at Object. (/vercel/5ccaedc9/node_modules/.bin/next:2:46) 20:43:24.967
    at Module._compile (internal/modules/cjs/loader.js:1156:30) 20:43:24.967
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10) 20:43:24.967
    at Module.load (internal/modules/cjs/loader.js:1000:32) 20:43:24.967
    at Function.Module._load (internal/modules/cjs/loader.js:899:14) 20:43:24.967
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) { 20:43:24.967
    code: 'MODULE_NOT_FOUND', 20:43:24.967
    requireStack: [ '/vercel/5ccaedc9/node_modules/.bin/next' ] 20:43:24.967
    } 20:43:24.969
    npm ERR! code ELIFECYCLE 20:43:24.969
    npm ERR! errno 1 20:43:24.970
    npm ERR! [email protected] build: next build 20:43:24.970
    npm ERR! Exit status 1 20:43:24.970
    npm ERR! 20:43:24.970
    npm ERR! Failed at the [email protected] build script. 20:43:24.970
    npm ERR! This is probably not a problem with npm. There is likely additional logging output above. 20:43:24.974
    npm ERR! A complete log of this run can be found in: 20:43:24.974
    npm ERR! /vercel/.npm/_logs/2020-06-17T19_43_24_971Z-debug.log 20:43:24.979
    Error: Command "npm run build" exited with 1 20:43:25.342
    [dmesg] follows: 20:43:25.342
    [ 962.449223] ecs-bridge: port 1(veth2a021300) entered disabled state 20:43:25.342
    [ 962.453655] device veth2a021300 entered promiscuous mode 20:43:25.342
    [ 962.457686] ecs-bridge: port 1(veth2a021300) entered blocking state 20:43:25.342
    [ 962.462004] ecs-bridge: port 1(veth2a021300) entered forwarding state 20:43:26.242
    Done with "package.json"

    Here's my Package.json

    {
      "name": "tdwcks",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "dev": "next",
        "build": "next build",
        "start": "next start"
      },
      "keywords": [],
      "author": "",
      "license": "ISC",
      "dependencies": {
        "cjs": "0.0.11",
        "core-util-is": "^1.0.2",
        "framer-motion": "^1.11.0",
        "gray-matter": "^4.0.2",
        "next": "^9.4.4",
        "raw-loader": "^4.0.1",
        "react": "^16.13.1",
        "react-dom": "^16.13.1",
        "react-ga": "^3.0.0",
        "react-markdown": "^4.3.1",
        "react-player": "^2.2.0",
        "react-scripts": "^3.4.1"
      },
      "devDependencies": {
        "postcss-preset-env": "^6.7.0",
        "tailwindcss": "^1.4.6"
      }
    }
    
    • hangindev.com
      hangindev.com almost 4 years
      So you can successfully build on your local machine with npm run build?
    • Tom Wicks
      Tom Wicks almost 4 years
      Yeah, building on my local machine totally fine.
    • Tenclea
      Tenclea almost 4 years
      Do you use a .gitignore file ? If yes, what's in it ?
    • Tom Wicks
      Tom Wicks almost 4 years
      @tenclea Nope I don't.
    • Tenclea
      Tenclea almost 4 years
      Have you tried to use another version of the next module ?
    • Tom Wicks
      Tom Wicks almost 4 years
      Yeah, still didn't run on Vercel.
    • Tenclea
      Tenclea almost 4 years
      This line seems interesting npm WARN [email protected] requires a peer of typescript@>=2.8.0 || (...) || >= 3.7.0-beta but none is installed. You must install peer dependencies yourself. 21:31:01.088 even though it's only a warning. You should try to give npm i typescript a shot
    • John Magistr
      John Magistr almost 4 years
      Having the same issue as of now. Seems to have come out of nowhere. Been building and deploying the same Dockerfile for months, and suddenly it starts to fail with this error.
    • AryanJ-NYC
      AryanJ-NYC almost 4 years
      Did you find out what it was? I now have this bug as well.
    • AryanJ-NYC
      AryanJ-NYC almost 4 years
    • paulogdm
      paulogdm almost 4 years
      You can reach out to our support channel on vercel.com/support/request if you have a reproduction. We will happily take a look at all cases.
  • Tom Wicks
    Tom Wicks almost 4 years
    Thanks for your help. I've tried both installing typescript and creating a .gitignore file for the .next folder but hasn't solved the issue sadly.
  • Tenclea
    Tenclea almost 4 years
    @TomWicks Is there a way for you to completely flush your Vercel app, or to delete this one to create another, and see if it changes anything ?
  • Tom Wicks
    Tom Wicks almost 4 years
    Updated the error message in the original question. I'll try creating another Vercel app.
  • Tom Wicks
    Tom Wicks almost 4 years
    still got the same issue :(
  • Tenclea
    Tenclea almost 4 years
    @TomWicks this might sound stupid, but do you remember doing anything that could have affected the build operation ? (Adding new modules, etc...)
  • Tom Wicks
    Tom Wicks almost 4 years
    That's interesting I'll get on to their support too.
  • edwardr
    edwardr almost 4 years
    I received a rather dubious but successful response from them. Is node_modules part of your repo? If so, try deleting it. Not sure why this would suddenly become a deployment issue, as I've made several hundred success deploys with this project previously. Nevertheless, whatever issue they were having seems to be fixed on my end by removing node_modules from the repo itself (also .next/, in case you had that there as well)
  • rolfmadsen
    rolfmadsen almost 4 years
    NB. I have had .next in the .gitignore file all along.
  • paulogdm
    paulogdm almost 4 years
    Hello, If you are facing problems with Vercel, message us at vercel.com/support/request w/ a reproduction so we can assist you.
  • remjx
    remjx over 3 years
    Yeah for me i renamed a folder and for some reason the update didn't push to git
  • Raul Butuc
    Raul Butuc over 3 years
    This is the only thing that actually works for me.. But why does NextJS expect the node_modules in a production deployment where we already ran the export command.. This is mind-boggling
  • Raul Butuc
    Raul Butuc over 3 years
    This is the only thing that actually works for me.. But why does NextJS expect the node_modules in a production deployment where we already ran the export command.. This is mind-boggling
  • AryanJ-NYC
    AryanJ-NYC over 3 years
    Where are you deploying your code? I've had success using environment variables in Microsoft Azure: github.com/vercel/next.js/discussions/…
  • Raul Butuc
    Raul Butuc over 3 years
    It's deployed to Azure DevOps (private repo) and picked by a custom pipeline. Works fine now, using your updated package.json line for yarn start.
  • Raul
    Raul over 3 years
    This is the answer, I found tons of question about this problems, and for me in this case it's works, only with the step 1 works for me, thanks!.
  • James
    James over 2 years
    This is the only thing that worked for me too. Is there something messed up locally that it doesn't look in node_modules for package.json commands?
  • vSimak
    vSimak almost 2 years
    This is what worked for me. Due to changing letter case of a folder in components folder. The cache didn't track it, meaning it was pushing the old version.