vercel/pkg

"Error: Cannot find module" for a module that definitely exists

iameli-streams opened this issue · 4 comments

What version of pkg are you using?

5.8.1

What version of Node.js are you using?

18.16,1

What operating system are you using?

Ubuntu 22.04

What CPU architecture are you using?

x86_64

What Node versions, OSs and CPU architectures are you building for?

node16-linux-x64 (EDIT: going to Node 18 did not fix the problem)

Describe the Bug

I'm getting an error importing a Next.js config file. Except... it knows it has the file, because it suggests it to me.

2023-07-17 23:26:29 [error]: 	Error: Cannot find module '/snapshot/studio/packages/www/next.config.js' imported from /snapshot/studio/node_modules/next/dist/server/config.js
Did you mean to import /snapshot/studio/packages/www/next.config.js?

I DID mean to import /snapshot/studio/packages/www/next.config.js, which is why I did it. 😕

Running with --debug shows the file being imported several times:

 ▶ cat pkg.log | grep next\\.config.js
  /home/iameli/code/studio/packages/www/next.config.js
> [debug] Content of /home/iameli/code/studio/packages/www/next.config.js is added to queue. It was required from /home/iameli/code/studio/packages/api/package.json
> [debug] Stat info of /home/iameli/code/studio/packages/www/next.config.js is added to queue.
> [debug] Bytecode of /home/iameli/code/studio/packages/www/next.config.js is added to queue. It was required from /home/iameli/code/studio/packages/www/server.js
  /home/iameli/code/studio/node_modules/@livepeer.studio/www/next.config.js
> [debug] Content of /home/iameli/code/studio/packages/www/next.config.js is added to queue. It was required from /home/iameli/code/studio/node_modules/@livepeer.studio/api/package.json
> [debug] Stat info of /home/iameli/code/studio/node_modules/@livepeer.studio/www/next.config.js is added to queue.
> [debug] Stat info of /home/iameli/code/studio/packages/www/next.config.js is added to queue.
> [debug] Content of /home/iameli/code/studio/packages/www/next.config.js is added to queue.
> [debug] Content of /home/iameli/code/studio/node_modules/@sentry/nextjs/package.json is added to queue. It was required from /home/iameli/code/studio/packages/www/next.config.js
> [debug] Bytecode of /home/iameli/code/studio/node_modules/@sentry/nextjs/cjs/index.server.js is added to queue. It was required from /home/iameli/code/studio/packages/www/next.config.js
> [debug] Content of /home/iameli/code/studio/node_modules/next-compose-plugins/package.json is added to queue. It was required from /home/iameli/code/studio/packages/www/next.config.js
> [debug] Bytecode of /home/iameli/code/studio/node_modules/next-compose-plugins/lib/index.js is added to queue. It was required from /home/iameli/code/studio/packages/www/next.config.js
> [debug] Content of /home/iameli/code/studio/node_modules/remark-emoji/package.json is added to queue. It was required from /home/iameli/code/studio/packages/www/next.config.js
> [debug] Bytecode of /home/iameli/code/studio/node_modules/remark-emoji/index.js is added to queue. It was required from /home/iameli/code/studio/packages/www/next.config.js
> [debug] Content of /home/iameli/code/studio/node_modules/@next/mdx/package.json is added to queue. It was required from /home/iameli/code/studio/packages/www/next.config.js
> [debug] Bytecode of /home/iameli/code/studio/node_modules/@next/mdx/index.js is added to queue. It was required from /home/iameli/code/studio/packages/www/next.config.js
> [debug] Content of /home/iameli/code/studio/node_modules/@next/bundle-analyzer/package.json is added to queue. It was required from /home/iameli/code/studio/packages/www/next.config.js
> [debug] Bytecode of /home/iameli/code/studio/node_modules/@next/bundle-analyzer/index.js is added to queue. It was required from /home/iameli/code/studio/packages/www/next.config.js
> [debug] Content of /home/iameli/code/studio/node_modules/duplicate-package-checker-webpack-plugin/package.json is added to queue. It was required from /home/iameli/code/studio/packages/www/next.config.js
> [debug] Bytecode of /home/iameli/code/studio/node_modules/duplicate-package-checker-webpack-plugin/lib/index.js is added to queue. It was required from /home/iameli/code/studio/packages/www/next.config.js
> [debug] Stat info of /home/iameli/code/studio/packages/www/next.config.js is added to queue.
  /home/iameli/code/studio/packages/www/next.config.js
  /home/iameli/code/studio/packages/www/next.config.js
  next.config.js

Expected Behavior

Working import

To Reproduce

I set up a minimal reproduction case here. For the record, all I'm trying to do is export a handler from the Next.js app so that it can be served from our API server for URLs that don't start with /api.

git clone https://github.com/iameli-streams/studio.git -b eli/pkg-bug
cd studio
yarn
cd packages/api
yarn run pkg:local
./bin/api

Output:

[Function (anonymous)]
error - Failed to load next.config.js, see more info here https://nextjs.org/docs/messages/next-config-error
node:internal/process/promises:279
            triggerUncaughtException(err, true /* fromPromise */);
            ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/snapshot/studio/packages/www/next.config.js' imported from /snapshot/studio/node_modules/next/dist/server/config.js
Did you mean to import /snapshot/studio/packages/www/next.config.js?
    at new NodeError (node:internal/errors:372:5)
    at finalizeResolution (node:internal/modules/esm/resolve:437:11)
    at moduleResolve (node:internal/modules/esm/resolve:1009:10)
    at defaultResolve (node:internal/modules/esm/resolve:1218:11)
    at ESMLoader.resolve (node:internal/modules/esm/loader:580:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:294:18)
    at ESMLoader.import (node:internal/modules/esm/loader:380:22)
    at importModuleDynamically (node:internal/modules/cjs/loader:1043:29)
    at importModuleDynamicallyWrapper (node:internal/vm/module:437:21)
    at importModuleDynamically (node:vm:383:46) {
  code: 'ERR_MODULE_NOT_FOUND'
}
iameli commented

FWIW my use case was loading that config file to boot a Next.js server programatically. I wrote this to export the HTTP handler and work around this issue:

// Programmatic entrypoint into the Next.js server for `pkg` purposes.

const next = require("next");
const config = require("./next.config.js");
const { normalizeConfig } = require("next/dist/server/config-shared");
const {
  PHASE_PRODUCTION_SERVER,
} = require("next/dist/shared/lib/constants.js");

const getApp = async () => {
  // Workaround for https://github.com/vercel/pkg/issues/1962
  const normalizedConfig = await normalizeConfig(
    PHASE_PRODUCTION_SERVER,
    config
  );
  const app = next({
    dir: __dirname,
    dev: false,
    conf: normalizedConfig,
  });
  await app.prepare();
  return app.getRequestHandler();
};
module.exports = getApp();

This issue is stale because it has been open 90 days with no activity. Remove the stale label or comment or this will be closed in 5 days. To ignore this issue entirely you can add the no-stale label

This issue is now closed due to inactivity, you can of course reopen or reference this issue if you see fit.

How do I re-open? Still a problem per my understanding.