Categories
TUTORIALS

Error: Uncaught PermissionDenied: network access to “127.0.0.1:8000”, run again with the –allow-net flag

Did you try to run a Deno web server code but came across an error that read: “error: Uncaught PermissionDenied: network access to “127.0.0.1:8000”, run again with the –allow-net flag” ?

If yes, then do not worry. Because you are not alone? Even I got excited to run a Deno example web server code but encountered the same error message.

So what is the solution here? How do we go about fixing this?

Well luckily, a solution to this problem is quite easy. Because here is what is happening!

Error: Uncaught PermissionDenied: network access to "127.0.0.1:8000", run again with the --allow-net flag

Error: Uncaught PermissionDenied: network access to “127.0.0.1:8000” – How To Fix It?

So here is the thing. Deno has security features built into it. So when you try to run a piece of Deno code that uses network, it needs to be run with network permission. Otherwise, you get the above error message!

That is all that is happening here. So we tried to run an example Deno web server application that looked like this:

So as you can see from the above Deno code, we are making use of a server module called serve. But this sever code is not on our local computer. So we have imported it from a github repository:

"https://deno.land/std@0.50.0/http/server.ts"

But after downloading it, when we tried to run the code using the command:

deno run server.ts

this is what is happening:

It triggered the serve module to run at port 8000.

However, there is a problem here. So as you see, we have just run the server.ts file as is. That is, we did not issue any explicit permission attached to it. So what happens in that case is that the Deno code tried to run without any extra permissions.

But by default, Deno code does not have permission to access network stuff. So, it just threw the above error!

Solution

So what is the solution then? Well, we just need to give network access to this program and it should start working! Right? So what is the command to do that? Here it is:

deno run --allow-net server.ts

So we just attached extra permission before running this Deno code. This will allow our program to use the port 8000 for accessing the server from a browser.

So when we run the above command, we get the output:

http://localhost:8000/

And when we access the above url, we can see that the server has rendered the text ‘Hello World’ to the browser.

So that is it! That is all we had to do to fix this Deno network program. Hope it was clear and easy for you!

If you still have any questions, do let me know in the comment section below!

Happy Coding!

Want To Learn Typescript? Take a look at this article!

By Amar Nath

You can buy me a cup of coffee at:

https://www.buymeacoffee.com/da

Leave a Reply

Your email address will not be published. Required fields are marked *