pinojs/pino

After building 'unable to determine transport target for "@axiomhq/pino"' due to "cjs" folder being deleted after build

Opened this issue · 1 comments

Hello !
thanks for the great package!

I have been trying to connect to axiom and send logs. while in development everything works fine.
but in deployment I keep receiving the follwoing error

unable to determine transport target for "@axiomhq/pino"
  at fixTarget (J:\...redecated...\.output\server\node_modules\pino\lib\transport.js:146:13)
  at Function.transport (J:\...redecated...\.output\server\node_modules\pino\lib\transport.js:116:22)

am using Nuxt3 with vite.
I did try to turn off treeshaking in nuxt.config.ts

  vite:{
    build:{
      rollupOptions:{
      // Exclude the package from tree shaking
      // external: ['@axiomhq']
      treeshake: false

      }
    }
  },

but still the same issue.

Here is my logger code:

import '@axiomhq/pino'
import pino from 'pino';

const logger = pino(
  { level: 'info',
    redact: {
      paths:["email","phone","mobile_number","customerMobile","TS_customer_mobile","TS_customer_email","TS_staff_member_mobile","TS_staff_member_email","user_mobile","user_email"],
      // remove: true
    }
  },
  pino.transport({
      target: '@axiomhq/pino',
      options: {
        dataset: useRuntimeConfig().axiomDataset,
        token: useRuntimeConfig().axiomToken,
        // dataset: process.env.AXIOM_DATASET,
        // token: process.env.AXIOM_TOKEN,
      },
  }
  ),
);
export {logger}

Note: it works just fine with pino-pretty. but am not sure what is happening in the building step.

i spent my whole day playing around .. and I have no clue... what is happening on the build step

Debugging the production version using this modification :

  function fixTarget (origin) {
    origin = bundlerOverrides[origin] || origin

    if (isAbsolute(origin) || origin.indexOf('file://') === 0) {
      return origin
    }

    if (origin === 'pino/file') {
      return join(__dirname, '..', 'file.js')
    }

    let fixTarget

    for (const filePath of callers) {
      try {
        const context = filePath === 'node:repl'
          ? process.cwd() + sep
          : filePath
        throw new Error(createRequire(context).resolve(origin))
        console.log(createRequire(context).resolve(origin))
        fixTarget = createRequire(context).resolve(origin)
        break
      } catch (err) {
        // Silent catch
        throw new Error(err)
        continue
      }
    }

    if (!fixTarget) {
      throw new Error(`unable to determine transport target for "${origin}"`)
    }

    return fixTarget
  }
}

shows that the @axiomhq\pino\dist\cjs\index.cjs is what it tries to use.

though in vite .cjs is depreciated and it should use .mjs
also in the build output the cjs folder is deleted and doesn't exists, which causes this error.

Thanks for reporting!

Can you provide steps to reproduce? We often need a reproducible example, e.g. some code that allows someone else to recreate your problem by just copying and pasting it. If it involves more than a couple of different file, create a new repository on GitHub and add a link to that.

These bundling issues are quite tricky, so if you would like to also send a PR... that would be amazing.