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

Module not found: Can't resolve './_next/static/media/b1db3e28af9ef94a-s.woff2' After set assetPrefix to '.', #47641

Closed
1 task done
coderup2021 opened this issue Mar 29, 2023 · 10 comments · Fixed by #49403
Closed
1 task done
Labels
bug Issue was opened via the bug report template. Developer Experience Issues related to Next.js logs, Error overlay, etc. Font (next/font) Related to Next.js Font Optimization.

Comments

@coderup2021
Copy link

coderup2021 commented Mar 29, 2023

Verify canary release

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

Provide environment information

❯ pnpm next info

    Operating System:
      Platform: darwin
      Arch: x64
      Version: Darwin Kernel Version 19.6.0: Sun Nov 14 19:58:51 PST 2021; root:xnu-6153.141.50~1/RELEASE_X86_64
    Binaries:
      Node: 16.18.1
      npm: 8.19.2
      Yarn: 1.22.19
      pnpm: 7.17.1
    Relevant packages:
      next: 13.2.4
      eslint-config-next: 13.2.4
      react: 18.2.0
      react-dom: 18.2.0

warn  - Latest canary version not detected, detected: "13.2.4", newest: "13.2.5-canary.20".
        Please try the latest canary version (`npm install next@canary`) to confirm the issue still exists before creating a new issue.
        Read more - https://nextjs.org/docs/messages/opening-an-issue

Which area(s) of Next.js are affected? (leave empty if unsure)

No response

Link to the code that reproduces this issue

no

To Reproduce

pnpm create next-app (with default options)

modify next.config.js as following

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  assetPrefix: '.',    // or './'
}

module.exports = nextConfig

pnpm build

then will get following error

pnpm build

> next-test@0.1.0 build /Users/lj/Study/web3.0/next-test
> next build

info  - Linting and checking validity of types  
Failed to compile.

src/pages/index.tsx
Module not found: Can't resolve './_next/static/media/d1d9458b69004127-s.woff2'

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

Import trace for requested module:
./node_modules/.pnpm/next@13.2.4_biqbaboplfbrettd7655fr4n2y/node_modules/next/font/google/target.css?{"path":"src/pages/index.tsx","import":"Inter","arguments":[{"subsets":["latin"]}],"variableName":"inter"}
./src/pages/index.tsx

src/pages/index.tsx
Module not found: Can't resolve './_next/static/media/b967158bc7d7a9fb-s.woff2'

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

Import trace for requested module:
./node_modules/.pnpm/next@13.2.4_biqbaboplfbrettd7655fr4n2y/node_modules/next/font/google/target.css?{"path":"src/pages/index.tsx","import":"Inter","arguments":[{"subsets":["latin"]}],"variableName":"inter"}
./src/pages/index.tsx

src/pages/index.tsx
Module not found: Can't resolve './_next/static/media/ae9ae6716d4f8bf8-s.woff2'

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

Import trace for requested module:
./node_modules/.pnpm/next@13.2.4_biqbaboplfbrettd7655fr4n2y/node_modules/next/font/google/target.css?{"path":"src/pages/index.tsx","import":"Inter","arguments":[{"subsets":["latin"]}],"variableName":"inter"}
./src/pages/index.tsx

src/pages/index.tsx
Module not found: Can't resolve './_next/static/media/c0f5ec5bbf5913b7-s.woff2'

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

Import trace for requested module:
./node_modules/.pnpm/next@13.2.4_biqbaboplfbrettd7655fr4n2y/node_modules/next/font/google/target.css?{"path":"src/pages/index.tsx","import":"Inter","arguments":[{"subsets":["latin"]}],"variableName":"inter"}
./src/pages/index.tsx

src/pages/index.tsx
Module not found: Can't resolve './_next/static/media/b1db3e28af9ef94a-s.woff2'

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

Import trace for requested module:
./node_modules/.pnpm/next@13.2.4_biqbaboplfbrettd7655fr4n2y/node_modules/next/font/google/target.css?{"path":"src/pages/index.tsx","import":"Inter","arguments":[{"subsets":["latin"]}],"variableName":"inter"}
./src/pages/index.tsx


> Build failed because of webpack errors
info  - Creating an optimized production build . ELIFECYCLE  Command failed with exit code 1.

Describe the Bug

I am not sure what happend, please help

Expected Behavior

expected pnpm build can work ok with assetPrefix config

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

@coderup2021 coderup2021 added the bug Issue was opened via the bug report template. label Mar 29, 2023
@coderup2021 coderup2021 changed the title Module not found: Can't resolve './_next/static/media/b1db3e28af9ef94a-s.woff2' After set assetPrefix to .', Module not found: Can't resolve './_next/static/media/b1db3e28af9ef94a-s.woff2' After set assetPrefix to '.', Mar 29, 2023
@JesseKoldewijn
Copy link
Contributor

You're missing the part where you add this woff2 font. Could you maybe link a repo or something like that where we could actually see the project's code to diagnose this issue for ya?

@balazsorban44 balazsorban44 added the please add a complete reproduction The issue lacks information for further investigation label Mar 30, 2023
@vercel vercel deleted a comment from github-actions bot Mar 30, 2023
@balazsorban44 balazsorban44 removed the please add a complete reproduction The issue lacks information for further investigation label Mar 30, 2023
@balazsorban44
Copy link
Member

Hmm. It looks like the repro steps given are enough to reproduce. (The default template uses next/font)

@balazsorban44 balazsorban44 added the Font (next/font) Related to Next.js Font Optimization. label Mar 30, 2023
@balazsorban44
Copy link
Member

After some digging, I believe this is intentionally not working. The assetPrefix is expected to be a full URL or an absolute path, and not start with a .. This is how it is documented as well: https://nextjs.org/docs/api-reference/next.config.js/cdn-support-with-asset-prefix

So I think we just need a better error message and clarify the docs.

This will be invalid for other use cases like `next/image as well, but that component will already surface this via the following error message:

image

@balazsorban44 balazsorban44 added area: documentation Developer Experience Issues related to Next.js logs, Error overlay, etc. labels Apr 4, 2023
@JesseKoldewijn
Copy link
Contributor

After some digging, I believe this is intentionally not working. The assetPrefix is expected to be a full URL or an absolute path, and not start with a .. This is how it is documented as well: https://nextjs.org/docs/api-reference/next.config.js/cdn-support-with-asset-prefix

So I think we just need a better error message and clarify the docs.

This will be invalid for other use cases like `next/image as well, but that component will already surface this via the following error message:

image

Well in my case I programatically import the image file and throw it into a Image component. It works on deployments to vercel and in dev but it breaks on static.

@balazsorban44
Copy link
Member

Well in my case I programatically import the image file and throw it into a Image component. It works on deployments to vercel and in dev but it breaks on static.

Can you clarify this part? What do you mean by "programmatically importing"? Maybe you can attach a reproduction?

The above error is shown when you import the image in the module scope and pass it to the src prop, this is what triggers the image loader to emit the files in the static folder.

const interpolatedName = loaderUtils.interpolateName(
this,
'/static/media/[name].[hash:8].[ext]',
opts
)
const outputPath = assetPrefix + '/_next' + interpolatedName

next/font has the same path generation:

const interpolatedName = loaderUtils.interpolateName(
this,
`static/media/[hash]${isUsingSizeAdjust ? '-s' : ''}${
preload ? '.p' : ''
}.${ext}`,
opts
)
const outputPath = `${assetPrefix}/_next/${interpolatedName}`

It just does not verify invalid URLs later on, as next/image does:

if (src.startsWith('//')) {
throw new Error(
`Failed to parse src "${src}" on \`next/image\`, protocol-relative URL (//) must be changed to an absolute URL (http:// or https://)`
)
}
if (!src.startsWith('/') && (config.domains || config.remotePatterns)) {
let parsedSrc: URL
try {
parsedSrc = new URL(src)
} catch (err) {
console.error(err)
throw new Error(
`Failed to parse src "${src}" on \`next/image\`, if using relative image it must start with a leading slash "/" or be an absolute URL (http:// or https://)`
)
}

@JesseKoldewijn
Copy link
Contributor

Well in my case I programatically import the image file and throw it into a Image component. It works on deployments to vercel and in dev but it breaks on static.

Can you clarify this part? What do you mean by "programmatically importing"? Maybe you can attach a reproduction?

The above error is shown when you import the image in the module scope and pass it to the src prop, this is what triggers the image loader to emit the files in the static folder.

const interpolatedName = loaderUtils.interpolateName(
this,
'/static/media/[name].[hash:8].[ext]',
opts
)
const outputPath = assetPrefix + '/_next' + interpolatedName

next/font has the same path generation:

const interpolatedName = loaderUtils.interpolateName(
this,
`static/media/[hash]${isUsingSizeAdjust ? '-s' : ''}${
preload ? '.p' : ''
}.${ext}`,
opts
)
const outputPath = `${assetPrefix}/_next/${interpolatedName}`

It just does not verify invalid URLs later on, as next/image does:

if (src.startsWith('//')) {
throw new Error(
`Failed to parse src "${src}" on \`next/image\`, protocol-relative URL (//) must be changed to an absolute URL (http:// or https://)`
)
}
if (!src.startsWith('/') && (config.domains || config.remotePatterns)) {
let parsedSrc: URL
try {
parsedSrc = new URL(src)
} catch (err) {
console.error(err)
throw new Error(
`Failed to parse src "${src}" on \`next/image\`, if using relative image it must start with a leading slash "/" or be an absolute URL (http:// or https://)`
)
}

I got my images in a images folder in the src dir. I import them from that dir in my code and then dump the image into a Image component. My repo is located in https://github.com/ShiftCodeEU/shiftcode.eu

@balazsorban44
Copy link
Member

This seems to be unrelated to the original issue though since I cannot see an assetPrefix config. (I also see output: "export" commented out/third-party tool generating the output, maybe it should be reported as a separate issue.)

@JesseKoldewijn
Copy link
Contributor

This seems to be unrelated to the original issue though since I cannot see an assetPrefix config. (I also see output: "export" commented out/third-party tool generating the output, maybe it should be reported as a separate issue.)

You're right that I'm not using the asset prefix. I did have the output export enabled while testing which is why it's commented out. As far as I know and from my experience with the pages dir I was under the impression it would still work the same as it does with importing images ending up in the _next/static dir.

@koba04
Copy link
Contributor

koba04 commented May 7, 2023

@balazsorban44 I've created a PR to improve the error message #49403

ijjk added a commit that referenced this issue May 8, 2023
fixes #47641

This adds a friendly error for loading a font error caused by the
invalid `assetPrefix` setting.

Current:
<img width="747" alt="image"
src="https://user-images.githubusercontent.com/250407/236685607-c03b6160-9e8c-4c88-80e1-714f6a140588.png">


This PR:
<img width="890" alt="image"
src="https://user-images.githubusercontent.com/250407/236685499-da4c3d69-0f97-458d-8709-dcc43475e0e9.png">


<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation or adding/fixing Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md



## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->

---------

Co-authored-by: JJ Kasper <jj@jjsweb.site>
@github-actions
Copy link
Contributor

github-actions bot commented Jun 8, 2023

This closed issue has been automatically locked because it had no new activity for a month. 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 Jun 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. Developer Experience Issues related to Next.js logs, Error overlay, etc. Font (next/font) Related to Next.js Font Optimization.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants