<?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: Ayesha Iftikhar</title>
    <description>The latest articles on DEV Community by Ayesha Iftikhar (@ayesha_iftikhar_cb305fc47).</description>
    <link>https://dev.to/ayesha_iftikhar_cb305fc47</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%2F3407569%2F81cfd268-140a-435e-88ea-73398ecf6eea.jpg</url>
      <title>DEV Community: Ayesha Iftikhar</title>
      <link>https://dev.to/ayesha_iftikhar_cb305fc47</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ayesha_iftikhar_cb305fc47"/>
    <language>en</language>
    <item>
      <title>How to Generate a Secure JWT Secret Key in Node.js</title>
      <dc:creator>Ayesha Iftikhar</dc:creator>
      <pubDate>Wed, 13 Aug 2025 10:35:52 +0000</pubDate>
      <link>https://dev.to/ayesha_iftikhar_cb305fc47/how-to-generate-a-secure-jwt-secret-key-in-nodejs-335f</link>
      <guid>https://dev.to/ayesha_iftikhar_cb305fc47/how-to-generate-a-secure-jwt-secret-key-in-nodejs-335f</guid>
      <description>&lt;p&gt;When working with JWT (JSON Web Tokens) in a Node.js application, your secret key plays a critical role in signing and verifying tokens.&lt;/p&gt;

&lt;p&gt;A weak or predictable key can compromise your application’s security, so it’s best to generate it randomly.&lt;/p&gt;

&lt;p&gt;Here’s how you can easily create a strong JWT secret key using Node.js:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Add Script to &lt;code&gt;package.json&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Open your package.json and inside the "scripts" section, add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
  "jwt-key": "node -e \"console.log(require('crypto').randomBytes(32).toString('hex'))\""
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Run the script&lt;/strong&gt;&lt;br&gt;
In your terminal run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run jwt-key
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will get:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a47f7c20e4f9a87c6d0af6e6c8c4bc25d2a8e7c4a9f5c30f72db7d9a48f1c3d2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Copy that string into your &lt;code&gt;.env&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you use this often, you can even combine it with a direct write to .env so it replaces the JWT secret automatically:&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;"scripts": {
  "jwt-key": "echo JWT_SECRET=$(node -e \"console.log(require('crypto').randomBytes(32).toString('hex'))\") &amp;gt; .env"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will overwrite &lt;code&gt;.env&lt;/code&gt; with the new secret each time.&lt;/p&gt;

</description>
      <category>node</category>
      <category>jwt</category>
      <category>npm</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
