Linkedin invalid url when requesting for access token: Nodejs

I’m having a challenge when requesting for linkedin access token for sign in with linkedin product.

> Social [email protected] dev
> node server --watch
http://localhost:3001
error TypeError: Failed to parse URL from POST https://www.linkedin.com/oauth/v2/accessToken?code=AQR_FCYP4tOggx6VpjiWj3gKU8fLKZzloCDcc0BJAifQ1ECmnpgKE8B2aQvvSPol-_VodYfIgAy-fwvSWd48V69c4tTme5mau6U4EPBptZDdjpbrdLmj8rs5cCNTV4eTfngmJgUUlsN2CD1THp34FJ0SK0DzpomMzft0Jgb3ySRCw1mUbJG3kOywbhAwaJQbGjHMA2yWvAuHSWYEm_8&grant_type=authorization_code&client_id=idq&client_secret=secret&redirect_uri=http%3A%2F%2Flocalhost%3A3001%2Fapi%2Flinkedin%2Fredirect
    at Object.fetch (node:internal/deps/undici/undici:11372:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async RedirectHelper (file:///C:/Users/paull/Documents/projects/others/web/web%20servers/sign_with_linkedin/AuthHelper.js:24:18) {
      at new URL (node:internal/url:775:36)
      at new _Request (node:internal/deps/undici/undici:5055:25)
      at fetch2 (node:internal/deps/undici/undici:9195:25)
      at Object.fetch (node:internal/deps/undici/undici:11370:18)
      at fetch (node:internal/process/pre_execution:282:25)
      at RedirectHelper (file:///C:/Users/paull/Documents/projects/others/web/web%20servers/sign_with_linkedin/AuthHelper.js:24:24)
      at file:///C:/Users/paull/Documents/projects/others/web/web%20servers/sign_with_linkedin/server.js:29:14
      at Layer.handle [as handle_request] (C:UserspaullDocumentsprojectsotherswebweb serverssign_with_linkedinnode_modulesexpresslibrouterlayer.js:95:5)
      at next (C:UserspaullDocumentsprojectsotherswebweb serverssign_with_linkedinnode_modulesexpresslibrouterroute.js:144:13)
      at Route.dispatch (C:UserspaullDocumentsprojectsotherswebweb serverssign_with_linkedinnode_modulesexpresslibrouterroute.js:114:3) {
    code: 'ERR_INVALID_URL',
    input: 'POST https://www.linkedin.com/oauth/v2/accessToken?code=AQR_FCYP4tOggx6VpjiWj3gKU8fLKZzloCDcc0BJAifQ1ECmnpgKE8B2aQvvSPol-_VodYfIgAy-fwvSWd48V69c4tTme5mau6U4EPBptZDdjpbrdLmj8rs5cCNTV4eTfngmJgUUlsN2CD1THp34FJ0SK0DzpomMzft0Jgb3ySRCw1mUbJG3kOywbhAwaJQbGjHMA2yWvAuHSWYEm_8&grant_type=authorization_code&client_id=77if1emcoje12q&client_secret=j1N3thirTkhxOVnY&redirect_uri=http%3A%2F%2Flocalhost%3A3001%2Fapi%2Flinkedin%2Fredirect'
  }
}

It throws INVALID URL error but I can’t exactly locate where the exact problem is.

Below is my code:

AuthHelper.js
    import 'dotenv/config';
import querystring from 'node:querystring';

const AuthHelper = () => {
    // client id
    // response type
    // redirect url
    // scope
    // state -optional

    return encodeURI(`${process.env.LINKEDIN_AUTH_URI}?client_id=${process.env.LINKEDIN_CLIENT_ID}&response_type=code&scope=${process.env.LINKEDIN_SCOPE}&redirect_uri=${process.env.LINKEDIN_REDIRECT_URI}`)
}

const RedirectHelper = async(code) => {

    const payload = {
        code,
        grant_type: 'authorization_code',
        client_id:  process.env.LINKEDIN_CLIENT_ID,
        client_secret: process.env.LINKEDIN_CLIENT_SECRET,
        redirect_uri: process.env.LINKEDIN_REDIRECT_URI,
    };

    const data = await fetch(`${process.env.LINKEDIN_ACCESS_TOKEN_URI}?${querystring.stringify(payload)}`,{
        method: 'POST',
        headers: {
            'Content-Type': 'x-www-form-urlencoded'
        }
    })
    .then((response) => {
        console.log("response", response);
        return response.json();
    }).catch((err) =>{
        console.log("error", err);
        return err;
    });

    return data;
}

export {
    AuthHelper,
    RedirectHelper,
};


server.js

import express from 'express';
import { AuthHelper, RedirectHelper } from './AuthHelper.js';


// APP INSTANCE ----------------------------------------------------------------
const app = express();


// APP SETTINGS ----------------------------------------------------------------
app.use(express.json());



const PORT = 3001;
const HOST = 'http://localhost'




app.get('/api/linkedin/authorize', (req, res) => {
    return res.redirect(AuthHelper());
});

app.get('/api/linkedin/redirect', async(req, res) => {
    res.json(RedirectHelper(req.query.code));
});

app.listen(PORT, () => {
    console.log(`${HOST}:${PORT}`);
});

What am I not getting right? I want to add a functionality to enable users of my application to sign in with linkedin.
I’m able to get the linkedin screen for password and allowing the application to access my profile.
I have added the redirect url and also verified the application and counter checked my credentials and they are correct.

I appreciate your help in advance.