Next.js Discord

Discord Forum

How to get console.log to appear on server side code

Unanswered
Siamese posted this in #help-forum
Open in Discord
SiameseOP
I have functions executing on the server side, like async data functions, but any console.log I use never appears in the terminal. What's worse, I can't add any server side breakpoints either. Is there something special I have to do to make this work?

35 Replies

Common House-Martin
you forgot to mark them as "use server" maybe?
SiameseOP
oh, i thought you only had to mark files as 'use client'; otherwise they were assumed server?
Perro Majorero
use server is for server actions, not for marking a component as a server component
@Siamese oh, i thought you only had to mark files as `'use client';` otherwise they were assumed server?
Perro Majorero
anything under app/ is assumed to be a server component unless you've marked it as "use client"; or it's a class component, yes
Just to clarify, it's a component that should be a server component but the logs only appear on the client?
@Common House-Martin you forgot to mark them as "use server" maybe?
thats not what "use server" is for
SiameseOP
in the same file as the component, but in an async function
hmm, would you mind sharing the code?
SiameseOP
Sure, but the issue only reproduces if there's an error. Which is frustrating, as I was using the console.log to debug the error
inside my page.jsx:

async function getBlogPosts() {
  return Promise.all(
    (await fg("src/app/blog/**/page.mdx", { stats: true })).map(
      async (file) => {
        console.log('getBlogPosts file:', file)
        const { path, mtimeMs, birthtimeMs } = file;
        const slug = path.match(/app\/blog\/(.*)\/page\.mdx/)[1];
        const meta = (await import(`./blog/${slug}/page.mdx`)).frontMatter;

        return {
          url: `/blog/${slug}`,
          createdMs: birthtimeMs,
          editedMs: mtimeMs,
          ...meta,
        };
      },
    ),
  );
}
for example, if I import from the wrong path, say ./blogs/, I'll get an error, and none of the console.logs will output
isn't that what is supposed to happen?
I don't use fg
but if the path is wrong, won't the file be not loaded, hence no console?
oh
nvm
you meant the meta path
you can have try & catch ig 🤷‍♂️
SiameseOP
the error is on the await import
which is within the map
@joulev if you have `console.log` outside the `Promise.all`, it should be logged
are you sure about that? try adding console.log above the Promise.all and see if it works
also put try/catch wrapping that promise.all call and print the error. you will see something
nextjs doesnt do any shenanigans wrt console.logs. if you have a console.log call and it is reached by javascript, it will be logged
then i need more than just this code. could you make a minimal reproduction repository that i can test?
can you share the error you're getting just to be for sure?
SiameseOP
async function getBlogPosts() {
  console.log('getBlogPosts')
  try {
    return Promise.all(
      (await fg("src/app/blog/**/page.mdx", { stats: true })).map(
        async (file) => {

          const { path, mtimeMs, birthtimeMs } = file;
          const slug = path.match(/app\/blog\/(.*)\/page\.mdx/)[1];
          const meta = (await import(`./blogs/${slug}/page.mdx`)).frontMatter;

          return {
            url: `/blog/${slug}`,
            createdMs: birthtimeMs,
            editedMs: mtimeMs,
            ...meta,
          };
        },
      ),
    );
  }
  catch (e) {
    console.error('failed:', e);
  }
}
getBlogPosts never gets output
is getBlogPosts() even called? please make a reproduction repository that i can clone and test
SiameseOP
you're sure getBlogPosts isn't logged?
SiameseOP
i'm not seeing it in the terminal, I shared a screenshot of the output