简体   繁体   English

在 Heroku 上部署的 React Portfolio 不显示图像

[英]Deployed React Portfolio on Heroku does not display images

I have deployed my portfolio on Heroku, https://tomdoan.herokuapp.com/我已经在 Heroku 上部署了我的投资组合, https: //tomdoan.herokuapp.com/

On the local machine the images display fine, so I assume for now the path set in Webpack is correct在本地机器上,图像显示正常,所以我现在假设 Webpack 中设置的路径是正确的

Deployed on Heroku, it seems their production machine will not recognize the path.部署在 Heroku 上,他们的生产机器似乎无法识别路径。

I am new to React / Deployment我是 React / Deployment 的新手

In the console, it seems heroku does not recognize the path I have set, I am thinking / assuming the production server might have changed the path在控制台中,似乎 heroku 无法识别我设置的路径,我在想/假设生产服务器可能已经更改了路径

Would really appreciate some guidance, thank you非常感谢您的指导,谢谢

// package.json file

{
"name": "portfolio",
"version": "0.1.0",
"private": true,
"dependencies": {
"express": "^4.17.1",
"history": "^4.9.0",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router-dom": "^5.0.0",
"react-scripts": "3.0.1",
"webpack": "^4.34.0",
"webpack-cli": "^3.3.4"
},
"engines": {
"node": "10.16.0",
"npm": "6.9.0"
},
"scripts": {
"start": "webpack && node index.js",
"build": "webpack"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
  ">0.2%",
  "not dead",
  "not op_mini all"
],
"development": [
  "last 1 chrome version",
  "last 1 firefox version",
  "last 1 safari version"
]
},
"description": "This project was bootstrapped with [Create React 
App](https://github.com/facebook/create-react-app).",
"main": "index.js",
"devDependencies": {
"@babel/plugin-proposal-class-properties": "^7.4.4",
"file-loader": "^4.0.0",
"image-webpack-loader": "^5.0.0",
"ttf-loader": "^1.0.2",
"url-loader": "^2.0.0"
},
"keywords": [],
"author": "",
"license": "ISC"
}

// webpack.config.js file // webpack.config.js 文件

const path = require('path')

   module.exports = {
   entry: './src/index.js',
   output: {
   path: path.join(__dirname, 'public', "js"),
   filename: 'bundle.js'
   },
   mode: 'development',
   module: {
    rules: [
      {
        loader: 'babel-loader',
        test: /\.jsx?$/,
        exclude: /node_modules/
      },
      {
        loader: 'style-loader!css-loader',
        test: /\.css$/
      },
      {
        test: /\.(svg|png|jpg|gif)$/,
                use: {
                 loader: "file-loader",
                 options: {
                     name:"[name].[ext]",
                     outputPath: "images"
                 } 
               }
        // test: /\.(gif|png|jpe?g|svg)$/i,
        //   use: [
        //     'file-loader?name=[name]. 
        // [ext]&outputPath=js/js/assets/',
        //     {
        //       loader: 'image-webpack-loader',
        //       options: {
        //         bypassOnDebug: true, // webpack@1.x
        //         disable: true, // webpack@2.x and newer
        //       },
        //     }]
          },
          {
            test: /\.ttf$/,
            use: [
              {
                loader: 'ttf-loader',
                options: {
                  name: './font/[hash].[ext]',
                  outputPath: "font"
                },
              },
            ]
        }
    ]
    },
    resolve: {
    extensions: ['.js', '.jsx']
    },
    devtool: 'source-map'
    }

When importing images into React, we usually rely on Webpack to copy the appropriate files over, rather than having to reference a public directory, eg 将图像导入React时,我们通常依靠Webpack复制相应的文件,而不必引用公共目录,例如

src
    components
        MyComponent
            index.jsx
            image.png

In my index.jsx file, I'd simply import the image like so: 在我的index.jsx文件中,我只是像这样导入图像:

import myImage from './image.png';

Finally, and most importantly, you need to update your Webpack config to include a loader for your images if doesn't already include one, such as file-loader . 最后,最重要的是,您需要更新Webpack配置,以便为图像添加加载器(如果尚未包含file-loader ,例如file-loader

See this question/answer for more details on the process: How to import image (.svg, .png ) in a React Component 有关该过程的更多详细信息,请参阅此问题/答案: 如何在React组件中导入图像(.svg,.png)

I solved this problem by reconfigure my webpack: 我通过重新配置我的webpack解决了这个问题:

My previous output path in Webpack was 我之前在Webpack中的输出路径是

module.exports = {
   entry: './src/index.js',
   output: {
   path: path.join(__dirname, 'public', "js"),
   filename: 'bundle.js'.
}

That created a js folder directory nested inside public folder. 这创建了一个嵌套在公共文件夹中的js文件夹目录。 I suspect that did not gel well with Heroku production machine. 我怀疑Heroku生产机器不能很好地凝胶。

THE SOLUTION: 解决方案:

module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'public'),
  }.

I deleted the extra js folder directory and just have all assets output directly inside of the public folder. 我删除了额外的js文件夹目录,只是将所有资源直接输出到公用文件夹中。 Instead of using path.join(__dirname, "public"), I resulted to using path.resolve(__dirname, "public") 我没有使用path.join(__ dirname,“public”),而是使用了path.resolve(__ dirname,“public”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 部署到 heroku 时,React 应用程序不显示任何内容 - React app does not display anything when deployed to heroku 有人知道我将bootstrap主题的产品组合部署到heroku时为什么会搞砸吗? - Does anyone know why the styling on my bootstrap themed portfolio got messed up when I deployed it to heroku? 无法浏览部署到Heroku的React应用程序 - Failing to browse React application deployed to Heroku 部署到Heroku时React应用程序崩溃 - React App Crashes When Deployed to Heroku 反应 - 无法在 Heroku 上渲染图像 - React - Unable to render images on Heroku 为什么react-grid-gallery两次显示我的图像? - Why does react-grid-gallery display my images twice? React 在部署到 gh-pages 时不显示放置在公共文件夹中的图像,但可以在本地主机中运行,为什么? - React doesn't display images placed in public folder when deployed to gh-pages but works in local host, why? Heroku-Express和React应用程序在部署时显示空白屏幕 - Heroku - Express and React app showing blank screen when deployed 无法使用部署在 Heroku 上的 React App 读取未定义的属性(读取“findOne”) - Cannot read properties of undefined (reading 'findOne') with React App deployed on Heroku 部署到 heroku 时,texpress-session 未在反应应用程序中设置 cookie - texpress-session not setting cookie in react app when deployed to heroku
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM