Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot find module lib/worker.js #46

Open
theogravity opened this issue Aug 19, 2022 · 7 comments
Open

Cannot find module lib/worker.js #46

theogravity opened this issue Aug 19, 2022 · 7 comments

Comments

@theogravity
Copy link

theogravity commented Aug 19, 2022

I have pino (v8.4.1) with the latest esbuild and the associated transports as part of a package with TypeScript files (which are not compiled / built in the package itself) that is included in a project (ex: import { logger } from 'external-private-package/src/logger.ts).

We use yarn and require this package using "external-private-package": "link:<path>" syntax to use it in the dependencies of package.json.

My esbuild config looks like this:

  entryPoints: ['src/index.ts'],
  platform: 'node',
  bundle: true,
  outdir: 'dist',
  sourcemap: true,
  treeShaking: true,
  minify: false,
  keepNames: true,
  target: 'node14.16',
  plugins: [
    nodeExternalsPlugin({
      allowList: ['external-private-package'],
    }),
    esbuildPluginPino({
      transports: ['pino-datadog-transport', 'pino-pretty'],
    }),
  ],

When I build, I get the following structure:

CleanShot 2022-08-19 at 09 42 11

But when I attempt to run the app using node index.js, I get the following:

node:internal/event_target:912
  process.nextTick(() => { throw err; });
                           ^
Error: Cannot find module 'dist/lib/worker.js'
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at MessagePort.<anonymous> (node:internal/main/worker_thread:191:24)
    at MessagePort.[nodejs.internal.kHybridDispatch] (node:internal/event_target:643:20)
    at MessagePort.exports.emitMessage (node:internal/per_context/messageport:23:28)
Emitted 'error' event on Worker instance at:
    at Worker.[kOnErrorMessage] (node:internal/worker:289:10)
    at Worker.[kOnMessage] (node:internal/worker:300:37)
@theogravity
Copy link
Author

theogravity commented Aug 19, 2022

Renaming the files manually work, but isn't ideal:

  • thread-stream-worker.js -> lib/worker.js
  • pino-pretty.js -> pino-pretty-transport.js
const path = require('path');
const fs = require('fs');

const distPath = path.join(__dirname, 'dist');

require('esbuild')
  .build(buildConfig)
  .then((result) => {

    fs.mkdirSync(distPath + '/lib');
    fs.renameSync(distPath + '/thread-stream-worker.js', distPath + '/lib/worker.js');
    fs.renameSync(distPath + '/thread-stream-worker.js.map', distPath + '/lib/worker.js.map');
    fs.renameSync(distPath + '/pino-pretty.js', distPath + '/pino-pretty-transport.js');
    fs.renameSync(distPath + '/pino-pretty.js.map', distPath + '/pino-pretty-transport.js.map');

    console.log('Build completed');
    process.exit(0);
  })

@ranihorev
Copy link

Hey, should I open a PR and fix this? or is it supposed to work now?

@wd-David
Copy link
Owner

PR welcome!

@ranihorev
Copy link

ranihorev commented Mar 25, 2023 via email

@wdanilo
Copy link

wdanilo commented Feb 9, 2024

Renaming like that doesn't work if you use esbuild watch mode. This plugin just doesn't work without this renaming. Is anyone using it?

@segevfiner
Copy link
Contributor

Might be hitting: evanw/esbuild#2607?

@segevfiner
Copy link
Contributor

segevfiner commented May 29, 2024

I also had it break when building multiple formats due to the pinoBundlerRan code using tsup with a config like this:

import { defineConfig } from "tsup";
import pino from "esbuild-plugin-pino";

export default defineConfig({
  entry: ["src/index.ts"],
  target: "node20",
  format: ["cjs", "esm"],
  sourcemap: true,
  esbuildPlugins: [pino({ transports: ["pino-pretty"] })],
});

I'm not sure what

if (pinoBundlerRan) return
pinoBundlerRan = true
is for, but it seems to break the build in this case...

EDIT: Not caused by multiple formats, but by mutliple versions of pino resolved by esbuild, though tree-shaken so only one ends up in the output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants