Next.js Discord

Discord Forum

Querying database from 'Dev' to 'Prod' inconsistancies

Answered
Jboncz posted this in #help-forum
Open in Discord
https://github.com/boncz92/nextjsLdapissue

When I run 'npm run dev' and hit the end point I get results, when I build and use 'npm run start' I get no results, even though it succesfully binds to the LDAP database. I am at a loss for what is causing this problem and it is preventing me from moving this endpoint to the app router. The github repository contains a fully functioning reproduction utilizing a public readonly ldap database.

The relevant code is below but to repro the issue the repository can be used.

import ldapjs from 'ldapjs';

async function bindAndSearchLdap(searchBase, filter, attributes) {
    return new Promise(async (resolve, reject) => {
        try {
            const client = ldapjs.createClient({ url: 'ldap://ldap.forumsys.com:389' });
            await new Promise((resolveClientBind, rejectClientBind) => {
                client.bind('cn=read-only-admin,dc=example,dc=com', 'password', (err, res) => {
                    if (err) {
                        rejectClientBind(err);
                        return;
                    }
                    console.log('LDAP bind successful');
                    resolveClientBind(client);
                });
            });

            const entries = await new Promise((resolveSearch, rejectSearch) => {
                client.search(searchBase, { scope: 'sub', filter, attributes }, (err, res) => {
                    if (err) {
                        rejectSearch(err);
                        return;
                    }

                    const entries = [];
                    res.on('searchEntry', (entry) => {
                        entries.push(entry.pojo);
                    });
                    res.on('end', () => {
                        resolveSearch(entries);
                    });
                    res.on('error', (err) => {
                        rejectSearch(err);
                    });
                });
            });

            resolve(entries);
        }
        catch (err) {
            reject(err);
        }
        finally {

        }
    });
}

export async function POST(request) {
    let results = await bindAndSearchLdap(' dc=example,dc=com', '(uid=boyle)', ['uid'])


    return Response.json({ message: results }, { status: 200 })
}
Answered by Jboncz
Siwtched to ldapts.
View full answer

16 Replies

npm run build -> npm run start results:
npm run dev results:
Works in
Next.js 14.0.0
Next.js 14.0.1
Next.js 14.0.2-canary.18

Does not work in
Next.js 14.0.2-canary.19
Next.js 14.1.0
Next.js 14.2.1
Next.js 14.3.0-canary.8
Per my testing, I beleive that this issue is a nextjs bug of some sort

Opened: https://github.com/vercel/next.js/issues/64688
Temporary resolution to the issue is to either downgrade to 14.0.1 or Use the pages/api/ router for ldap connections.
Still looking for a solution to this issue. Not 100% confident its a framework issue. soooo bump.
@Jboncz Still looking for a solution to this issue. Not 100% confident its a framework issue. soooo bump.
Black Caiman
HI, it seems I have the same problem. talking with ldap in dev mode works, but in production mode, no user are found. did you find any solution yet?
(next.js 15.3.1, app router)
What library are you using?
ldapjs?
Id recommend switching to ldapts as ldapjs is not longer being maintained. @Black Caiman
Thats what I did to fix the issue.
Siwtched to ldapts.
Answer
Black Caiman
thx, but my whole project is in js
Ldapts is built around Ldapjs, so it should almost be drop in and it works. ldapjs broke awhile back after a specific nodejs version cant remember
Black Caiman
ok, thx alot
Black Caiman
const nextConfig = {
serverExternalPackages: ["ldapjs"],
}
did the trick