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

I'm trying to use node-fetch to do a GET request. I'm using dotenv to take the url, but the f

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745633569a4637251.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信