Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nextjs 15: Importing git modules issue - 'Module not found: Can't resolve 'package-name' #69926

Open
moltco opened this issue Sep 10, 2024 · 7 comments
Labels
bug Issue was opened via the bug report template. Module Resolution Module resolution (CJS / ESM, module resolving).

Comments

@moltco
Copy link

moltco commented Sep 10, 2024

Link to the code that reproduces this issue

https://github.com/moltco/simple-consumer-2

To Reproduce

  1. Clone sample TypeScript module package: git clone https://github.com/moltco/simple-package
  2. Clone NextJs@canary app boilerplate that imports the above package: git clone https://github.com/moltco/simple-consumer-2
  3. pnpm install && pnpm build (or pnpm dev then visit http://localhost:3000/ to get it to compile)
  4. Error raised by Nextjs: 'Module not found: Can't resolve 'simple-package'

Current vs. Expected behavior

Expected: import to work
Current: typescript intellisense etc works but cannot compile.

Provide environment information

OS: Win 10
Nodejs: v20.17.0
pnpm: 9.10.0
"next": "15.0.0-canary.148",
"react": "19.0.0-rc-7771d3a7-20240827",
"react-dom": "19.0.0-rc-7771d3a7-20240827",

Which area(s) are affected? (Select all that apply)

Module Resolution

Which stage(s) are affected? (Select all that apply)

next dev (local), next build (local), next start (local)

Additional context

I tested agains canary 15.0.0-canary.56 and 15.0.0-canary.148 -> both seem to suffer from the same issue

@moltco moltco added the bug Issue was opened via the bug report template. label Sep 10, 2024
@github-actions github-actions bot added the Module Resolution Module resolution (CJS / ESM, module resolving). label Sep 10, 2024
@moltco
Copy link
Author

moltco commented Sep 10, 2024

Also fails with Nextjs 14.2.9 and React 18, same error indicating possible issue with Webpack:

> next build

  ▲ Next.js 14.2.9

   Creating an optimized production build ...
Failed to compile.

./app/page.tsx
Module not found: Can't resolve 'simple-package'

https://nextjs.org/docs/messages/module-not-found


> Build failed because of webpack errors
 ELIFECYCLE  Command failed with exit code 1.

I have tried using ts-load with 'special' webpack config as mentioned here https://stackoverflow.com/questions/72259833/error-importing-private-repo-into-next-project but this did not work.

Note - all repos used to reproduce issue here are public (although I would normally use a private repo).

@moltco
Copy link
Author

moltco commented Sep 10, 2024

I realised after a while that the packages installed from git are not compiled by npm/pnpm/yarn which was causing some issues. Once compiled, it feels the next dev fails to compile some code and instead of reporting what the issue is, the node reports that it can't find the file (which happens to be the git repo package).
So, I get something like

⨯ uncaughtException: TypeError [ERR_INVALID_ARG_VALUE]: The argument 'path' must be a string, Uint8Array, or URL without null bytes. Received 'C:\\Users\\moltco\\source\\repos\\nextjs-dashboard\\.next\\server\\vendor-chunks\\nice-message@[email protected]+mol...
    at Object.stat (node:fs:1583:10)
    at Object.stat (C:\Users\moltco\source\repos\nextjs-dashboard\node_modules\.pnpm\[email protected][email protected][email protected]_5a2lopq3y3vcnpkpn7fcimpuiy\node_modules\next\dist\compiled\webpack\bundle5.js:1:278998)
    at writeOut (C:\Users\moltco\source\repos\nextjs-dashboard\node_modules\.pnpm\[email protected][email protected][email protected]_5a2lopq3y3vcnpkpn7fcimpuiy\node_modules\next\dist\compiled\webpack\bundle5.js:28:144693)
    at C:\Users\moltco\source\repos\nextjs-dashboard\node_modules\.pnpm\[email protected][email protected][email protected]_5a2lopq3y3vcnpkpn7fcimpuiy\node_modules\next\dist\compiled\webpack\bundle5.js:28:1378856
    at FSReqCallback.oncomplete (node:fs:187:23)
    at FSReqCallback.callbackTrampoline (node:internal/async_hooks:130:17) {
  code: 'ERR_INVALID_ARG_VALUE'

What I did then is copy the code from git into the main project and after that I found that React/Nextjs were having issues in transferring data and functions between server and client components. Fair enough but the error messages with the repo were completely absent. Also, this wasn't the fault of the imported git library as such. I am also not clear why the issues between passing data/functions between server/client components wasn't raised before as the git library is a wrapper so all underlying types and their serialization properties were the same before and after the library. It is worth mentioning that next build worked at every step of the way without reporting any issues.

So I fixed the newly discovered issues between server/client components and then tried again to import the library from git instead of having the local code. But then I received again the error above with path (compiled file) not found. So I guess there must be more errors that are hidden by the nextjs/react compilation process.

Is there a way to get more detailed output so to understand why exactly nextjs build/webpack/react 19 are failing?

I tried NODE_OPTIONS='--inspect' but it doesn't give anything more.

@moltco
Copy link
Author

moltco commented Oct 4, 2024

I have now realised that there is a further discrepancy in importing packages from git with nextjs:

  1. If I run next dev -> this will fail at the first import of the package with the same TypeError ERR_INVALID_ARG_VALUE
  2. If I run next build && next start -> this works as expected, no issues with imports

I have tried adding "stuff" to next.config.mjs based on various posts with similar issues but none of this works - e.g I now have

  transpilePackages: ["simple-package"],
  webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
    config.module.rules.push({
      test: /\.(ts)x?$/, // Just `tsx?` file only
      use: [
        {
          loader: "ts-loader",
          options: {
            transpileOnly: true,
            experimentalWatchApi: true,
            onlyCompileBundledFiles: true,
          },
        },
      ],
    });

// Ensure proper resolution of .mjs files
    config.resolve = {
      ...config.resolve,
      extensions: [".js", ".jsx", ".ts", ".tsx", ".mjs"],
      alias: {
        ...config.resolve.alias,
        "simple-package": path.resolve(__dirname, "node_modules", "simple-package"),
      },
    };

    // Clear cache if necessary
    if (dev) {
      config.cache = false;
    }

Also, the folder which Webpack raises with TypeError does not exist with next dev; node_modules contains the package and it is compiled from ts to js correctly too.

I wonder what is the difference in the webpack options used between next dev and next build/start?

@PharaohMaster
Copy link

PharaohMaster commented Nov 29, 2024

Having the same issue when trying to use some git repo as dependency.

uncaughtException: TypeError: The argument 'path' must be a string, Uint8Array, or URL without null bytes. Received '/Users/code/test-repo/.next/server/vendor-chunks/@codeorg+ui@[email protected]+codeorg... code: 'ERR_INVALID_ARG_VALUE'

@dannywin25

This comment has been minimized.

@csonchen
Copy link

csonchen commented Dec 25, 2024

Having the same issue when trying to use some git repo as dependency.

uncaughtException: TypeError: The argument 'path' must be a string, Uint8Array, or URL without null bytes. Received '/Users/code/test-repo/.next/server/vendor-chunks/@codeorg+ui@[email protected]+codeorg... code: 'ERR_INVALID_ARG_VALUE'

got the same issue too when using pnpm

@nekinie
Copy link

nekinie commented Dec 26, 2024

effecting both the latest and canary with the following dependency

      '@primitivedotgg/proto':
        specifier: github:primitivedotgg/proto
        version: git+https://[email protected]:primitivedotgg/proto.git#212f7d4018719fedac32ef6d8fdd7910ee3dc501

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. Module Resolution Module resolution (CJS / ESM, module resolving).
Projects
None yet
Development

No branches or pull requests

5 participants