Skip to content
New issue

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

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

Already on GitHub? # to your account

Cannot find module '@next/third-parties/google' when trying to import GoogleTagManager #58697

Closed
1 task done
renanleonel opened this issue Nov 20, 2023 · 20 comments · Fixed by #59244 or #59503
Closed
1 task done
Assignees
Labels
bug Issue was opened via the bug report template. locked Module Resolution Module resolution (CJS / ESM, module resolving). TypeScript Related to types with Next.js.

Comments

@renanleonel
Copy link

Link to the code that reproduces this issue

https://codesandbox.io/p/devbox/musing-solomon-f4tnqz?file=%2Fapp%2Flayout.tsx%3A8%2C11

To Reproduce

  1. Start application with next dev
  2. Check layout.tsx
  3. import error Cannot find module '@next/third-parties/google' or its corresponding type declarations.typescript(2307)

Current vs. Expected behavior

Expected: import work as intended in documentation https://nextjs.org/docs/app/building-your-application/optimizing/third-party-libraries#google-tag-manager

Current: import not working
image

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Node: v20.9.0
Next: 14.0.3,
React: 8.2.0
react-dom: 18.2.0,
npm: 10.1.0

Windows 11

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

App Router

Additional context

Tested using next/canary, next v14 and nextv13, all with same problems.

after changing the import to import { GoogleTagManager } from '@next/third-parties/dist/google', the error goes away but when building the application i got an error because there is no exported module in node_modules@next\third-parties\dist\google.

package.json

image

Error message:

Failed to compile.
./src/app/layout.tsx
Module not found: Package path ./dist/google is not exported from package /tmp/build_6a78ac44/node_modules/@next/third-parties (see exports field in /tmp/build_6a78ac44/node_modules/@next/third-parties/package.json)

@renanleonel renanleonel added the bug Issue was opened via the bug report template. label Nov 20, 2023
@djshubs
Copy link

djshubs commented Nov 20, 2023

I have the same error.

@roryf
Copy link

roryf commented Nov 21, 2023

Although my editor highlights the error, it works as expected in development server, but build command fails locally and on vercel.

@djshubs
Copy link

djshubs commented Nov 21, 2023

Although my editor highlights the error, it works as expected in development server, but build command fails locally and on vercel.

I added the following in my tsconfig under paths, and its allowing me to build locally. Yet to test remote.

"@next/third-parties/google": [ "./node_modules/@next/third-parties/dist/google" ]

@AntonyJudeShaman
Copy link

Had the same issue with this import { GoogleTagManager } from '@next/third-parties/google'

Use npm install @next/third-parties or npm install @next/third-parties based on your package manager.

Now import the module GoogleTagManager and it lets you use it without any error in both local development and production.
It worked fine for me when i pushed it to Vercel

@kasyap-007
Copy link

kasyap-007 commented Nov 28, 2023

This is due to the typescript's module resolution strategy. In typescript 5.0 new option was introduced and has been the recommended way if we use bundler's, new create-next-app uses this by default

update tsconfig.json to use "moduleResolution": "Bundler",

@z0n
Copy link
Contributor

z0n commented Dec 4, 2023

update tsconfig.json to use "moduleResolution": "Bundler",

That does indeed fix GTM but unfortunately breaks a different module import. Is this something package authors need to fix?

@djshubs
Copy link

djshubs commented Dec 4, 2023

update tsconfig.json to use "moduleResolution": "Bundler",

That does indeed fix GTM but unfortunately breaks a different module import. Is this something package authors need to fix?

I have Combobox from headlessui breaking because of it.

@huozhi
Copy link
Member

huozhi commented Dec 5, 2023

We fixed it in v14.0.4-canary.40, please upragde!

@paqstd-dev
Copy link

Still not working in 14.0.4.

Caution

Cannot find module '@next/third-parties/google' or its corresponding type declarations.

@renanleonel
Copy link
Author

Still not working in 14.0.4.

Caution

Cannot find module '@next/third-parties/google' or its corresponding type declarations.

added this to my tsconfig.json, under paths

"paths": { "@next/third-parties/google": [ "./node_modules/@next/third-parties/dist/google" ] }

and importing on layout.tsx

import { GoogleTagManager } from '@next/third-parties/google';

currently working on "next": "^14.0.4",

@paqstd-dev
Copy link

Still not working in 14.0.4.
Caution
Cannot find module '@next/third-parties/google' or its corresponding type declarations.

added this to my tsconfig.json, under paths

"paths": { "@next/third-parties/google": [ "./node_modules/@next/third-parties/dist/google" ] }

and importing on layout.tsx

import { GoogleTagManager } from '@next/third-parties/google';

currently working on "next": "^14.0.4",

Yes, that's what I did. That helped. Thank you.
But why not add this to the documentation then? This looks like a local crutch, but not a global one for everyone. Cause I'd like to use it out of the box

@renanleonel
Copy link
Author

Still not working in 14.0.4.
Caution
Cannot find module '@next/third-parties/google' or its corresponding type declarations.

added this to my tsconfig.json, under paths
"paths": { "@next/third-parties/google": [ "./node_modules/@next/third-parties/dist/google" ] }
and importing on layout.tsx
import { GoogleTagManager } from '@next/third-parties/google';
currently working on "next": "^14.0.4",

Yes, that's what I did. That helped. Thank you. But why not add this to the documentation then? This looks like a local crutch, but not a global one for everyone. Cause I'd like to use it out of the box

i agree, tried without this tsconfig.json and didn't work. hope they'll fix it soon

@DevYemi
Copy link

DevYemi commented Dec 20, 2023

14.0.4-canary.40

Hi there, i upgraded to v14.0.4-canary.40 and I'm still getting the same error

@danwild
Copy link

danwild commented Dec 21, 2023

Noting this is a of #58458 (closed)

@ryancwalsh
Copy link

#58697 (comment) helped me with my static site. Thanks.

@huozhi huozhi added the Module Resolution Module resolution (CJS / ESM, module resolving). label Jan 2, 2024
@V-iktor
Copy link

V-iktor commented Jan 9, 2024

Not fixed in 14.0.4

Although my editor highlights the error, it works as expected in development server, but build command fails locally and on vercel.

I added the following in my tsconfig under paths, and its allowing me to build locally. Yet to test remote.

"@next/third-parties/google": [ "./node_modules/@next/third-parties/dist/google" ]

This works for me

@ETOPS7
Copy link

ETOPS7 commented Jan 9, 2024

I added the following in my tsconfig under paths, and its allowing me to build locally. Yet to test remote.
"@next/third-parties/google": [ "./node_modules/@next/third-parties/dist/google" ]

This works for me

It helps me too (build as well), but I guess crutch. Good to wait for official solution.

@isaacdarcilla
Copy link

Although my editor highlights the error, it works as expected in development server, but build command fails locally and on vercel.

I added the following in my tsconfig under paths, and its allowing me to build locally. Yet to test remote.

"@next/third-parties/google": [ "./node_modules/@next/third-parties/dist/google" ]

This works for me "@next/third-parties": "^14.0.4",

@Evavic44
Copy link
Contributor

As of the latest canary release, you no longer need the tsconfig hack, simply upgrade @next/third-parties to the latest canary version

npm i @next/third-parties@14.0.5-canary.59

And then imports should work as expected:

import { GoogleAnalytics } from "@next/third-parties/google";

Tested and working on next 14.0.4

Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 31, 2024
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
bug Issue was opened via the bug report template. locked Module Resolution Module resolution (CJS / ESM, module resolving). TypeScript Related to types with Next.js.
Projects
None yet