<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: sksharein</title>
    <description>The latest articles on DEV Community by sksharein (@iamsharein).</description>
    <link>https://dev.to/iamsharein</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F157939%2F828c66b6-69db-4fee-9701-42d8713a7599.jpg</url>
      <title>DEV Community: sksharein</title>
      <link>https://dev.to/iamsharein</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/iamsharein"/>
    <language>en</language>
    <item>
      <title>Deno - NextGen Node.js</title>
      <dc:creator>sksharein</dc:creator>
      <pubDate>Mon, 18 May 2020 06:41:11 +0000</pubDate>
      <link>https://dev.to/iamsharein/deno-nextgen-node-js-4eeg</link>
      <guid>https://dev.to/iamsharein/deno-nextgen-node-js-4eeg</guid>
      <description>&lt;p&gt;&lt;a href="https://deno.land/"&gt;Deno&lt;/a&gt; is a secure runtime for JavaScript and TypeScript(From the official website). A nice sentence isn’t it?&lt;/p&gt;

&lt;p&gt;Then what is Node.js?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://nodejs.org/en/"&gt;Node.js&lt;/a&gt; is also a JavaScript runtime built on Chrome's V8 JavaScript engine. (From the official Node.js website)&lt;/p&gt;

&lt;p&gt;From the above statements can we say that both Deno and Node.js are the same? Yes it is.&lt;/p&gt;

&lt;p&gt;So the developer who gave Node.js also developed Deno as an alternative.&lt;/p&gt;

&lt;p&gt;Well Ryan Dhal the creator of Node.js has some regrets about it and Deno is simply the “better version of Node.js” with all the fixes.&lt;/p&gt;

&lt;p&gt;So what did the developer of Node.js think was wrong with it?&lt;/p&gt;

&lt;h4&gt;
  
  
  What is wrong with Node.js?
&lt;/h4&gt;

&lt;p&gt;The most important thing: nothing is wrong with Node.js and you should not switch just because Deno exists.&lt;/p&gt;

&lt;p&gt;Node.js is used by thousands of developers and companies and it has a huge and stable ecosystem with a highly active community - Node.js isn’t going anywhere.&lt;/p&gt;

&lt;p&gt;But there are a couple of weaknesses which could be improved but does not have much of an impact.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Node.js focuses only on Javascript and does not use static types natively.&lt;/li&gt;
&lt;li&gt;The import syntax is very specific to Node.js and not what we know from the ES modules.&lt;/li&gt;
&lt;li&gt;It does not support modern Javascript features like Promises.&lt;/li&gt;
&lt;li&gt;It is not secure by default.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The last point is a tricky one and can be interpreted wrong.&lt;/p&gt;

&lt;p&gt;Node.js allows you to build a secure application, there is no doubt in that.&lt;/p&gt;

&lt;p&gt;But a node script doesn’t have a built-in security model. To be precise, by default, every Node script has full access to your file system, network and environment.&lt;/p&gt;

&lt;p&gt;This makes Node.js very much flexible, but it also means tools like ESLint could anything with your files on your file system theoretically.&lt;/p&gt;

&lt;h4&gt;
  
  
  How does Deno fix these issues?
&lt;/h4&gt;

&lt;p&gt;Deno can be used for the same things as Node.js. It can be used to build web servers as we did with Node.js and other utility scripts.&lt;/p&gt;

&lt;p&gt;But Deno &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;By default supports Typescript unlike Node.js - hence it is a Javascript and Typescript runtime.&lt;/li&gt;
&lt;li&gt;Uses ES modules import system instead of having its own.&lt;/li&gt;
&lt;li&gt;Embraces modern Javascript features like Promises.&lt;/li&gt;
&lt;li&gt;It is secure by default.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Let’s try this out
&lt;/h4&gt;

&lt;p&gt;You can install Deno using&lt;br&gt;
&lt;code&gt;curl -fsSL https://deno.land/x/install/install.sh | sh&lt;/code&gt;&lt;/p&gt;
&lt;h5&gt;
  
  
  Typescript Support
&lt;/h5&gt;

&lt;p&gt;You can write the usual Javascript script but can also switch to Typescript any point as Typescript compiler is provided by default.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;hello&lt;/span&gt; &lt;span class="nx"&gt;world&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This code would fail when executed with Node.js but works with Deno.&lt;br&gt;
But this is totally optional, but if you want to use it, you don’t have to configure your custom Typescript project compilation flow.&lt;/p&gt;
&lt;h5&gt;
  
  
  ES Module Support
&lt;/h5&gt;

&lt;p&gt;Node.js comes with its own module system&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But when it comes to modern web frameworks we are used to a different format&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;some_node_module&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In addition, in Node projects we use npm to manage our project packages. This tool downloads them and stores them in node_modules directory.&lt;/p&gt;

&lt;p&gt;This folder can easily become very large and it's already an important part of the Node.js design.&lt;/p&gt;

&lt;p&gt;In Node.js when we create a web server we depend on Express.js and the web server would be something like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here the require imports the module from the node_modules directory.&lt;/p&gt;

&lt;p&gt;But Deno simplifies it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;serve&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//deno.land/std@0.50.0/http/server.ts’;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;serve&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This imports the serve function from the server.ts package from the web.&lt;/p&gt;

&lt;p&gt;Deno automatically downloads and caches this package when it runs for the first time.&lt;/p&gt;

&lt;h5&gt;
  
  
  Modern Features
&lt;/h5&gt;

&lt;p&gt;Deno supports modern Javascript features like Promises and async iterables out of the box.&lt;/p&gt;

&lt;p&gt;To spin up a complete server&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;serve&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//deno.land/std@0.50.0/http/server.ts’;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;serve&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;respond&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt; &lt;span class="nx"&gt;world&lt;/span&gt;&lt;span class="o"&gt;!!!&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Also it eliminates the need to wrap await inside an async function.&lt;/p&gt;

&lt;h5&gt;
  
  
  Security
&lt;/h5&gt;

&lt;p&gt;As mentioned, Deno has built-in security.&lt;/p&gt;

&lt;p&gt;But this does not mean Deno applications are always secured.&lt;/p&gt;

&lt;p&gt;This just means Deno application does not have control over your file system, network and environment.&lt;br&gt;
For example when we run the application &lt;br&gt;
&lt;code&gt;deno run server.ts&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;Compile file:///home/sharein/Desktop/deno_samples/server.ts
Download https://deno.land/std@0.50.0/http/server.ts
&lt;span class="nb"&gt;.&lt;/span&gt;
&lt;span class="nb"&gt;.&lt;/span&gt;
&lt;span class="nb"&gt;.&lt;/span&gt;
Download https://deno.land/std@0.50.0/http/http_status.ts
Download https://deno.land/std@0.50.0/bytes/mod.ts
error: Uncaught PermissionDenied: network access to &lt;span class="s2"&gt;"0.0.0.0:3000"&lt;/span&gt;, run again with the &lt;span class="nt"&gt;--allow-net&lt;/span&gt; flag
    at unwrapResponse &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$deno$/&lt;/span&gt;ops/dispatch_json.ts:43:11&lt;span class="o"&gt;)&lt;/span&gt;
    at Object.sendSync &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$deno$/&lt;/span&gt;ops/dispatch_json.ts:72:10&lt;span class="o"&gt;)&lt;/span&gt;
    at Object.listen &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$deno$/&lt;/span&gt;ops/net.ts:51:10&lt;span class="o"&gt;)&lt;/span&gt;
    at listen &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$deno$/&lt;/span&gt;net.ts:152:22&lt;span class="o"&gt;)&lt;/span&gt;
    at serve &lt;span class="o"&gt;(&lt;/span&gt;https://deno.land/std@0.50.0/http/server.ts:261:20&lt;span class="o"&gt;)&lt;/span&gt;
    at file:///home/sharein/Desktop/deno_samples/server.ts:3:16
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The above error clearly specifies that we could not run our server without providing permission to access the network.&lt;/p&gt;

&lt;p&gt;The scripts executes only when it is executed as &lt;/p&gt;

&lt;p&gt;&lt;code&gt;deno run --allow-net server.ts&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In this case &lt;code&gt;--allow-net&lt;/code&gt; provides the permission to access the network, similarly you can provide permissions for read &lt;code&gt;(--allow-read)&lt;/code&gt; and write &lt;code&gt;(--allow-write)&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Conclusion
&lt;/h4&gt;

&lt;p&gt;Should you switch to Deno, that’s totally up to you.&lt;/p&gt;

&lt;p&gt;But Deno version 1.0 was released recently and just because it's a major release it does not mean it is production ready.&lt;/p&gt;

&lt;p&gt;It is very new and under active development and now would be a time to dive into its package ecosystem.&lt;/p&gt;

&lt;p&gt;We do not know if it would become a replacement of Node.js, only time can answer that.&lt;/p&gt;

</description>
      <category>deno</category>
      <category>node</category>
      <category>javascript</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
