question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error: Cannot find module '../cli/next-start' when installing node_modules in a Dockerfile

See original GitHub issue

Bug report

The floowing error alerted me to this bug.

(node:67) UnhandledPromiseRejectionWarning: Error: Cannot find module '../cli/next-start'
 Require stack:
 - /home/site/wwwroot/node_modules/.bin/next

This error happens whetever I used a Dockerized of next.

Describe the bug

When installing dependencies in a Dockerfile the wrong node_modules/.bin/next binary is created.

To Reproduce

Can be recreated with these dependencies and this Dockerfile

{
  "dependencies" : {
    "next": "^9.1.6",
    "react": "16.12.0",
    "react-dom": "16.12.0",
  }
}
FROM node:12-slim
WORKDIR /install
COPY package.json .
COPY package-lock.json .

RUN npm ci
RUN cat /install/node_modules/.bin/next

Then a simple docker build . will print out the wrong bin file.

Expected behavior

The expected bin file looks like this on my own system

#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")

case `uname` in
    *CYGWIN*) basedir=`cygpath -w "$basedir"`;;
esac

if [ -x "$basedir/node" ]; then
  "$basedir/node"  "$basedir/../next/dist/bin/next" "$@"
  ret=$?
else 
  node  "$basedir/../next/dist/bin/next" "$@"
  ret=$?
fi
exit $ret

but the Dockerfile reports this:

#!/usr/bin/env node
"use strict";var _index=_interopRequireDefault(require("next/dist/compiled/arg/index.js"));function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj};}function _getRequireWildcardCache(){if(typeof WeakMap!=="function")return null;var cache=new WeakMap();_getRequireWildcardCache=function(){return cache;};return cache;}function _interopRequireWildcard(obj){if(obj&&obj.__esModule){return obj;}if(obj===null||typeof obj!=="object"&&typeof obj!=="function"){return{default:obj};}var cache=_getRequireWildcardCache();if(cache&&cache.has(obj)){return cache.get(obj);}var newObj={};var hasPropertyDescriptor=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var key in obj){if(Object.prototype.hasOwnProperty.call(obj,key)){var desc=hasPropertyDescriptor?Object.getOwnPropertyDescriptor(obj,key):null;if(desc&&(desc.get||desc.set)){Object.defineProperty(newObj,key,desc);}else{newObj[key]=obj[key];}}}newObj.default=obj;if(cache){cache.set(obj,newObj);}return newObj;}['react','react-dom'].forEach(dependency=>{try{// When 'npm link' is used it checks the clone location. Not the project.
require.resolve(dependency);}catch(err){// tslint:disable-next-line
console.warn(`The module '${dependency}' was not found. Next.js requires that you include it in 'dependencies' of your 'package.json'. To add it, run 'npm install --save ${dependency}'`);}});const defaultCommand='dev';const commands={build:async()=>await Promise.resolve().then(()=>_interopRequireWildcard(require('../cli/next-build'))).then(i=>i.nextBuild),start:async()=>await Promise.resolve().then(()=>_interopRequireWildcard(require('../cli/next-start'))).then(i=>i.nextStart),export:async()=>await Promise.resolve().then(()=>_interopRequireWildcard(require('../cli/next-export'))).then(i=>i.nextExport),dev:async()=>await Promise.resolve().then(()=>_interopRequireWildcard(require('../cli/next-dev'))).then(i=>i.nextDev),telemetry:async()=>await Promise.resolve().then(()=>_interopRequireWildcard(require('../cli/next-telemetry'))).then(i=>i.nextTelemetry)};const args=(0,_index.default)({// Types
'--version':Boolean,'--help':Boolean,'--inspect':Boolean,// Aliases
'-v':'--version','-h':'--help'},{permissive:true});// Version is inlined into the file using taskr build pipeline
if(args['--version']){// tslint:disable-next-line
console.log(`Next.js v${"9.1.6"}`);process.exit(0);}// Check if we are running `next <subcommand>` or `next`
const foundCommand=Boolean(commands[args._[0]]);// Makes sure the `next <subcommand> --help` case is covered
// This help message is only showed for `next --help`
if(!foundCommand&&args['--help']){// tslint:disable-next-line
console.log(`
    Usage
      $ next <command>

    Available commands
      ${Object.keys(commands).join(', ')}

    Options
      --version, -v   Version number
      --inspect       Enable the Node.js inspector
      --help, -h      Displays this message

    For more information run a command with the --help flag
      $ next build --help
  `);process.exit(0);}const command=foundCommand?args._[0]:defaultCommand;const forwardedArgs=foundCommand?args._.slice(1):args._;if(args['--inspect'])throw new Error(`Use env variable NODE_OPTIONS instead: NODE_OPTIONS="--inspect" next ${command}`);// Make sure the `next <subcommand> --help` case is covered
if(args['--help']){forwardedArgs.push('--help');}const defaultEnv=command==='dev'?'development':'production';process.env.NODE_ENV=process.env.NODE_ENV||defaultEnv;// this needs to come after we set the correct NODE_ENV or
// else it might cause SSR to break
const React=require('react');if(typeof React.Suspense==='undefined'){throw new Error(`The version of React you are using is lower than the minimum required version needed for Next.js. Please upgrade "react" and "react-dom": "npm install --save react react-dom" https://err.sh/zeit/next.js/invalid-react-version`);}commands[command]().then(exec=>exec(forwardedArgs));if(command==='dev'){const{CONFIG_FILE}=require('../next-server/lib/constants');const{watchFile}=require('fs');watchFile(`${process.cwd()}/${CONFIG_FILE}`,(cur,prev)=>{if(cur.size>0||prev.size>0){// tslint:disable-next-line
console.log(`\n> Found a change in ${CONFIG_FILE}. Restart the server to see the changes in effect.`);}});}

Screenshots

System information

  • OS: Bitbucket pipelines
  • Version of Next.js: 9.1.6

Additional context

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
pmabrescommented, Mar 19, 2020

Sorry, I’ve had the same issue today, you can try conserving symlinks while zipping files, node relies on symlinks for this. zip -r --symlinks output.zip .

0reactions
balazsorban44commented, Jan 30, 2022

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot find module for a node js app running in a docker ...
The common way is to create a Dockerfile that is already aware of your application, and make it copy your package.json file and...
Read more >
Dockerized Node.js app Error: cannot find module - Compose
When I start a dockerized Node.js testapp with sudo docker-compose up I get the following error: Starting testapp_web_1 ... done Attaching ...
Read more >
Docker Integration - PM2.IO
Advanced process manager for production Node.js applications. Load balancer, logs facility, startup script, micro service management, at a glance.
Read more >
Newbie in Docker. Getting "Error: Cannot find module 'express'"
Hi. I am following a docker tutorial. We are building a small project where we create a small node js app, wrap it...
Read more >
10 best practices to containerize Node.js web applications ...
js Docker images. You'll find it helpful regardless of the Node.js application you aim to build. This article will be helpful for you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found