{"componentChunkName":"component---src-templates-post-tsx","path":"/develop-node.js-projects-with-wsl-2","result":{"data":{"markdownRemark":{"html":"<p>I have a PC in my apartment. But I never use it as a development machine. Recently, I feel tired of bringing the Mac mini everyday. So I decided to set up a development environement on my PC. Here is what I've done for Node.js projects.</p>\n<h2>Network Issues</h2>\n<div class=\"custom-block info\"><div class=\"custom-block-body\"><p>If you don't live in mainland China, you can just skip this section.</p></div></div>\n<p>If you want to be a competent developer in mainland China, you must resolve <a href=\"https://en.wikipedia.org/wiki/Great_Firewall\" target=\"_blank\" rel=\"noopener noreferrer\">some well-known network issues</a>. But I just hadn't expected that these issues showed up so early.</p>\n<p>First, find the IP address of Windows by running <a href=\"https://github.com/Microsoft/WSL/issues/1032#issuecomment-535764958\" target=\"_blank\" rel=\"noopener noreferrer\"><code class=\"language-text\">cat /etc/resolv.conf</code></a>. You will get something like this.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\"># This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:\n# [network]\n# generateResolvConf = false\nnameserver 192.168.163.65</code></pre></div>\n<p>The IP address after <code class=\"language-text\">nameserver</code> is the host. Suppose your proxy runs at port 7890, you can set the HTTP and HTTPS proxy by following commands.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token builtin class-name\">export</span> <span class=\"token assign-left variable\">https_proxy</span><span class=\"token operator\">=</span>http://192.168.163.65:7890\n<span class=\"token builtin class-name\">export</span> <span class=\"token assign-left variable\">http_proxy</span><span class=\"token operator\">=</span>http://192.168.163.65:7890</code></pre></div>\n<p>And, of course, proxy for git. This step is of two parts. First, you have to tell ssh that use SSH over HTTPS for github.com by adding following content to <code class=\"language-text\">~/.ssh/config</code>.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">Host github.com\n  Hostname ssh.github.com\n  Port 443</code></pre></div>\n<p>Second, let git use the proxy.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">git</span> config --global http.proxy http://192.168.163.65:7890</code></pre></div>\n<p>Remember to set the proxy in root user because <code class=\"language-text\">apt</code> runs at root.</p>\n<h2>Intall Node.js and Yarn</h2>\n<p>Install Node.js by following <a href=\"https://github.com/nodesource/distributions#installation-instructions\" target=\"_blank\" rel=\"noopener noreferrer\">commands in NodeSource</a>. For example, I like to use the latest (15).</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token comment\"># Using Debian, as root</span>\n<span class=\"token function\">curl</span> -sL https://deb.nodesource.com/setup_15.x <span class=\"token operator\">|</span> <span class=\"token function\">sudo</span> <span class=\"token function\">bash</span> -\n<span class=\"token function\">sudo</span> <span class=\"token function\">apt-get</span> <span class=\"token function\">install</span> -y nodejs</code></pre></div>\n<p>Then, install Yarn according to <a href=\"https://classic.yarnpkg.com/en/docs/install#debian-stable\" target=\"_blank\" rel=\"noopener noreferrer\">its docs</a>.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">curl</span> -sS https://dl.yarnpkg.com/debian/pubkey.gpg <span class=\"token operator\">|</span> <span class=\"token function\">sudo</span> apt-key <span class=\"token function\">add</span> -\n<span class=\"token builtin class-name\">echo</span> <span class=\"token string\">\"deb https://dl.yarnpkg.com/debian/ stable main\"</span> <span class=\"token operator\">|</span> <span class=\"token function\">sudo</span> <span class=\"token function\">tee</span> /etc/apt/sources.list.d/yarn.list\n<span class=\"token function\">sudo</span> <span class=\"token function\">apt</span> update <span class=\"token operator\">&amp;&amp;</span> <span class=\"token function\">sudo</span> <span class=\"token function\">apt</span> <span class=\"token function\">install</span> <span class=\"token function\">yarn</span></code></pre></div>\n<h2>Install Dependencies</h2>\n<p>Because WSL 2 is a barely naked install, i.e. without many necessary packages by default. In order to build some Gatsby dependencies, we must install some build tools first.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token comment\"># For pngquant</span>\n<span class=\"token function\">sudo</span> <span class=\"token function\">apt</span> <span class=\"token function\">install</span> libpng-devel automake libtool autoconf\n<span class=\"token comment\"># For mozjpeg</span>\n<span class=\"token function\">sudo</span> <span class=\"token function\">apt</span> <span class=\"token function\">install</span> nasm pkg-config</code></pre></div>\n<p>Then, run <code class=\"language-text\">yarn</code>. Everything will be okay.</p>\n<h2>Signing Commits Using GPG</h2>\n<p>Just follow steps in <a href=\"https://gist.github.com/ankurk91/c4f0e23d76ef868b139f3c28bde057fc\" target=\"_blank\" rel=\"noopener noreferrer\">this gist</a>.</p>\n<p>I encountered this error when I signed off commits.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">$ git commit -S\nerror: gpg failed to sign the data\nfatal: failed to write commit object</code></pre></div>\n<p>According to <a href=\"https://stackoverflow.com/questions/57619460/trying-to-sign-commits-on-git-using-gpg-on-wsl-but-does-not-work\" target=\"_blank\" rel=\"noopener noreferrer\">this post</a>, just run <code class=\"language-text\">export GPG_TTY=$(tty)</code> before using GPG.</p>","frontmatter":{"title":"Develop Node.js Projects with WSL 2","date":"December 05, 2020","tags":["Windows","WSL"],"language":"en"}},"site":{"siteMetadata":{"title":"Luyu Cheng"}}},"pageContext":{"id":"0eb9646d-633c-54ec-bbdf-73f28b25ad2f"}},"staticQueryHashes":["3649515864","63159454","684267432"]}