简体   繁体   中英

Nextjs : typescript next/image

I'm aware that when using nextjs image components without typescript, the url needs to be configured in next.config.js. but i'm not sure why this doesn't work with typescript.

"...., is not configured under images in your next.config.js See more info: https://nextjs.org/docs/messages/next-image-unconfigured-host"

what's the workaround? or is my only option to use a regular img tag?

  • List item

image component:

  <Image
        src="https://images.unsplash.com/photo-1621794279356-0150106a4b45?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1"
        alt="blog title"
        width="100"
        height="100"
      /> 

next.config.ts

export const images = {
  domains: [
    "images.unsplash.com"
  ],
};

I think you should better use next.config.js for configuring next instead of next.config.ts in typescript project. This should be the only file to be in .js .

If you insist to use next.config.ts , you can refer to this issue .

But again, some may slow down the boot up time, and some still require you to have next.config.js alongside with next.config.ts

Next.js will ultimately look for a next.config.js file, which you probably don't have.

Either you compile your .ts config to a .js config, or simply create a next.config.js file instead.

module.exports = {
    images: {
        domains: ['images.unsplash.com']
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM