<?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: Javien Lee</title>
    <description>The latest articles on DEV Community by Javien Lee (@javien).</description>
    <link>https://dev.to/javien</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%2F780203%2F6d08f676-7ba9-4fa7-b2d1-357e5e8f1157.jpg</url>
      <title>DEV Community: Javien Lee</title>
      <link>https://dev.to/javien</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/javien"/>
    <language>en</language>
    <item>
      <title>How to use Prettier plugins with Yarn PnP in VSCode?</title>
      <dc:creator>Javien Lee</dc:creator>
      <pubDate>Sat, 07 Oct 2023 05:59:00 +0000</pubDate>
      <link>https://dev.to/javien/how-to-use-prettier-plugin-with-yarn-pnp-in-vscode-4pf8</link>
      <guid>https://dev.to/javien/how-to-use-prettier-plugin-with-yarn-pnp-in-vscode-4pf8</guid>
      <description>&lt;p&gt;When using Yarn Berry with Prettier in VS Code, you might encounter issues loading Prettier plugins. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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="c1"&gt;// ❌ This doesn't work!&lt;/span&gt;
  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;prettier-plugin-organize-imports&lt;/span&gt;&lt;span class="dl"&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;This results in an error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[ERROR] Error resolve node module 'prettier-plugin-organize-imports'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;For this explanation, I used the following versions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;prettier: v3.0.3&lt;/li&gt;
&lt;li&gt;prettier-vscode: v10.1.0&lt;/li&gt;
&lt;li&gt;yarn: v3.6.4&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note that the solution might not work if major versions are different.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Quick Fix (TL;DR)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Unplug your plugins
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;yarn unplug &amp;lt;your-plugin&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example, to unplug prettier-plugin-organize-imports, use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;yarn unplug prettier-plugin-organize-imports
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;You can find more information about &lt;code&gt;yarn unplug&lt;/code&gt; &lt;a href="https://yarnpkg.com/cli/unplug" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  2. Edit .prettierrc.js
&lt;/h3&gt;

&lt;p&gt;🚨 Important: Your Prettier configuration file &lt;strong&gt;must be a &lt;code&gt;.prettierrc.js&lt;/code&gt; file&lt;/strong&gt; (not &lt;code&gt;.json&lt;/code&gt;, &lt;code&gt;.yml&lt;/code&gt;, etc.) to use this method. We'll be using &lt;code&gt;require.resolve&lt;/code&gt;, which is a Node.js function that resolves the absolute path to a module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/** @type {import("prettier").Options} */&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;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;your-plugin&lt;/span&gt;&lt;span class="dl"&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;Replace &lt;code&gt;'your-plugin'&lt;/code&gt; with the name of your plugin. For example, if you're using &lt;code&gt;prettier-plugin-organize-imports&lt;/code&gt;, the configuration should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/** @type {import("prettier").Options} */&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;semi&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;singleQuote&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;trailingComma&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;all&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;organizeImportsSkipDestructiveCodeActions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;prettier-plugin-organize-imports&lt;/span&gt;&lt;span class="dl"&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;h3&gt;
  
  
  3. Reload Window
&lt;/h3&gt;

&lt;p&gt;Finally, you need to reload VS Code.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Press &lt;code&gt;F1&lt;/code&gt; or &lt;code&gt;cmd+shift+P&lt;/code&gt; to open the command palette.&lt;/li&gt;
&lt;li&gt;Type "reload window".&lt;/li&gt;
&lt;li&gt;Select "Developer: Reload Window".&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcuhck28l3awkvc6ew9fk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcuhck28l3awkvc6ew9fk.png" alt="Open command prompt, type 'reload window' and run 'Developer: Reload Window'" width="751" height="197"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Alternatively, you can simply close and reopen VS Code.&lt;/p&gt;

&lt;p&gt;🎉 You should now have Prettier working correctly with your plugins! 🎉&lt;/p&gt;




&lt;h2&gt;
  
  
  Okay, so what was the problem?
&lt;/h2&gt;

&lt;p&gt;The core issue lies in how Yarn Berry (with PnP enabled) manages packages. Instead of storing packages in a traditional &lt;code&gt;node_modules&lt;/code&gt; directory, Yarn Berry often stores them as zip files in its folder (&lt;code&gt;.yarn/cache&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;When you specify a plugin as a plain string:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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="c1"&gt;// ❌ Doesn't work&lt;/span&gt;
  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;prettier-plugin-organize-imports&lt;/span&gt;&lt;span class="dl"&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;&lt;code&gt;prettier-vscode&lt;/code&gt; attempts to load the plugin on its own. However, because Yarn Berry uses zip files and the &lt;code&gt;.pnp.cjs&lt;/code&gt; loader, &lt;code&gt;prettier-vscode&lt;/code&gt; fails to locate the plugin, resulting in an error like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;["ERROR" - 11:43:14 PM] Error resolve node module 'prettier-plugin-organize-imports'
Error: Error resolve node module 'prettier-plugin-organize-imports'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even when providing an absolute path using &lt;code&gt;require.resolve&lt;/code&gt;, you might encounter issues because the path points to a zip file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Cannot find module '(~).yarn/__virtual__/prettier-plugin-organize-imports-virtual-9cbb536fbb/0/cache/prettier-plugin-organize-imports-npm-3.2.3-7f40e110b3-e97dd707ce.zip/node_modules/prettier-plugin-organize-imports/index.js' imported from (~).yarn/unplugged/prettier-npm-3.0.3-fced695dae/node_modules/prettier/index.mjs
Did you mean to import (~).yarn/__virtual__/prettier-plugin-organize-imports-virtual-9cbb536fbb/0/cache/prettier-plugin-organize-imports-npm-3.2.3-7f40e110b3-e97dd707ce.zip/node_modules/prettier-plugin-organize-imports/index.js?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;prettier-vscode&lt;/code&gt; doesn't inherently know how to handle these zip files or use the &lt;code&gt;.pnp.cjs&lt;/code&gt; loader.&lt;/p&gt;

&lt;p&gt;By using &lt;code&gt;yarn unplug&lt;/code&gt;, you extract the plugin from the zip file, making it directly accessible. Then, &lt;code&gt;require.resolve&lt;/code&gt; provides the correct path to the extracted plugin, allowing &lt;code&gt;prettier-vscode&lt;/code&gt; to load it successfully.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The primary challenge arises from Yarn PnP's use of zip files, which &lt;code&gt;prettier-vscode&lt;/code&gt; cannot directly interpret. Unplugging the plugin and using &lt;code&gt;require.resolve&lt;/code&gt; to specify the path resolves this issue.&lt;/p&gt;

&lt;p&gt;If you're still encountering problems, feel free to provide more details about your specific setup, and I'll do my best to assist you!&lt;/p&gt;

</description>
      <category>yarn</category>
      <category>prettier</category>
      <category>vscode</category>
    </item>
    <item>
      <title>Do you use semicolon in JS?</title>
      <dc:creator>Javien Lee</dc:creator>
      <pubDate>Sun, 05 Jun 2022 06:53:31 +0000</pubDate>
      <link>https://dev.to/javien/do-you-use-semicolon-in-js-42b1</link>
      <guid>https://dev.to/javien/do-you-use-semicolon-in-js-42b1</guid>
      <description>&lt;p&gt;Do you usually use semicolon when you program? And why?&lt;/p&gt;

&lt;p&gt;In my case, I don't use semicolon. I know how ASI(Auto Semicolon Insertion) works and it's rule makes my code more clean and readable.&lt;/p&gt;

&lt;p&gt;Let me give you an example. The code below is an incorrect way to use ASI.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bar&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;because ASI will fix the code like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bar&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, most of developer do not write code like this. In that code, we can't figure out what [1, 2] means. So we store it in a variable with name, that everyone can understand.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-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;ids&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&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;newIds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ids&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I'm not blaming people for using semicolons in their code, but I don't think there's a particular reason for me to use semicolons. Please share your opinion :p&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
