<?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>Forem: Surjoyday Talukdar</title>
    <description>The latest articles on Forem by Surjoyday Talukdar (@surjoyday_kt).</description>
    <link>https://forem.com/surjoyday_kt</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%2F1238650%2Fcaa9318d-1555-4a7a-88b0-6dd253a31b56.jpg</url>
      <title>Forem: Surjoyday Talukdar</title>
      <link>https://forem.com/surjoyday_kt</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/surjoyday_kt"/>
    <language>en</language>
    <item>
      <title>I ran npm run dev but it is saying next command not found</title>
      <dc:creator>Surjoyday Talukdar</dc:creator>
      <pubDate>Thu, 13 Mar 2025 21:10:43 +0000</pubDate>
      <link>https://forem.com/surjoyday_kt/i-ran-npm-run-dev-but-it-is-saying-next-command-not-found-20b1</link>
      <guid>https://forem.com/surjoyday_kt/i-ran-npm-run-dev-but-it-is-saying-next-command-not-found-20b1</guid>
      <description>&lt;div class="ltag__link"&gt;
  &lt;a href="/surjoyday_kt" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&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%2Fuser%2Fprofile_image%2F1238650%2Fcaa9318d-1555-4a7a-88b0-6dd253a31b56.jpg" alt="surjoyday_kt"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://dev.to/surjoyday_kt/fix-sh-next-command-not-found-in-nextjs-quick-easy-solution-2ilf" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Fix 'sh: next: command not found' in Next.js – Quick &amp;amp; Easy Solution!&lt;/h2&gt;
      &lt;h3&gt;Surjoyday Talukdar ・ Mar 13&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#webdev&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#programming&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#nextjs&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


</description>
      <category>nextjs</category>
      <category>npm</category>
    </item>
    <item>
      <title>Fix 'sh: next: command not found' in Next.js – Quick &amp; Easy Solution!</title>
      <dc:creator>Surjoyday Talukdar</dc:creator>
      <pubDate>Thu, 13 Mar 2025 21:09:55 +0000</pubDate>
      <link>https://forem.com/surjoyday_kt/fix-sh-next-command-not-found-in-nextjs-quick-easy-solution-2ilf</link>
      <guid>https://forem.com/surjoyday_kt/fix-sh-next-command-not-found-in-nextjs-quick-easy-solution-2ilf</guid>
      <description>&lt;p&gt;I recommend following the steps below, as suggested by &lt;a href="https://github.com/vercel/next.js/issues/8945#issuecomment-537877585" rel="noopener noreferrer"&gt;timneutkens&lt;/a&gt; in the Next.js GitHub issue #8945. Installing Next.js globally can cause multiple issues when deploying to production.&lt;/p&gt;

&lt;p&gt;Steps to Fix:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;In your terminal type:&lt;br&gt;
&lt;code&gt;nano ~/.zshrc&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Scroll to the very end of the .zshrc file and add the following lines to include the locally installed node_modules/.bin directory in your PATH:&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;export PATH="./node_modules/.bin:$PATH"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Save and exit the file:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Press CTRL + X to exit.&lt;/li&gt;
&lt;li&gt;Press Y to confirm saving.&lt;/li&gt;
&lt;li&gt;Press Enter to save the changes.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Apply the changes by running:
&lt;code&gt;source ~/.zshrc&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This ensures that your locally installed dependencies are prioritized without requiring a global installation of Next.js.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>nextjs</category>
    </item>
    <item>
      <title>How to Handle Errors When Accessing Context Outside the Provider in React</title>
      <dc:creator>Surjoyday Talukdar</dc:creator>
      <pubDate>Sat, 21 Sep 2024 21:40:40 +0000</pubDate>
      <link>https://forem.com/surjoyday_kt/how-to-handle-errors-when-accessing-context-outside-the-provider-in-react-41ce</link>
      <guid>https://forem.com/surjoyday_kt/how-to-handle-errors-when-accessing-context-outside-the-provider-in-react-41ce</guid>
      <description>&lt;p&gt;When working with &lt;strong&gt;React’s Context API&lt;/strong&gt;, it’s important to handle cases where components try to access context outside the Provider. If you don't, it could lead to unintended results or hard-to-track bugs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Issue&lt;/strong&gt;&lt;br&gt;
When you create a context using &lt;code&gt;createContext()&lt;/code&gt;, you have the option to pass in a default value. This default value is what gets returned if a component tries to access the context outside of the Provider.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If you don’t pass a default value to &lt;code&gt;createContext()&lt;/code&gt;, accessing the context outside a Provider will return &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you do pass a default value (like &lt;code&gt;null&lt;/code&gt; or any other value), that value will be returned instead when the context is accessed outside of a Provider.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const PostContext = React.createContext(null); // Default value is null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, if a component tries to access PostContext without being wrapped in a Provider, it will return &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Fix: A Custom Hook with Error Handling&lt;/strong&gt;&lt;br&gt;
To avoid situations where the context is accessed outside its Provider, we can create a custom hook that throws an error if the context is accessed incorrectly. This is useful for catching mistakes early in development.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function usePosts() {
  const context = useContext(PostContext);

  if (context === null) {
    // checking for "null" because that's the default value passed in createContext 
    throw new Error("usePosts must be used within a PostProvider");
  }

  return context;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why This Matters&lt;/strong&gt;&lt;br&gt;
If no error handling is in place, accessing context outside of its Provider could return &lt;code&gt;null&lt;/code&gt;, &lt;code&gt;undefined&lt;/code&gt;, or whatever default value you used. This can lead to hard-to-debug issues in your app. By throwing an error, it’s much easier to catch and fix the problem early.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Managing State in Multiple Instances of the Same Component in React</title>
      <dc:creator>Surjoyday Talukdar</dc:creator>
      <pubDate>Thu, 22 Aug 2024 19:56:12 +0000</pubDate>
      <link>https://forem.com/surjoyday_kt/managing-state-in-multiple-instances-of-the-same-component-in-react-5dfk</link>
      <guid>https://forem.com/surjoyday_kt/managing-state-in-multiple-instances-of-the-same-component-in-react-5dfk</guid>
      <description>&lt;p&gt;When you're working with React and have multiple instances of the same component, managing state can get tricky. Depending on how your components need to interact, you'll want to handle state differently. Here’s what I’ve found works well.&lt;/p&gt;

&lt;h2&gt;
  
  
  Independent Instances: Keep State Inside the Component
&lt;/h2&gt;

&lt;p&gt;If your components don’t need to talk to each other, it’s best to keep their state inside the component. This way, each instance has its own state, and changes in one don’t affect the others.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Counter() {
  const [count, setCount] = useState(0);

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;Count: {count}&amp;lt;/p&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; setCount(count + 1)}&amp;gt;Increment&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

// Usage
&amp;lt;Counter /&amp;gt; // Instance 1
&amp;lt;Counter /&amp;gt; // Instance 2

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, each Counter component keeps track of its own count. So, if you click the button in one counter, it doesn’t change the count in the other.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dependent Instances: Manage State in the Parent Component
&lt;/h2&gt;

&lt;p&gt;But if the components need to share some state or work in a coordinated manner, it’s better to move the state up to the parent component. The parent can manage the shared state and pass it down as props. This ensures that all instances stay in sync and work together smoothly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Parent() {
  const [sharedCount, setSharedCount] = useState(0);

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;Total Count: {sharedCount}&amp;lt;/p&amp;gt;
      &amp;lt;Counter count={sharedCount} setCount={setSharedCount} /&amp;gt;
      &amp;lt;Counter count={sharedCount} setCount={setSharedCount} /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

function Counter({ count, setCount }) {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;Count: {count}&amp;lt;/p&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; setCount(count + 1)}&amp;gt;Increment&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach works because when the state is in the parent component, any update to that state triggers a re-render of all instances, ensuring they all display the latest UI. If the state were kept in each instance separately, only the instance with the state change would re-render, leading to inconsistent UI across instances.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Examples from My Projects&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I figured this out while building an accordion component. Here are two examples from my own work:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Independent Accordion Instances: &lt;a href="https://codesandbox.io/p/sandbox/adoring-zhukovsky-ly9fmt" rel="noopener noreferrer"&gt;example&lt;/a&gt;. In this setup, each accordion instance works independently.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Dependent Accordion Instances: &lt;a href="https://codesandbox.io/p/sandbox/headless-currying-xn3lph" rel="noopener noreferrer"&gt;example&lt;/a&gt;. In this version, all accordion instances depend on each other and stay in sync.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Quick Recap&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If components work separately, keep state inside each component.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If they need to share state or work together in a coordinated manner, manage the state in the parent.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This approach made a big difference for me when building these accordion examples. Hope it helps you too!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>The Story behind typeof null returning "object" in Javascript</title>
      <dc:creator>Surjoyday Talukdar</dc:creator>
      <pubDate>Sat, 10 Aug 2024 22:46:55 +0000</pubDate>
      <link>https://forem.com/surjoyday_kt/the-story-behind-typeof-null-returning-object-in-javascript-2287</link>
      <guid>https://forem.com/surjoyday_kt/the-story-behind-typeof-null-returning-object-in-javascript-2287</guid>
      <description>&lt;p&gt;Like in many languages, JavaScript represents objects as references (pointers) to memory locations where the object data is stored. When JavaScript was created, &lt;code&gt;null&lt;/code&gt; was represented as a special pointer value that didn't point to any memory location (a "null pointer"). When the &lt;code&gt;typeof&lt;/code&gt; operator was later introduced, it used the same internal mechanism of the JavaScript engine to check the type of values. Since &lt;code&gt;null&lt;/code&gt; was represented as a special type of object (a null pointer), the &lt;code&gt;typeof&lt;/code&gt; operator returns "object" for null. This behavior, though technically a bug, has not been fixed to maintain JavaScript's backward compatibility.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>learning</category>
      <category>development</category>
    </item>
    <item>
      <title>What happens each time that we require a module by calling the require function with the “module” name as the argument.</title>
      <dc:creator>Surjoyday Talukdar</dc:creator>
      <pubDate>Sun, 09 Jun 2024 13:32:41 +0000</pubDate>
      <link>https://forem.com/surjoyday_kt/what-happens-each-time-that-we-require-a-module-by-calling-the-require-function-with-the-module-name-as-the-argument-14cf</link>
      <guid>https://forem.com/surjoyday_kt/what-happens-each-time-that-we-require-a-module-by-calling-the-require-function-with-the-module-name-as-the-argument-14cf</guid>
      <description>&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%2Fxie8dbg9tbbo92sa3dac.jpeg" 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%2Fxie8dbg9tbbo92sa3dac.jpeg" alt="what-happens-when-we-rquire-a-module-steps" width="800" height="183"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;First the path to the require module is resolved and the file is loaded.&lt;br&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%2Fyykx1lletfa5pgxq1e5h.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%2Fyykx1lletfa5pgxq1e5h.png" alt="requiring-and-loading-module" width="800" height="401"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After the module is loaded, the modules code is wrapped into a special function which will give us access to a couple of special objects.&lt;br&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%2Fq7pwhq4pyt2zbyxbpqee.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%2Fq7pwhq4pyt2zbyxbpqee.png" alt="wrapping-module" width="800" height="403"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The NodeJS runtime takes the code of our module and puts it inside an &lt;code&gt;IIFE&lt;/code&gt;  (the wrapper function is an IIFE), so it means Node does not directly execute the code that we write in the module and  that we required using the &lt;code&gt;require&lt;/code&gt; function, but instead its the “ &lt;code&gt;wrapper (IIFE)&lt;/code&gt; &lt;strong&gt;function&lt;/strong&gt;” that will contain our code in it’s body and will then execute our code . The “wrapper IIFE” is also the one that passes the “&lt;code&gt;exports&lt;/code&gt;”, “&lt;code&gt;require&lt;/code&gt;”, “&lt;code&gt;module&lt;/code&gt;”, “&lt;code&gt;__dirname&lt;/code&gt;” “&lt;code&gt;__filename&lt;/code&gt;” into the module (file) and that is why in every module we automatically have access to stuff like the &lt;code&gt;require&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;→ By doing this Node achieve 2 important things :&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;i. Firstly doing this gives developer access to all the variables like “require”, “__filename”, etc.&lt;br&gt;
ii. Secondly it keeps the “&lt;strong&gt;top-level variables&lt;/strong&gt; ( variables declared outside of any function )” that we define in our modules private, so scoped only to the current module, instead of leaking everything to the global object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt; : If we have 2 modules and we export module 1 to module 2 then all the top-level variables of module 1 are now scoped inside the wrapper function of the module 2 instead of leaking the everything from the module 1 into the global object.  It promotes modularity, prevents naming conflicts, and offers controlled access to functionalities within modules&lt;/p&gt;

&lt;p&gt;&lt;em&gt;math.js (Module 1):&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Top-level variable (private)
const PI = 3.14159;

function add(a, b) {
  return a + b;
}

exports.add = add; // Expose the add function
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;app.js (Module 2):&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const math = require('./math');

console.log(math.add(5, 3)); // Output: 8 (using the exported function)

// Cannot access PI directly from math module
// console.log(PI); // This would result in an error

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3.After that the code in the module’s “wrapper function” is EXECUTED by the Node.JS runtime.&lt;/p&gt;

&lt;p&gt;4.Now after the code execution it time for the “require” function to return something and whats returns is the exports of the required module. This exports are stored in the “module.exports” object &lt;/p&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%2F7lbxthncvmhcfxe8yxis.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%2F7lbxthncvmhcfxe8yxis.png" alt="returning-exports" width="800" height="402"&gt;&lt;/a&gt;&lt;br&gt;
→ When to use “&lt;code&gt;module.exports&lt;/code&gt;” or simply “&lt;code&gt;exports&lt;/code&gt;”&lt;/p&gt;

&lt;p&gt;i. We use  &lt;code&gt;module.exports&lt;/code&gt;  to export single variable, e.g. one class or one function and set it equal to the variable (&lt;code&gt;module.exports = Calculator&lt;/code&gt; )&lt;br&gt;
ii. We use &lt;code&gt;exports&lt;/code&gt; when we want to export multiple named exports like multiple function, for example , &lt;code&gt;exports.add = (a+b) =&amp;gt; a+b&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;5.And finally the entire module gets “Cached” after the first time they are loaded, meaning if we require the same module multiple times we always get the same result and the code and modules is only executed in the first call , in subsequent calls the result is then retrieved from the cache.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>udemy</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
