<?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: Brijesh Bittu</title>
    <description>The latest articles on DEV Community by Brijesh Bittu (@brijesh).</description>
    <link>https://dev.to/brijesh</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%2F199%2F717550.jpeg</url>
      <title>DEV Community: Brijesh Bittu</title>
      <link>https://dev.to/brijesh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/brijesh"/>
    <language>en</language>
    <item>
      <title>Make a Pocket app like html parser in Python</title>
      <dc:creator>Brijesh Bittu</dc:creator>
      <pubDate>Tue, 27 Mar 2018 08:48:14 +0000</pubDate>
      <link>https://dev.to/brijesh/make-a-pocket-app-like-html-parser-in-python-7ai</link>
      <guid>https://dev.to/brijesh/make-a-pocket-app-like-html-parser-in-python-7ai</guid>
      <description>&lt;p&gt;This tutorial will show how to extract only the relevant html from any article or blog post by their URL in &lt;strong&gt;Python&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Most of us have used &lt;a href="https://getpocket.com/"&gt;Pocket&lt;/a&gt; app on our phones or browsers to save links and read them later. It is kind of like a bookmark app but which also saves the link's contents. After adding the link to Pocket, you can see that it extracts only the main content of the article and discards other things like the websites's footer, menu, sidebar (if any), user comments, etc. We will not be getting into the algorithm related to identifying the html tag with the most amount of text content. You can read the discussion here on &lt;a href="http://stackoverflow.com/questions/3652657/what-algorithm-does-readability-use-for-extracting-text-from-urls"&gt;stackoverflow&lt;/a&gt; about the algorithms.&lt;/p&gt;

&lt;h3&gt;
  
  
  Newspaper
&lt;/h3&gt;

&lt;p&gt;Newspaper is a Python module that deals with extracting text/html from URLs. Besides this, it can also extract article's title, author, publish time, images, videos etc. And if used in conjunction with &lt;code&gt;nltk&lt;/code&gt; it can also extract article's keywords and summary.&lt;/p&gt;

&lt;p&gt;In this tutorial, we will be using &lt;a href="https://github.com/codelucas/newspaper"&gt;newspaper&lt;/a&gt; to extract the html tag containing the relevant contents of the URL, and then we will expose this functionality on web using &lt;a href="http://flask.pocoo.org/"&gt;flask&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Get started
&lt;/h2&gt;

&lt;p&gt;We will be working in the global Python environment for simplicity of the tutorial. But you should do all the process described below in a virtual environment.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Install &lt;code&gt;newspaper&lt;/code&gt; and &lt;code&gt;flask&lt;/code&gt; using &lt;code&gt;pip&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For &lt;code&gt;python2.x&lt;/code&gt;, &lt;code&gt;pip install newspaper flask&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;python3.x&lt;/code&gt;, &lt;code&gt;pip install newspaper3k flask&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;If there is some error while installing &lt;code&gt;newspaper&lt;/code&gt; you can read the detailed guide specific to your platform &lt;a href="https://pypi.python.org/pypi/newspaper#get-it-now"&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a file called &lt;code&gt;extractor.py&lt;/code&gt; with the following code.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;newspaper&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Article&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Config&lt;/span&gt;

&lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Config&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;keep_article_html&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;article&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Article&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;article&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;download&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;article&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;article&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;article&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;article&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;article&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;top_image&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;authors&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;article&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;authors&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;This is the only code we need for the main part thanks to &lt;code&gt;newspaper&lt;/code&gt; which does all the heavy lifting. It identifies the html element containing the most relevant data, cleans it up, removes any &lt;code&gt;script&lt;/code&gt;, &lt;code&gt;style&lt;/code&gt; and other irrelevant tags that are not likely to make up the main article.&lt;/p&gt;

&lt;p&gt;In the above code &lt;code&gt;Article&lt;/code&gt; is &lt;code&gt;newspaper&lt;/code&gt;'s way of representing the article from a URL. By default, &lt;code&gt;newspaper&lt;/code&gt; does not save the article content's html to save some extra processing. That is why we are importing &lt;code&gt;Config&lt;/code&gt; from &lt;code&gt;newspaper&lt;/code&gt; and creating a custom configuration telling &lt;code&gt;newspaper&lt;/code&gt; to keep the article html.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the &lt;code&gt;extract&lt;/code&gt; function that accepts a &lt;code&gt;url&lt;/code&gt;, we first create an &lt;code&gt;Article&lt;/code&gt; instance passing in the &lt;code&gt;url&lt;/code&gt; and custom &lt;code&gt;config&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Then we download the full article html with &lt;code&gt;article.download()&lt;/code&gt;. &lt;code&gt;newspaper&lt;/code&gt; still hasn't processed the full html yet.&lt;/li&gt;
&lt;li&gt;Now we call &lt;code&gt;article.parse()&lt;/code&gt;. After this, all the necessary data related to the article in &lt;code&gt;url&lt;/code&gt; will be generated. The data includes article's title, full text, author, image etc.&lt;/li&gt;
&lt;li&gt;Then we return the data that we need in a &lt;code&gt;dict&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Exposing as an API
&lt;/h3&gt;

&lt;p&gt;Now that we have created the functionality to extract articles, we will be making this available on the web so that we can test it out in our browsers. We will be using &lt;code&gt;flask&lt;/code&gt; to make an API. Here is the code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;flask&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;Flask&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;jsonify&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;request&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;extractor&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;extract&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Flask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;index&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"""
    &amp;lt;form action="/extract"&amp;gt;
        &amp;lt;input type="text" name="url" placeholder="Enter a URL" /&amp;gt;
        &amp;lt;button type="submit"&amp;gt;Submit&amp;lt;/button&amp;gt;
    &amp;lt;/form&amp;gt;
    """&lt;/span&gt;


&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/extract'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;extract_url&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'url'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;jsonify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'error'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Provide a URL'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;406&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;jsonify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'success'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;


 &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;'__main__'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
     &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;debug&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;We first create a &lt;code&gt;Flask&lt;/code&gt; app.&lt;/li&gt;
&lt;li&gt;In its &lt;code&gt;index&lt;/code&gt; route we return a simple form with text input where users will paste the url and then submit the form to the &lt;code&gt;/extract&lt;/code&gt; route specified in &lt;code&gt;action&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;In the &lt;code&gt;extract_url&lt;/code&gt; function, we get the &lt;code&gt;url&lt;/code&gt; from &lt;code&gt;request.args&lt;/code&gt; and check if it is empty. If empty, we return an error. Otherwise will pass the &lt;code&gt;url&lt;/code&gt; to our &lt;code&gt;extract&lt;/code&gt; function that we created and then return the result using &lt;code&gt;jsonify&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Now you can simply run &lt;code&gt;python app.py&lt;/code&gt; and head over to &lt;a href="http://localhost:5000"&gt;http://localhost:5000&lt;/a&gt; in your browser it test the implemetation.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://github.com/brijeshb42/extractor"&gt;Full Code&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Few things to remember
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Instead of checking the url for just empty string, we can also use regex to verify that the url, is in fact a url.&lt;/li&gt;
&lt;li&gt;We should also check that the &lt;code&gt;url&lt;/code&gt; is not for our own domain as it will lead to an infinite loop calling the same &lt;code&gt;extract_url&lt;/code&gt; function again and again.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;newspaper&lt;/code&gt; will not always be able to extract the most relevant html. Its functionality completely depends on how the content is organized in the source &lt;code&gt;url&lt;/code&gt;'s website. Sometimes, it may give one or two extra paragraphs or sometimes less. But for most of the standard news websites and blogs, it will always return the most relevant html.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Things to do next
&lt;/h4&gt;

&lt;p&gt;The above demonstration is a very simple application that takes in a URL, returns the html and then forgets about it. But to make this more useful, you can take it a step further by:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Adding a database&lt;/li&gt;
&lt;li&gt;Save the url and its extracted contents so that you can return the result from DB if the same URL is provided again.&lt;/li&gt;
&lt;li&gt;You can add some more advanced APIs like returning a list of most recent queried/saved URLs and their title and other contents.&lt;/li&gt;
&lt;li&gt;Then you can use the API service to create a web or android/ios app similar in features to what &lt;a href="https://getpocket.com/"&gt;Pocket&lt;/a&gt; is.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Read More
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://newspaper.readthedocs.io/en/latest/"&gt;newspaper API&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This post was originally published on &lt;a href="http://bitwiser.in/2016/06/09/make-a-pocket-app-like-html-parser-using-python.html"&gt;bitwiser.in&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>pocket</category>
    </item>
    <item>
      <title>Setup a ES6 javascript project using webpack, babel and eslint</title>
      <dc:creator>Brijesh Bittu</dc:creator>
      <pubDate>Thu, 01 Mar 2018 05:40:51 +0000</pubDate>
      <link>https://dev.to/brijesh/setup-a-es6-javascript-project-using-webpack-babel-and-eslint-1ek3</link>
      <guid>https://dev.to/brijesh/setup-a-es6-javascript-project-using-webpack-babel-and-eslint-1ek3</guid>
      <description>

&lt;p&gt;Table of contents -&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Setup&lt;/li&gt;
&lt;li&gt;Styling&lt;/li&gt;
&lt;li&gt;Linting&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Whenever someone learns a new programming language, there is a 99% chance that their first program is going to be a &lt;strong&gt;Hello World&lt;/strong&gt; program. In this proverbial program, they are supposed to print &lt;code&gt;Hello World&lt;/code&gt; on their screen/console. Depending on the language, it can range from 1 line program to multiline just for printing this &lt;strong&gt;Hello World&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In Javascript, in olden times (4-5 years back), one would simply create an HTML file with this content and open it up in their browsers to see &lt;strong&gt;Hello World&lt;/strong&gt; printed in their browser windows (and also in the browser console).&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Hello World&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello World&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script&amp;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="s1"&gt;'Hello World'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But as javascript ecosystem has matured, this process has gotten a little complicated (for the better). In this tutorial, you will get to know how to set up this type of project.&lt;/p&gt;

&lt;h3&gt;Assumptions&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You know Javascript (preferrably some es6 too).&lt;/li&gt;
&lt;li&gt;You have &lt;code&gt;nodejs&lt;/code&gt; and &lt;code&gt;npm&lt;/code&gt; installed on your system (&lt;a href="https://docs.npmjs.com/getting-started/installing-node"&gt;Tutorial&lt;/a&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The full code is available at &lt;a href="https://github.com/brijeshb42/hello-world-tutorial"&gt;https://github.com/brijeshb42/hello-world-tutorial&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Part 1&lt;/h2&gt;

&lt;p&gt;Open your terminal app or Command Prompt and &lt;code&gt;cd&lt;/code&gt; to a directory where you would like to create this project. Let's assume the project folder is called &lt;code&gt;hello-world&lt;/code&gt; in some directory on your disk. Now type these commands -&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;cd hello-world&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;npm init --y&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This will create a &lt;code&gt;package.json&lt;/code&gt; file in &lt;code&gt;hello-world&lt;/code&gt; directory. &lt;code&gt;package.json&lt;/code&gt; is file in your project which is used by &lt;code&gt;nodejs&lt;/code&gt; and &lt;code&gt;npm&lt;/code&gt; to keep track of installed packages and your project's metadata. Your &lt;code&gt;package.json&lt;/code&gt; might look something like this -&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="s2"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"hello-world"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="s2"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1.0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="s2"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="s2"&gt;"main"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"index.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="s2"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"echo &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;Error: no test specified&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt; &amp;amp;&amp;amp; exit 1"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="s2"&gt;"keywords"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="s2"&gt;"author"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="s2"&gt;"license"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ISC"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now add webpack and dev-server -&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;webpack@3.11.1 webpack-dev-server &lt;span class="nt"&gt;--save-dev&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;at the time of writing this, the version of webpack installed was &lt;code&gt;3.11.1&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Edit: the version has been added as pointed out by &lt;code&gt;Patrick Cole&lt;/code&gt; in the comments  because, since this tutorial has been published, &lt;code&gt;webpack&lt;/code&gt; version 4 has been released which has a slightly different configuration and might break this tutorial's setup.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Create a &lt;code&gt;src&lt;/code&gt; directory inside your project folder and then create &lt;code&gt;index.js&lt;/code&gt; file inside it.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;mkdir src&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;echo "console.log('Hello world');" &amp;gt; src/index.js&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is our hello world program that will print &lt;code&gt;Hello world&lt;/code&gt; in the browser console when run.&lt;/p&gt;

&lt;p&gt;At this point, you can start with writing a webpack config file to bundle your files for browser to load.&lt;/p&gt;

&lt;p&gt;Create a &lt;code&gt;webpack.config.js&lt;/code&gt; file in your project folder with the following content. This file is used by webpack to read your configuration and build project accordingly.&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;path&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="s1"&gt;'path'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;bundle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'./src/index.js'&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'dist'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="na"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'bundle.js'&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;Now, you can run this command for webpack to load the entry file and create a bundled js file in &lt;code&gt;dist&lt;/code&gt; directory in the root of your project directory.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;./node_modules/.bin/webpack
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is the build command that will bundle up all the dependencies and create a &lt;code&gt;bundle.js&lt;/code&gt; file as specified in the &lt;code&gt;output&lt;/code&gt; option of webpack config file. After running this command, you can see a &lt;code&gt;bundle.js&lt;/code&gt; file in &lt;code&gt;dist&lt;/code&gt;. You can not load this js file yet as you first have to have an html file. Browser will load that file which in turn will load the js file.&lt;br&gt;
 You can manually create an &lt;code&gt;index.html&lt;/code&gt; file in &lt;code&gt;dist&lt;/code&gt; with this content.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"./bundle.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is the minimum amount of html required to load and run our bundled js. Now you can double click this html file which will open it in a browser. You can open the browser console using &lt;code&gt;CMD/CTRL&lt;/code&gt; &lt;code&gt;+&lt;/code&gt; &lt;code&gt;SHIFT&lt;/code&gt; &lt;code&gt;+&lt;/code&gt; &lt;code&gt;I&lt;/code&gt; to see the output. Let's look at a better way through which you don't have to write the html file.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install html-webpack-plugin --save-dev&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is a webpack plugin that automatically generates the &lt;code&gt;index.html&lt;/code&gt; file in &lt;code&gt;dist&lt;/code&gt; with proper references to all the generated javascript files. To use this plugin, update your &lt;code&gt;webpack.config.js&lt;/code&gt; with this -&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight diff"&gt;&lt;code&gt;  const path = require('path');
&lt;span class="gi"&gt;+ const HtmlWebpackPlugin = require('html-webpack-plugin');
&lt;/span&gt;
 module.exports = {
   entry: {
    bundle: './src/index.js'
  },
   output: {
     path: path.resolve(__dirname, 'dist'),
      filename: 'bundle.js'
&lt;span class="gd"&gt;-   }
&lt;/span&gt;&lt;span class="gi"&gt;+  },
+  plugins: [
+    new HtmlWebpackPlugin()
+  ]
&lt;/span&gt; };
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After this, you can run the build command again -&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;./node_modules/.bin/webpack
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will now create and extra &lt;code&gt;index.html&lt;/code&gt; file in &lt;code&gt;dist&lt;/code&gt; directory with proper script tags to include &lt;code&gt;bundle.js&lt;/code&gt;. This can now be opened in browser directly and it'll work like before, except that you didn't have to create it yourself. &lt;/p&gt;

&lt;p&gt;To make the build command shorter, lets create an alias inside &lt;code&gt;package.json&lt;/code&gt; so that you only have to type &lt;code&gt;npm run build&lt;/code&gt; to bundle your files. Update your &lt;code&gt;package.json&lt;/code&gt; -&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight diff"&gt;&lt;code&gt; {
   "name": "hello-world",
   "version": "1.0.0",
    "description": "",
   "main": "index.js",
   "scripts": {
     "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1"
   },
   "keywords": [],
   "author": "",
   "license": "ISC",
   "devDependencies": {
     "html-webpack-plugin": "^2.30.1",
     "webpack": "^3.11.0",
     "webpack-dev-server": "^2.11.1"
&lt;span class="gd"&gt;-  }
&lt;/span&gt;&lt;span class="gi"&gt;+  },
+  "scripts": {
+    "build": "webpack"
+  }
&lt;/span&gt; }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Till now, webpack bundles the files and exits. This is good when you just want to bundle and deploy to your local or remote server. But during development, this can get frustrating really quickly. To overcome this frustration, you'll use &lt;code&gt;webpack-dev-server&lt;/code&gt; which constantly watches your files for changes and refreshes the page in browser instantly. It also starts a development server inside &lt;code&gt;dist&lt;/code&gt; so the html file is loaded from a server instead of a file system (just in case you are using ajax in your js which does not work when opened from filesystem). Install it using -&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install webpack-dev-server&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This starts the development server with &lt;code&gt;dist&lt;/code&gt; as the base directory. The default url is &lt;code&gt;http://localhost:8080&lt;/code&gt;. Opening this url in your browser will load the &lt;code&gt;index.html&lt;/code&gt; file and log &lt;code&gt;Hello World&lt;/code&gt; in the console. Now if you update your console log from &lt;code&gt;Hello World&lt;/code&gt; to &lt;code&gt;Hi World&lt;/code&gt; inside &lt;code&gt;src/index.js&lt;/code&gt;, &lt;code&gt;webpack-dev-server&lt;/code&gt; will automatically reload the browser and you will be able to see the new output.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;./node_modules/.bin/webpack-dev-server --content-base dist&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Lets add this also as an alias in &lt;code&gt;package.json&lt;/code&gt; -&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight diff"&gt;&lt;code&gt; {
   "name": "hello-world",
   "version": "1.0.0",
   "description": "",
   "main": "index.js",
   "scripts": {
     "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1"
   },
   "keywords": [],
   "author": "",
   "license": "ISC",
   "devDependencies": {
     "html-webpack-plugin": "^2.30.1",
     "webpack": "^3.11.0",
     "webpack-dev-server": "^2.11.1"
   },
   "scripts": {
     "build": "webpack",
&lt;span class="gi"&gt;+    "dev": "webpack-dev-server --content-base dist"
&lt;/span&gt;   }
 }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now running &lt;code&gt;npm run dev&lt;/code&gt; will start &lt;code&gt;webpack-dev-server&lt;/code&gt; with auto reload on changes.&lt;/p&gt;

&lt;p&gt;At this point, you cannot use es6 syntax in your js code yet. Let's add that support. This will be done by using &lt;code&gt;babel&lt;/code&gt;. To add babel support in the build process, let us first install it. The &lt;code&gt;babel-loader&lt;/code&gt; will require &lt;code&gt;babel-core&lt;/code&gt; to be installed. And to support es6/7/8/* syntax, you'll add &lt;code&gt;babel-preset-env&lt;/code&gt;. Run this in your terminal in the project folder -&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install babel-core babel-loader babel-preset-env --save-dev&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;First create a &lt;code&gt;.babelrc&lt;/code&gt; file in project directory so that babel can load its configuration. Add this to the file -&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="s2"&gt;"presets"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"env"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="s2"&gt;"targets"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"browsers"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Chrome &amp;gt;= 55"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This configuration is used deliberately so that you can see the bundled js file in &lt;code&gt;dist&lt;/code&gt; directory and discern how your es6 code has been transpiled. As browsers started supporting more and more es6 features, &lt;code&gt;babel&lt;/code&gt;, instead of transpiling all of the code blindly, now smartly identifies which features are supported natively and does not transpile those parts. This reduces the overall bundle size.&lt;/p&gt;

&lt;p&gt;The simplest configuration to be used instead of the above (if you don't care about browser version) would have been -&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="s2"&gt;"presets"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"env"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now let's instruct &lt;code&gt;webpack&lt;/code&gt; to use babel to transpile the &lt;code&gt;js&lt;/code&gt; files first.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight diff"&gt;&lt;code&gt;  const path = require('path');
&lt;span class="gi"&gt;+ const HtmlWebpackPlugin = require('html-webpack-plugin');
&lt;/span&gt;
 module.exports = {
   entry: `{
    bundle: './src/index.js'
  },
   output: {
     path: path.resolve(__dirname, 'dist'),
      filename: 'bundle.js'
    },
   plugins: [
     new HtmlWebpackPlugin()
&lt;span class="gd"&gt;-  ]
&lt;/span&gt;&lt;span class="gi"&gt;+  ],
+  module: {
+    rules: [{
+      test: /\.js$/,
+     exclude: /node_modules/,
+     use: 'babel-loader'
+   }]
+ }
&lt;/span&gt; };
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Create a new file &lt;code&gt;src/message.js&lt;/code&gt; and add this - &lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="s2"&gt;"Hello World"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now modify &lt;code&gt;src/index.js&lt;/code&gt; to use the simplest es6 feature of importing -&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'./message'&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;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In above code, es6 module syntax is used. Now running &lt;code&gt;npm run dev&lt;/code&gt; will create an updated bundle (though the output is same) which you can test in your browser console.&lt;/p&gt;

&lt;p&gt;This sums up the first part of the tutorial where you have setup the simplest (seriously simplest) javascript project using webpack for bundling with babel integration for transpiling es6 to es5.&lt;/p&gt;




&lt;h2&gt;Part 2&lt;/h2&gt;

&lt;p&gt;Now, let's move on to second part of the tutorial where we'll setup webpack to import &lt;code&gt;css&lt;/code&gt; files. Through this, you can directly include styles in your javascript files.&lt;/p&gt;

&lt;p&gt;First, let's modify &lt;code&gt;src/index.js&lt;/code&gt; to show some text on the page instead of just logging to console.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight diff"&gt;&lt;code&gt; import message from './message';

&lt;span class="gd"&gt;-console.log(message);
&lt;/span&gt;&lt;span class="gi"&gt;+const paragraph = document.createElement('p');
+paragraph.innerHTML = message;
+
+document.body.prepend(paragraph);
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This creates a &lt;code&gt;p&lt;/code&gt; tag with the imported &lt;code&gt;message&lt;/code&gt; as the html and adds it to the page.&lt;/p&gt;

&lt;p&gt;Now, let's style this &lt;code&gt;p&lt;/code&gt; tag using css. This requires &lt;code&gt;css-loader&lt;/code&gt; and &lt;code&gt;style-loader&lt;/code&gt;. Install it using -&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install css-loader style-loader --save-dev&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To support &lt;code&gt;css&lt;/code&gt; file importing, let's update our &lt;code&gt;webpack.config.js&lt;/code&gt; with a new rule which tests if an imported file has &lt;code&gt;css&lt;/code&gt; extension and parses it using &lt;code&gt;style-loader&lt;/code&gt; and &lt;code&gt;css-loader&lt;/code&gt; -&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight diff"&gt;&lt;code&gt;  const path = require('path');
  const HtmlWebpackPlugin = require('html-webpack-plugin');

  module.exports = {
   entry: {
     bundle: './src/index.js '
   },
   output: {
     path: path.resolve(__dirname, 'dist'),
      filename: 'bundle.js'
    },
   plugins: [
     new HtmlWebpackPlugin()
   ],
   module: {
     rules: [{ 
        test: /\.js$/,
        exclude: /node_modules/,
       use: 'babel-loader'
&lt;span class="gi"&gt;+    }, {
+      test: /\.css$/,
+      exclude: /node_modules/,
+      use: [
+        {loader: 'style-loader'},
+        {loader: 'css-loader'}
+       ]
+     }]
&lt;/span&gt;   }
 };
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now create a css file &lt;code&gt;src/index.css&lt;/code&gt; and style the &lt;code&gt;p&lt;/code&gt; tag -&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&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;Import this css file in &lt;code&gt;src/index.css&lt;/code&gt; -&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight diff"&gt;&lt;code&gt; import message from './message';
&lt;span class="gi"&gt;+import './index.css';
&lt;/span&gt;
 const paragraph = document.createElement('p');
 paragraph.innerHTML = message;

 document.body.prepend(paragraph);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, restart dev server using &lt;code&gt;npm run dev&lt;/code&gt;. You'll be able to see that the page now show &lt;code&gt;Hello World&lt;/code&gt; in red color. If you change the color from to red to blue in &lt;code&gt;index.css&lt;/code&gt;, the page will reload and new style will be visible. To see the new style without the actual page reload, modify the dev server command in &lt;code&gt;package.json&lt;/code&gt; -&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight diff"&gt;&lt;code&gt; {
  "name": "hello-world",
  "version": "1.0.0", 
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "webpack",
&lt;span class="gd"&gt;-    "dev": "webpack-dev-server --content-base dist"
&lt;/span&gt;&lt;span class="gi"&gt;+    "dev": "webpack-dev-server --content-base dist --hot"
&lt;/span&gt;  },
  "keywords": [],
  "author": "" ,
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-env": "^1.6.1",
    "css-loader": "^0.28.9",
    "html-webpack-plugin": "^2.30.1",
    "style-loader": "^0.20.2",
    "webpack": "^3.11.0",
    "webpack-de v-server": "^2.11.1"
  }
 }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This enables hot module replacement in webpack which shows the new changes in your code (in &lt;code&gt;css&lt;/code&gt; or &lt;code&gt;js&lt;/code&gt; or any file as long as webpack knows how to load it) without full reload of the page. Restart the server with &lt;code&gt;npm run dev&lt;/code&gt; and try to change the color of &lt;code&gt;p&lt;/code&gt; in css. You'll notice that the color changes in page without actually reloading the page.&lt;/p&gt;

&lt;p&gt;If you try to run the build command, &lt;code&gt;npm run build&lt;/code&gt;, in the &lt;code&gt;dist&lt;/code&gt; directory, you'll notice that there aren't any css files built. That is because webpack adds the styles in javascript bundles as strings and applies these styles in the page by creating &lt;code&gt;style&lt;/code&gt; tags. This is fine when you are developing. But during deployment process, it is always a good practice to include your css files in the &lt;code&gt;head&lt;/code&gt; tag so that the page look is not compromised while the javascript is loading. To fix this, we'll use &lt;code&gt;extract-text-webpack-plugin&lt;/code&gt; which extracts all the imported css to its own file during the build process. Before this, let's first setup webpack to understand &lt;code&gt;development&lt;/code&gt; and &lt;code&gt;production&lt;/code&gt; mode.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight diff"&gt;&lt;code&gt;  const path = require('path');
  const HtmlWebpackPlugin = require('html-webpack-plugin');

&lt;span class="gi"&gt;+ const env = process.env.NODE_ENV || 'development';
+ const isDev = env === 'development';
+ const isProd = env === 'production';
&lt;/span&gt;
  module.exports = {
    entry: {
      bundle: './src/index.js'
    },
    output: {
      path: path.resolve(__dirname, 'dist'),
      filename: 'bundle.js'
    },
    plugins: [
      new HtmlWebpackPlugin()
    ],
    module: {
      rules: [{
        test: /\.js$/,
        exclude: /node_modules/,
       use: 'babel-loader'
      }, {
        test: /\.css$/,
        exclude: /node_modules/,
        use: [
          {loader: 'style-loader'},
          {loader: 'css-loader'}
        ]
      }]
    }
  };
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And modify &lt;code&gt;package.json&lt;/code&gt; to run build command in production mode and dev server in development mode.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight diff"&gt;&lt;code&gt; { 
   "name": "hello-world",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
&lt;span class="gd"&gt;-    "build": "webpack",
-    "dev": "webpack-dev-server --content-base dist --hot"
&lt;/span&gt;&lt;span class="gi"&gt;+    "build": "NODE_ENV=production webpack",
+    "dev": "NODE_ENV=development webpack-dev-server --content-base dist --hot"
&lt;/span&gt;  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-env": "^1.6.1",
    "css-loader": "^0.28.9",
    "extract-text-webpack-plugin": "^3.0.2",
    "html-webpack-plugin": "^2.30.1",
    "style-loader": "^0.20.2",
    "webpack": "^3.11.0",
    "webpack-dev-server": "^2.11.1"
  }
 }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now install &lt;code&gt;extract-text-webpack-plugin&lt;/code&gt; using -&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install extract-text-webpack-plugin --save-dev&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And update &lt;code&gt;webpack.config.js&lt;/code&gt; -&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight diff"&gt;&lt;code&gt; const path = require('path');
 const HtmlWebpackPlugin = require('html-webpack-plugin');
&lt;span class="gi"&gt;+const ExtractTextPlugin = require('extract-text-webpack-plugin');
&lt;/span&gt;
 const env = process.env.NODE_ENV || 'development';
 const isDev = env === 'development';
 const isProd = env === 'production';

&lt;span class="gi"&gt;+const extractCss = new ExtractTextPlugin({
+  filename: 'index.css',
+  disable: isDev
+});
&lt;/span&gt;
 module.exports = {
   entry: {
     bundle: './src/index.js'
   },
    output: {
     path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
   },
   plugins: [
&lt;span class="gd"&gt;-    new HtmlWebpackPlugin()
&lt;/span&gt;&lt;span class="gi"&gt;+    new HtmlWebpackPlugin(),
+    extractCss
&lt;/span&gt;   ],
   module: {
     rules: [{
       test: /\.js$/,
       exclude: /node_modules/,
       use: 'babel-loader'
     }, {
       test: /\.css$/,
       exclude: /node_modules/,
&lt;span class="gd"&gt;-      use: [
-         {loader: 'style-loader'},
-         {loader: 'css-loader'}
-      ]
&lt;/span&gt;&lt;span class="gi"&gt;+      use: extractCss.extract({
+        use:[
+          {loader: 'css-loader'}
+        ],
+        fallback: 'style-loader'
+     })
&lt;/span&gt;     }]
   }
 };
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This disables &lt;code&gt;extractCss&lt;/code&gt; in developement mode in which case, &lt;code&gt;style&lt;/code&gt; tag is used to apply css. In production mode, &lt;code&gt;extractCss&lt;/code&gt; plugin extracts all the &lt;code&gt;css&lt;/code&gt; from &lt;code&gt;js&lt;/code&gt; bundles into their own files which is named according to the value of &lt;code&gt;filename&lt;/code&gt; used while declaring &lt;code&gt;extractCss&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now running &lt;code&gt;npm run build&lt;/code&gt; will create 3 files in &lt;code&gt;dist&lt;/code&gt; - &lt;code&gt;bundle.js&lt;/code&gt;, &lt;code&gt;index.css&lt;/code&gt; and &lt;code&gt;index.html&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;Update - Adding scss support&lt;/h4&gt;

&lt;p&gt;Let's add &lt;code&gt;scss&lt;/code&gt; parsing support to the webpack config file. For this you'll need &lt;code&gt;sass-loader&lt;/code&gt; which in turn needs &lt;code&gt;node-sass&lt;/code&gt;. Install these using -&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install node-sass sass-loader --save-dev&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, update &lt;code&gt;webpack.config.js&lt;/code&gt; so that webpack knows how to process imported scss files -&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight diff"&gt;&lt;code&gt; const path = require('path');
 const HtmlWebpackPlugin = require('html-webpack-plugin');
 const ExtractTextPlugin = require('extract-text-webpack-plugin');

 const env = process.env.NODE_ENV || 'development';
 const isDev = env === 'development';
 const isProd = env === 'production';

&lt;span class="gd"&gt;-const extractCss = new ExtractTextPlugin({
&lt;/span&gt;&lt;span class="gi"&gt;+const extractScss = new ExtractTextPlugin({
&lt;/span&gt;   filename: 'index.css',
   disable: isDev
 });

 module.exports = {
   entry: {
     bundle: './src/index.js'
   },
   output: {
     path: path.resolve(__dirname, 'dist'),
     filename: 'bundle.js'
   },
   plugins: [
     new HtmlWebpackPlugin(),
&lt;span class="gd"&gt;-    extractCss
&lt;/span&gt;&lt;span class="gi"&gt;+    extractScss
&lt;/span&gt;   ],
   module: {
     rules: [{
       test: /\.js$/,
       exclude: /node_modules/,
       use: 'babel-loader'
     }, {
&lt;span class="gd"&gt;-      test: /\.css$/,
&lt;/span&gt;&lt;span class="gi"&gt;+      test: /(\.css|\.scss)$/,
&lt;/span&gt;       exclude: /node_modules/,
&lt;span class="gd"&gt;-      use: extractCss.extract({
&lt;/span&gt;&lt;span class="gi"&gt;+      use: extractScss.extract({
&lt;/span&gt;         use:[
&lt;span class="gd"&gt;-          {loader: 'css-loader'}
&lt;/span&gt;&lt;span class="gi"&gt;+          {loader: 'css-loader'},
+          {loader: 'sass-loader'}
&lt;/span&gt;         ],
         fallback: 'style-loader'
       })
     }]
   }
 };
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now to test this out, rename &lt;code&gt;index.css&lt;/code&gt; to &lt;code&gt;index.scss&lt;/code&gt; and update its content with basic scss nesting -&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="nt"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&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;Update the import in &lt;code&gt;index.js&lt;/code&gt; -&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight diff"&gt;&lt;code&gt; import message from './message';
&lt;span class="gd"&gt;-import './index.css';
&lt;/span&gt;&lt;span class="gi"&gt;+import './index.scss';
&lt;/span&gt;
 const paragraph = document.createElement('p');
 paragraph.innerHTML = message;

 document.body.prepend(paragraph);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Test this by running &lt;code&gt;npm run dev&lt;/code&gt; and open the url in browser.&lt;/p&gt;

&lt;p&gt;This part concludes the usage of importing &lt;code&gt;css&lt;/code&gt; and &lt;code&gt;scss&lt;/code&gt; files in &lt;code&gt;js&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;Part 3&lt;/h2&gt;

&lt;p&gt;As the codebase of a project increases in size, it can become difficult to maintain a strict coding guideline if not taken care of at early stage. Also, as more people start contributing to a single project, they may bring their own style of coding which can result in code in various files looking different and it becomes painful for new developers to decide which style to follow. This problem is taken care of by using linters. They help in following a single strict guideline for writing code. Linters in javascript show many helpful messages like unused variables, missing semicolon (this may not be a problem in some projects), codes exceeding maximum permitted length, etc. Let's update our project to use &lt;code&gt;eslint&lt;/code&gt; to throw error when a particular guideline is not followed. For this, we need &lt;code&gt;eslint&lt;/code&gt; and &lt;code&gt;eslint-loader&lt;/code&gt;. Install them using -&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install eslint eslint-loader --save-dev&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now update &lt;code&gt;webpack.config.js&lt;/code&gt; to inform webpack to use &lt;code&gt;eslint-loader&lt;/code&gt; before passing it through &lt;code&gt;babel-loader&lt;/code&gt; -&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight diff"&gt;&lt;code&gt;const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

const env = process.env.NODE_ENV || 'development';
const isDev = env === 'development';
const isProd = env === 'production';

const extractScss = new ExtractTextPlugin({
  filename: 'index.css',
  disable: isDev
});

 module.exports = {
   entry: {
     bundle: './src/index.js'
   },
   output: {
     path: path.resolve(__dirname, 'dist'),
     filename: 'bundle.js'
   },
   plugins: [
     new HtmlWebpackPlugin(),
     extractScss
   ],
   module: {
     rules: [{
&lt;span class="gi"&gt;+      enforce: 'pre',
+      test: /\.js$/,
+      exclude: /node_modules/,
+      use: 'eslint-loader'
+    }, {
&lt;/span&gt;       test: /\.js$/,
       exclude: /node_modules/,
       use: 'babel-loader'
     }, {
       test: /(\.css|\.scss)$/,
       exclude: /node_modules/,
       use: extractScss.extract({
         use:[
           {loader: 'css-loader'},
           {loader: 'sass-loader'}
         ],
         fallback: 'style-loader'
       })
     }]
   }
 };
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Create a new file &lt;code&gt;.eslintrc&lt;/code&gt; at the top level of your project (alongside &lt;code&gt;package.json&lt;/code&gt;). In this file, you can define your own custom rules and the parser for &lt;code&gt;eslint&lt;/code&gt; to follow.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="s2"&gt;"parserOptions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"ecmaVersion"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"sourceType"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"module"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="s2"&gt;"extends"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"eslint:recommended"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;ecmaVersion&lt;/code&gt; allows eslint to recognise ES6 features, &lt;code&gt;sourceType: module&lt;/code&gt; allows the usage of &lt;code&gt;import&lt;/code&gt; and &lt;code&gt;export&lt;/code&gt; keywords. By default, there are no rules set for &lt;code&gt;eslint&lt;/code&gt;. So &lt;code&gt;"extends": "eslint:recommended"&lt;/code&gt; tells &lt;code&gt;eslint&lt;/code&gt; to use default recommended rules.&lt;/p&gt;

&lt;p&gt;At this point, you can run &lt;code&gt;npm run dev&lt;/code&gt;. In the console, you'll see that there are 2 same type of errors -&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;4:19  error  'document' is not defined  no-undef
7:1   error  'document' is not defined  no-undef
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This tells that the variable &lt;code&gt;document&lt;/code&gt; has not been defined (&lt;code&gt;no-undef&lt;/code&gt;) anywhere but is still being used. This can be fixed in 2 ways. To fix this, you'll need to use the &lt;code&gt;globals&lt;/code&gt; key in &lt;code&gt;.eslintrc&lt;/code&gt;. Update your &lt;code&gt;.eslintrc&lt;/code&gt; -&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight diff"&gt;&lt;code&gt; {
   "parserOptions": {
     "ecmaVersion": 6,
     "sourceType": "module"
   },
&lt;span class="gd"&gt;-  "extends": "eslint:recommended"
&lt;/span&gt;&lt;span class="gi"&gt;+  "extends": "eslint:recommended",
+. "globals": {
&lt;/span&gt;      "document": true
    }
 }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This tells &lt;code&gt;eslint&lt;/code&gt; that the variable &lt;code&gt;document&lt;/code&gt; is global and will be provided by the JS environment (in this case, browser). Now you can run &lt;code&gt;npm run dev&lt;/code&gt; without any error. You can also add a linting command to &lt;code&gt;package.json&lt;/code&gt; to see lint error independently of webpack. Update &lt;code&gt;package.json&lt;/code&gt; -&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight diff"&gt;&lt;code&gt;{
  "name": "hello-world",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "NODE_ENV=production webpack",
&lt;span class="gd"&gt;-   "dev": "NODE_ENV=development webpack-dev-server --content-base dist --hot"
&lt;/span&gt;&lt;span class="gi"&gt;+   "dev": "NODE_ENV=development webpack-dev-server --content-base dist --hot",
+   "lint": "eslint ./src --ext .js"
&lt;/span&gt;  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-env": "^1.6.1",
    "css-loader": "^0.28.9",
    "eslint": "^4.18.1",
    "eslint-loader": "^2.0.0",
    "extract-text-webpack-plugin": "^3.0.2",
    "html-webpack-plugin": "^2.30.1",
    "node-sass": "^4.7.2",
    "sass-loader": "^6.0.6",
    "style-loader": "^0.20.2",
    "webpack": "^3.11.0",
    "webpack-dev-server": "^2.11.1"
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now you can run &lt;code&gt;npm run lint&lt;/code&gt; in your console and check for any linting errors regardless of whether you are bundling the project or not. This can also be used in git pre-commit hooks to not allow commits if &lt;code&gt;eslint&lt;/code&gt; throws any error. &lt;code&gt;eslint ./src --ext .js&lt;/code&gt; tells &lt;code&gt;eslint&lt;/code&gt; to check for errors in all files in &lt;code&gt;src&lt;/code&gt; dirctory with &lt;code&gt;js&lt;/code&gt; extension. You can also add an optional &lt;code&gt;--fix&lt;/code&gt; option to this command which automatically tries to fix errors so that you don't have to.&lt;/p&gt;

&lt;p&gt;You can also add your own rules in &lt;code&gt;.eslintrc&lt;/code&gt; file as per your requirements. The &lt;code&gt;eslint:recommended&lt;/code&gt; option does not allow you to use &lt;code&gt;console.log&lt;/code&gt; in your code (recommended way is to use a logging module). You can add a rule to tell &lt;code&gt;eslint&lt;/code&gt; to show a warning in &lt;code&gt;console.log&lt;/code&gt; statements instead of an error. Update &lt;code&gt;.eslintrc&lt;/code&gt; file -&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight diff"&gt;&lt;code&gt; {
   "parserOptions": {
     "ecmaVersion": 6,
     "sourceType": "module"
   },
   "extends": "eslint:recommended",
   "globals": {
&lt;span class="gd"&gt;-    "document": true
&lt;/span&gt;&lt;span class="gi"&gt;+    "document": true,
+    "console": true
&lt;/span&gt;&lt;span class="gd"&gt;-  }
&lt;/span&gt;&lt;span class="gi"&gt;+  },
+  "rules": {
+    "no-console": 1
+  }
&lt;/span&gt; }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;"no-console": 1&lt;/code&gt; tells &lt;code&gt;eslint&lt;/code&gt; to show a warning instead of an error. Other values are &lt;code&gt;0&lt;/code&gt; (turn off &lt;code&gt;eslint&lt;/code&gt; for this rule) and &lt;code&gt;2&lt;/code&gt; (throw an error if this rule is violated). There are some standard javascript style guides that a lot of companies use (instead of the default &lt;code&gt;eslint:recommended&lt;/code&gt;). One of them is airbnb's &lt;a href="https://github.com/airbnb/javascript"&gt;javascript style guide&lt;/a&gt; which adds a lot of well accepted linting rules. You can use this instead of the current one. Let's add that to our configuration. It requires the installation of an extra &lt;code&gt;eslint-plugin-import&lt;/code&gt; dependency. Install &lt;code&gt;eslint-config-airbnb-base&lt;/code&gt; and it's dependencies using -&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npx install-peerdeps --dev eslint-config-airbnb-base&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now update &lt;code&gt;.eslintrc&lt;/code&gt; -&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight diff"&gt;&lt;code&gt; {
&lt;span class="gd"&gt;-  "parserOptions": {
-   "ecmaVersion": 6,
-   "sourceType": "module"
-  },
-  "extends": "eslint:recommended",
&lt;/span&gt;&lt;span class="gi"&gt;+  "extends": "airbnb-base",
&lt;/span&gt;   "globals": {
     "document": true,
     "console": true
   },
   "rules": {
     "no-console": 1
   }
 }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;airbnb-base&lt;/code&gt; has &lt;code&gt;parserOptions&lt;/code&gt; internally. So it has been removed. Now, when you run &lt;code&gt;npm run dev&lt;/code&gt;, you'll get an error-&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...hello-world/src/message.js
1:16  error  Strings must use singlequote  quotes
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That's because &lt;code&gt;airbnb-base&lt;/code&gt; has a rule to use single quotes for strings instead of double quotes. Running &lt;code&gt;npm run lint&lt;/code&gt; with &lt;code&gt;--fix&lt;/code&gt; option will automatically change &lt;code&gt;"&lt;/code&gt; to &lt;code&gt;'&lt;/code&gt; in &lt;code&gt;src/message.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This concludes the usage of &lt;code&gt;eslint&lt;/code&gt; to enforce code quality in your code.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Part 4 will update this project to start developing ReactJS apps.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This post was originally published on &lt;a href="http://bitwiser.in/2018/02/22/setup-javascript-webpack-project.html"&gt;bitwiser.in&lt;/a&gt;&lt;/p&gt;


</description>
      <category>javascript</category>
      <category>webpack</category>
      <category>babel</category>
      <category>es6</category>
    </item>
  </channel>
</rss>
