-
Notifications
You must be signed in to change notification settings - Fork 27.5k
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
TypeError: Cannot use 'in' operator to search for '__next_img_default' in undefined #63803
Comments
But why this test pass? Next.js version for test environment : Reprohttps://github.com/ojj1123/next-custom-loader-issue-repro/tree/with-next-canary So I try to upgrade nextjs version to
|
I found the reason. https://github.com/ojj1123/next-custom-loader-issue-repro/tree/export-default-loader diff --git a/custom-loader.ts b/custom-loader.ts
index 461b1e3..d0af4f9 100644
--- a/custom-loader.ts
+++ b/custom-loader.ts
@@ -2,6 +2,10 @@
import { ImageLoaderProps } from 'next/image';
-export function customLoader({ src, width, quality }: ImageLoaderProps) {
+export default function customLoader({
+ src,
+ width,
+ quality,
+}: ImageLoaderProps) {
return `${src}?url=${encodeURIComponent(src)}&w=${width}&q=${quality || 75}`;
} SuggestionBTW, How about supporting a |
Hi, I'd say it is similar to how the |
Yeah, I agree with you. next.js/packages/next/src/build/create-compiler-aliases.ts Lines 112 to 119 in b434ea4
next.js/packages/next/src/shared/lib/image-external.tsx Lines 7 to 8 in 29d53c8
But I think it would be good to warn users not to use named exports. Is it possible? |
Yeah, an accurate error message is always preferred. Mind if I open a PR? Feel free to open one if you'd like to! |
Oh, so Could I try to PR? I want to investigate and resolve this issue! |
Yeah, for sure! It's up to you if you'd like to contribute. Ping me if you want to discuss anything. 😄 |
UPDATE I try to resolve this issue but I want to discuss about this issue. 1. During runtimeAfter build ( import defaultLoader from '.....'
if(typeof defaultLoader === 'undefined') {
throw new Error('you should export the loader function as default');
} It was working well. But I thought it's a bit hacky, because if doing that, I write that code wherever Next.js import 2. During buildtimeI try to make the custom webpack loader for allowing our to use a default function. // /webpack/loader/next-image-loader-file-loader.ts
const nextImageLoaderFileLoader: webpack.LoaderDefinitionFunction = function (
this,
source
) {
const hasDefaultExport = /export default/.test(source)
if (!hasDefaultExport) {
throw new Error('you should export the loader function as default')
}
return source
}
// webpack.config.ts
...
module: {
rules: [
...(config.images.loaderFile
? [
{
test: config.images.loaderFile,
use: ['next-image-loader-file-loader'],
},
]
: []),
]
}
... It was throwing the error well. But I thought it has some edge case: function customLoader() {...}
export default customLoader; // correct
export customLoader; // throw error
export { customLoader as default } // throw error.. but this case is also default export so it should have not to throw the error!! It's a bit difficult to check whether or not default function during either runtime or buildtime. |
…efault function (#64036) <!-- 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 ### 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 --> fixes #63803 ## What I do? - If the loader file export the function as `named`, Next.js throws the error. - But this error is a bit confusing for the developers. - So I open this PR for showing the accurate error message. ## AS-IS / TO-BE ### AS-IS ``` TypeError: Cannot use 'in' operator to search for '__next_img_default' in undefined ``` <img width="1202" alt="스크린샷 2024-03-28 16 10 53" src="https://github.com/vercel/next.js/assets/33178048/e7c81cb5-7976-46ff-b86f-9c8fd9a7a681"> ### TO-BE ``` Error: The loader file must export a default function that returns a string. See more info here: https://nextjs.org/docs/messages/invalid-images-config ``` <img width="500" alt="스크린샷 2024-03-28 16 10 53" src="https://github.com/vercel/next.js/assets/33178048/c391e61b-6a44-4f85-8600-28ab6cb5b0eb"> --------- Co-authored-by: Steven <[email protected]>
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. |
Link to the code that reproduces this issue
https://github.com/ojj1123/next-custom-loader-issue-repro
To Reproduce
Current vs. Expected behavior
Expected behavior
I want to configure the image loader globally.
Nextjs docs describes that I can configure the image loader at
next.config.js
:Current
But currently, nextjs occurs this error:
It is shown at either App router or Page router
Provide environment information
Operating System: Platform: darwin Arch: arm64 Version: Darwin Kernel Version 23.1.0: Mon Oct 9 21:28:12 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T8103 Binaries: Node: 18.19.0 npm: 10.2.3 Yarn: 1.22.21 pnpm: 8.15.4 Relevant Packages: next: 14.1.4 eslint-config-next: 14.1.4 react: 18.2.0 react-dom: 18.2.0 typescript: 5.4.3 Next.js Config: output: N/A
Which area(s) are affected? (Select all that apply)
App Router, Image optimization (next/image, next/legacy/image)
Which stage(s) are affected? (Select all that apply)
next dev (local), next build (local)
Additional context
This error occurs at
getImgProps
next.js/packages/next/src/shared/lib/get-img-props.ts
Lines 286 to 294 in 0c21ff7
rest.loader
is what the user passloader
function at Image'sloader
prop. But I didn't pass the custom loader atImage
component so then the value ofloader
variable above would bedefaultLoader
.__next_img_default
property is only indefaultLoader
for checking whether or not the user pass the customloader
. I don't know why the value isundefined
Update
If setting
images.loaderFile
atnext.config.js
,defaultLoader
module is resolved asconfig.images.loaderFile
during build. So we have to export loader function as default. #63803 (comment)next.js/packages/next/src/build/create-compiler-aliases.ts
Lines 112 to 119 in b434ea4
Nextjs internally import/export
defaultLoader
as a default functionnext.js/packages/next/src/shared/lib/image-external.tsx
Lines 7 to 8 in 29d53c8
After build, the build assets is like:
Since custom loader is exported as a named,
defaultLoader.default
isundefined
The text was updated successfully, but these errors were encountered: