I am new to Gatsby. I was able to get a site working on Netlify and need help with with swapping out the logo. I did find this response from another question/post helpful in understanding how Gatsby works but the code I submitted based on the other response didn't work in my case.
gatsbyjs, reactjs -images do not appear?
** summary of response to link above ***
For your image not showing up, this is why: Everything in your src folder is meant to be dynamic, meaning it won't be served directly. If you want to include image statically, you can create a public folder in your root directory (at the same level with src folder), and put images in there. Anything in this public folder will be served directly. So for example, you can have this structure:
|-- src
`-- public
`-- images
`-- logo.png
Then in your code, you can include the path like
<img src="/images/logo.png" alt="logo" />
1) Github repository:
egatsby-starter-netlify-cms
2) Site URL
3) Code Changes: SRC/Navbar.js (this is where I believe the header/logo code is located)
-- the last line is what I added and the line above it is the original (commented out)
import React from 'react'
import { Link } from 'gatsby'
import github from '../img/github-icon.svg'
{/* import logo from '../img/logo.svg' */}
import logo from '../image/logo.png'
4) Error message when I deployed to Netlify
1:02:18 PM: success Generating image thumbnails - 0.780s - 17/17 21.80/s
1:03:29 PM: error "gatsby-plugin-netlify-cms" threw an error while running the onCreateWebpackConfig lifecycle:
1:03:29 PM: ModuleNotFoundError: Module not found: Error: Can't resolve '../image/logo.png' in '/opt/build/repo/src/components'
1:03:29 PM: error UNHANDLED REJECTION Converting circular structure to JSON
1:03:29 PM:
1:03:29 PM: TypeError: Converting circular structure to JSON
1:03:29 PM:
1:03:29 PM: - JSON.stringify
Any suggestions would be greatly appreciated.
This is the wrong path i.e. "/" means project root directory:
<img src="/images/logo.png" alt="logo" />
compared to this which means "go up one directory from current directory of this source file":
import logo from '../image/logo.png'
Do this:
// Use an IDE to check if this path is valid and points to the right file
import logo from '../image/logo.png'
// ...
// refer to the imported logo in the source
<img src={logo} alt="logo" />
The build error on Netlify seem to be unrelated to your image chainging issue.
1:03:29 PM: error "gatsby-plugin-netlify-cms" threw an error while running the onCreateWebpackConfig lifecycle:
1:03:29 PM: ModuleNotFoundError: Module not found: Error: Can't resolve '../image/logo.png' in '/opt/build/repo/src/components'
The "gatsby-plugin-netlify-cms" plugin is throwing an error because it expects a file to be somewhere in the netlify cms. But you have that file directly in your git project. You don't need to have the image BOTH in your git project and the CMS. Pick one location.
The Gatsby starters can be quite overwhelming for someone who is just starting out with gatsby. At least I felt this way. My suggestion: Start from the basics, then add complexity step by step.
Use the basic gatsby starter from the tutorial
Change the image there, test if publish to netlify is successful
add the netlify cms plugin manually to this basic starter
Learn about how to integrate plugins correctly
Change the image to netlify cms, test if publish to netlify is successful
Now you understand each step towards your problem. Now use the complicated gatsby-starter-netlify-cms and change the code there.
I was able to get the image (logo) changed. I used Illustrator to create a .svg file and then just uploaded it to Github (renamed it logo2.svg) and changed the name in the Navbar.js file
import React from 'react'
import { Link } from 'gatsby'
import github from '../img/github-icon.svg'
import logo from '../img/logo2.svg'
However, the new image (logo) is too small and I don't know enough about .svg or other settings to get it to show properly.
Here is the image I uploaded into Git and how it shows using Chrome inspect
enter image description here
Here is how the original image looks in Chrome inspect
enter image description here
Just thought it might help others. Unfortunately I am not well versed with .svg files. It would be great if there was a simpler, more elegant option - i.e. using a static .png file.
There is another snippet of code that defines the logo width but when I changed it to "200" (same width as new logo) it caused the header to widen too far (created a large vertical whitespace).
div className="navbar-brand">
<Link to="/" className="navbar-item" title="Logo">
<img src={logo} alt="MetaMedia" style={{ width: '88px' }} />
Related
I have upgraded to the latest Create React App 4.0. Now the scss cannot resolve image assets in the public folder. I was using CRA 3.4.1 before. It worked fine.
Any ideas? I don't want to use npm eject
The icon.svg is in public/images
background-image: url(/images/icon.svg);
Failed to compile.
./src/Components/style.scss
(./node_modules/css-loader/dist/cjs.js??ref--5-oneOf-6-1!
./node_modules/postcss-loader/src??postcss!
./node_modules/resolve-url-loader??ref--5-oneOf-6-3!
./node_modules/sass-loader/dist/cjs.js??ref--5-oneOf-6-4!
./src/Components/style.scss)
Error: Can't resolve '../../../../../../icon.svg' in ''
In Create React App 3.x, referencing an image from the public folder in (S)CSS worked by simply using a starting slash, as has been been answered here.
As the OP has explained, the image is in public/images and is being referenced as url(/images/icon.svg).
This doesn't work in Create React App 4.0.0 anymore and gives the error message Error: Can't resolve '../../../../../../icon.svg' in ''. The changelog of Create React App doesn't mention a breaking change regarding the public folder.
I think it is deprecated in CRA 4, (after all.. it was a breaking change..)
there are some workarounds using craco but I suggest to move these files to the src folder.
Try to change to this: (webpack should resolve this to the 'real' path)
background-image: url(./icon.svg);
I know you wanted to add the image as a background-image css property, but maybe another approach is relevant for you.
When you import like this you use it exactly as you would use a normal Component:
import { ReactComponent as Icon } from'<path_to_resource>/images/icon.svg';
<Icon />
Source:
https://create-react-app.dev/docs/adding-images-fonts-and-files/
As a temporary workaround, you could move the images into src/, import them directly in the components import myImage from '../file.svg' and set style={{ backgroundImage: file }}>?
I've created a simple React app using npx create-react-app react-portfolio
Created simple page, all things are working properly on my local machine
Pushed to GitHub and published my portfolio into gh-pages (https://yanalinso.github.io/react-portfolio/) and it works!
I changed PCs, and cloned the same code from my repository (https://github.com/yanalinso/react-portfolio)
run npm install and npm start on my local machine, the images does not work anymore (logo and the background picture), but the CSS is working
https://ibb.co/4NchQJG (Result on my local machine)
https://ibb.co/RQVv6KB (Chrome console)
https://ibb.co/ZGN1cPd (File tree, same as with my other machine)
https://ibb.co/qmfwX44 (cmd run result)
My code is simple to use images
img src="img/logo1.png"
I can still push changes on GitHub page and it's working fine,
though images are not working on my local machine.
You need to import it first like this to tell webpack that JS file is using this image
import logo1 from './img/logo1.png';
and then this:
<img src={logo1} />
I tried changing to other picture but still failed,
SOLUTION!
I've restored the package.json to default, so i've removed the config of gh-page on script and removed homepage == IT WORKS!!
Thanks guys for helping :)
I have been searching around regarding this and I find this very strange. I'm a beginner and the solution I can only come back with is
import ANYNAME from "path of file e.g ../images/logo1.png".
import ANYNAME from "../images/logo1.png"
Take note ../ is to reference a file outside the folder you are in.
If in the same folder use ./
Then when you are to use it
<img src={ANYNAME}/>
rather than <img src="../images/logo1.png"/> // This line doesn't work
I want to deploy a React app to netlify but the build fails. When I check the build logs I find that it fails where I used lazy loading to get a component.
When I use lazy loading to get a component, as shown below,
import React, {lazy } from 'react';
const SidebarLeft = lazy(() => import('./pages/SidebarLeft'))
the build fails with the error message
12:26:05 AM: Cannot find file './pages/WorldCases' in './src'.
What could be the cause of this, and how do I fix it?
For anyone who might run into this problem, it turned out that git didn't change the folder name when I did. I initially created a Pages folder. I later renamed it to pages, with a small p. But git didn't pick up that change. So I had to change the name back to Pages.
I'm having some issues deploying to netlify. These are the errors I receive:
Error: ./src/pages/index.js
12:55:30 PM: Module not found: Error: Can't resolve '../components/Featured' in '/opt/build /repo/src/pages'
12:55:30 PM: resolve '../components/Featured' in '/opt/build/repo/src/pages'
12:55:30 PM: using description file: /opt/build/repo/package.json (relative path: ./src/p ages)
12:55:30 PM: Field 'browser' doesn't contain a valid alias configuration
For some reason it can't find the components. Building locally works fine.
I used gatsby-starter w styled-components.
things I tried:
I had some local building errors regarding the window undefined. I fixed these by conditionally including them as the manual proposes.
After applying this, I started receiving console logs from ‘workbox’.
(btw this is my first time trying to deploy to netlify)
also the error Field 'browser' doesn't contain a valid alias configuration keeps repeating in the logs
here's a link to the full log I get from netlify
The errors are due to webpack not being able to resolve your relative paths. Make sure to use the correct case.
import Hero from '../components/hero'
import Introduce from '../components/introduce'
import HelpMeHelpYou from '../components/helpmehelpyou'
import Featured from '../components/featured'
import Testimonials from '../components/testimonials'
import Projects from '../components/projects'
import Gallery from '../components/gallery'
import Outroduce from '../components/outroduce'
Run below command and then push the code to Github repository.
git config core.ignorecase false
I used create-react-app to play with react. I noticed that no matter what I type in after localhost:3000/ (which is the default url on dev machine) I always get the content from localhost:3000
This means I cannot access images so that sort of means I can't really do anything with it.
Is this a bug or is it some awesome new feature that I just don't understand? How can this be switched off?
Create React App uses historyApiFallback for the webpack-dev-server, which means that all requests that give a 404 response will fall back to load the index.html file.
If you e.g. add an image to the public folder, you can see that the image takes precedence over the index.html file.
You can also import the image with the help of Webpack and use the import as src in your code, and Webpack will make sure the image ends up in the final build folder for you.
import img from './image.png';
you can use
render() {
return <img src={process.env.PUBLIC_URL + '/img/logo.png'} />;
}
for more information please refer Adding Assets Outside of the Module System