Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Killeon Patterson
Killeon Patterson
17,540 Points

TypeError: Failed to construct 'URL': Invalid URL

Hello,

I'm attempting to use images and videos in my react component and I'm receiving one of two errors. Where I try the new URL constructor as it is below, I receive the errors 'TypeError: Failed to construct 'URL': Invalid URL'

When I try the new URL constructor as const made = new URL('./public/made.png', import.meta.url); I get the error 'Module parse failed: Unexpected token (18:48) File was processed with these loaders:

  • ./node_modules/babel-loader/lib/index.js
  • ./node_modules/eslint-loader/dist/cjs.js You may need an additional loader to handle the result of these loaders.'

I've tried changing the browserslist in the package.json, deleting the cache, and running npm install. I've still been unsuccessful. Do you know how I can use local media (images/videos) in my project? Thank you.

best, Killeon

import React, { useState, useEffect } from 'react';
import axios from 'axios';


function WeatherMan () {

  const [data, setData] = useState({});
  const [location, setLocation] = useState("");

  const url = "https://api.openweathermap.org/data/2.5/weather?q=portland&units=imperial&appid=de972e4c2eb6a9fbe69412995d65170d";

// Below is the line that reports the error
// Below is the line that reports the error
// Below is the line that reports the error

 const made = new URL('./public/made.png');

  useEffect(
  () => {
     axios.get(url).then((response) => {
       setData(response.data);
       console.log(response.data);
     })
   }, []);

  return (
    <div className="container sapp" style={{marginBottom: "0%"}}>

      <div className="top" style={{marginBottom: "0%"}}>
          <div className="location" style={{ display: "inline-block"}}>
                <h3 style={{fontSize: "2rem"}}>
                  {data.name}
                </h3>
          </div>
          <div className="temp" style={{marginLeft: "1%", display: "inline-block"}}>

            {data.main ? <h2>{data.main.temp.toFixed()}°F</h2> : null}

          </div>
          </div>
      <div className="description" style={{fontWeight: "bold", display: "inline-block"}}>
        {data.weather ? <p1>  <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-clouds" viewBox="0 0 16 16">
        <path d="M16 7.5a2.5 2.5 0 0 1-1.456 2.272 3.513 3.513 0 0 0-.65-.824 1.5 1.5 0 0 0-.789-2.896.5.5 0 0 1-.627-.421 3 3 0 0 0-5.22-1.625 5.587 5.587 0 0 0-1.276.088 4.002 4.002 0 0 1 7.392.91A2.5 2.5 0 0 1 16 7.5z"/>
        <path d="M7 5a4.5 4.5 0 0 1 4.473 4h.027a2.5 2.5 0 0 1 0 5H3a3 3 0 0 1-.247-5.99A4.502 4.502 0 0 1 7 5zm3.5 4.5a3.5 3.5 0 0 0-6.89-.873.5.5 0 0 1-.51.375A2 2 0 1 0 3 13h8.5a1.5 1.5 0 1 0-.376-2.953.5.5 0 0 1-.624-.492V9.5z"/>
      </svg> ({data.weather[0].main})</p1> : null}
      </div>

           <div className="wind" style={{ marginLeft: "0.5%", display: "inline-block"}}>
        {data.wind ? <p style={{fontWeight: "bold", fontSize:"1rem"}}> wind {data.wind.speed.toFixed()} mph </p> : null }
      </div>
      <div>
       <img src={"made"}/>
      </div>
      </div>
  );
}

export default WeatherMan;

1 Answer

Rohald van Merode
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Rohald van Merode
Treehouse Staff

Hey Killeon Patterson,

The URL constructor expects a url to be passed instead of just a file path. I'd suggest instead of creating a URL object like that simply set the src of your img tag to be the path 🙂

<img src="./made.png"/>

Hope this helps!

Killeon Patterson
Killeon Patterson
17,540 Points

Hey Rohald van Merode,

Thank you for your time and help : )