最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - TypeError: Only absolute URLs are supported using env file with fetch - Stack Overflow

matteradmin7PV0评论

I'm trying to use node-fetch to do a GET request. I'm using dotenv to take the url, but the fetch retrieve me TypeError: Only absolute URLs are supported, the URL_ENV variable isn't undefined

require('dotenv').config();
let url = process.env.URL_ENV;
        fetch(url ,{method: 'GET'}).then(response => {
            return response.json() 
         })

I'm trying to use node-fetch to do a GET request. I'm using dotenv to take the url, but the fetch retrieve me TypeError: Only absolute URLs are supported, the URL_ENV variable isn't undefined

require('dotenv').config();
let url = process.env.URL_ENV;
        fetch(url ,{method: 'GET'}).then(response => {
            return response.json() 
         })

If I change the url variable with the content of the URL_ENVinside the .env file it works

let url = "http://192.168.1.140:8080"
        fetch(url,{method: 'GET'}).then(response => {
            return response.json()
        })

Share Improve this question asked May 23, 2019 at 12:16 SMattiaSMattia 491 gold badge1 silver badge6 bronze badges 8
  • 1 Wele to Stack Overflow! Could you clarify you question? What is not working? What behavior do you expect? – Maris B. Commented May 23, 2019 at 12:22
  • Thanks for you interest. I can't understand why putting the same url, in different way, I have two different behaviors. I'm expect that also with the env variable, the first snippet of code, the fetch works. – SMattia Commented May 23, 2019 at 13:53
  • 1 Have you tried to check value of process.env.URL_ENV at run-time? Try to use Console.log() or similar method to find out the actual value. As the error says, it must be an absolute URL. – Maris B. Commented May 24, 2019 at 8:35
  • Yes I have tried, http://192.168.1.140:8080 is printed – SMattia Commented May 24, 2019 at 9:01
  • 3 If the code is not working with process.env.URL_ENV and suddenly works when you replace it with string literal http://192.168.1.140:8080 then there may be some unnecessary characters like spaces, tabs, newlines, etc.? – Maris B. Commented May 24, 2019 at 9:47
 |  Show 3 more ments

1 Answer 1

Reset to default 1

Do you have mas or semicolon symbols at the end of your lines in .env file? If you do, remove them and it should work

Post a comment

comment list (0)

  1. No comments so far