首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Netlify上部署next.js时无法解决此错误。导入图像和标签抛出错误

在Netlify上部署next.js时无法解决此错误。导入图像和标签抛出错误
EN

Stack Overflow用户
提问于 2022-06-24 10:14:18
回答 1查看 344关注 0票数 2

我尝试删除index.js行中的代码:4和行:15,然后部署到netlify上(正常工作,但图像没有编码,但我想要映像)。

现在,当我尝试这样做时,下面的代码。这让我犯了这个错误。我只写了一行:4和行:15。

这是我的Index.js

代码语言:javascript
复制
import Head from 'next/head'
import Image from 'next/image'
import styles from '../styles/Home.module.css'
import phd from "../images/phd.jpg";

export default function Home() {
  return (
    <div className={styles.container}>
      <Head>
        <title>Create Next App</title>
        <meta name="description" content="Generated by create next app" />
        <link rel="icon" href="/favicon.ico" />
      </Head>
      <div>
        <Image src={phd} alt='' width="500" height="400"></Image>
      </div>
    </div>
  )
}

这是我的app.js

代码语言:javascript
复制
import '../styles/globals.css'

function MyApp({ Component, pageProps }) {
  return <Component {...pageProps} />
}

export default MyApp

这就是我在netlify上面临的错误。

代码语言:javascript
复制
3:13:45 PM: ────────────────────────────────────────────────────────────────
3:13:45 PM:   1. @netlify/plugin-nextjs (onPreBuild event)                  
3:13:45 PM: ────────────────────────────────────────────────────────────────
3:13:45 PM: ​
3:13:45 PM: No Next.js cache to restore.
3:13:45 PM: Netlify configuration property "build.environment.NEXT_PRIVATE_TARGET" value changed.
3:13:45 PM: ​
3:13:45 PM: (@netlify/plugin-nextjs onPreBuild completed in 21ms)
3:13:45 PM: ​
3:13:45 PM: ────────────────────────────────────────────────────────────────
3:13:45 PM:   2. Build command from Netlify app                             
3:13:45 PM: ────────────────────────────────────────────────────────────────
3:13:45 PM: ​
3:13:45 PM: $ npm run build
3:13:46 PM: npm WARN config tmp This setting is no longer used.  npm stores temporary files in a special
3:13:46 PM: npm WARN config location in the cache, and they are managed by
3:13:46 PM: npm WARN config     [`cacache`](http://npm.im/cacache).
3:13:46 PM: > myapp@0.1.0 build
3:13:46 PM: > next build
3:13:46 PM: warn  - No build cache found. Please configure build caching for faster rebuilds. Read more: https://nextjs.org/docs/messages/no-cache
3:13:46 PM: info  - Checking validity of types...
3:13:48 PM: info  - Creating an optimized production build...
3:13:56 PM: Failed to compile.
3:13:56 PM: 
3:13:56 PM: ./images/phd.jpg
3:13:56 PM: Error: Call retries were exceeded
3:13:56 PM:     at ExperimentalWorker.initialize (/opt/build/repo/node_modules/next/dist/compiled/jest-worker/index.js:1:16721)
3:13:56 PM:     at ExperimentalWorker._onExit (/opt/build/repo/node_modules/next/dist/compiled/jest-worker/index.js:1:17655)
3:13:56 PM:     at Worker.emit (node:events:527:28)
3:13:56 PM:     at Worker.emit (node:domain:475:12)
3:13:56 PM:     at Worker.[kOnExit] (node:internal/worker:278:10)
3:13:56 PM:     at Worker.<computed>.onexit (node:internal/worker:198:20)
3:13:56 PM: Import trace for requested module:
3:13:56 PM: ./pages/index.js
3:13:56 PM: > Build failed because of webpack errors
3:13:56 PM: ​
3:13:56 PM: ────────────────────────────────────────────────────────────────
3:13:56 PM:   "build.command" failed                                        
3:13:56 PM: ────────────────────────────────────────────────────────────────
3:13:56 PM: ​
3:13:56 PM:   Error message
3:13:56 PM:   Command failed with exit code 1: npm run build (https://ntl.fyi/exit-code-1)
3:13:56 PM: ​
3:13:56 PM:   Error location
3:13:56 PM:   In Build command from Netlify app:
3:13:56 PM:   npm run build
3:13:56 PM: ​
3:13:56 PM:   Resolved config
3:13:56 PM:   build:
3:13:56 PM:     command: npm run build
3:13:56 PM:     commandOrigin: ui
3:13:56 PM:     environment:
3:13:56 PM:       - NEXT_PRIVATE_TARGET
3:13:56 PM:     publish: /opt/build/repo/.next
3:13:56 PM:     publishOrigin: ui
3:13:56 PM:   plugins:
3:13:56 PM:     - inputs: {}
3:13:56 PM:       origin: ui
3:13:56 PM:       package: '@netlify/plugin-nextjs'
3:13:56 PM: Caching artifacts
3:13:56 PM: Started saving node modules
3:13:56 PM: Finished saving node modules
3:13:56 PM: Started saving build plugins
3:13:56 PM: Finished saving build plugins
3:13:56 PM: Started saving pip cache
3:13:56 PM: Finished saving pip cache
3:13:56 PM: Started saving emacs cask dependencies
3:13:56 PM: Finished saving emacs cask dependencies
3:13:56 PM: Started saving maven dependencies
3:13:56 PM: Finished saving maven dependencies
3:13:56 PM: Started saving boot dependencies
3:13:56 PM: Finished saving boot dependencies
3:13:58 PM: Creating deploy upload records
3:13:56 PM: Started saving rust rustup cache
3:13:56 PM: Finished saving rust rustup cache
3:13:56 PM: Started saving go dependencies
3:13:56 PM: Finished saving go dependencies
3:13:58 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
3:13:58 PM: Failing build: Failed to build site
3:13:58 PM: Failed during stage 'building site': Build script returned non-zero exit code: 2 (https://ntl.fyi/exit-code-2)
3:13:58 PM: Finished processing build request in 43.146759075s```
EN

回答 1

Stack Overflow用户

发布于 2022-06-24 10:27:54

Next.js为静态文件服务提供“公用”文件夹。你可以找到细节这里。将您的图像放在公用文件夹中,并按以下方式更改index.js:

代码语言:javascript
复制
import Head from 'next/head'
import Image from 'next/image'
import styles from '../styles/Home.module.css'

export default function Home() {
  return (
      <div className={styles.container}>
        <Head>
          <title>Create Next App</title>
          <meta name="description" content="Generated by create next app" />
          <link rel="icon" href="/favicon.ico" />
        </Head>
        <div>
          <Image src='/phd.jpg' alt='' width="500" height="400"></Image>
        </div>
      </div>
  )
}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72742487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档