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

compatibility patch #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

rokku-x
Copy link

@rokku-x rokku-x commented Dec 7, 2021

This will fix some compatibility issues with some node.js use-cases. This fix will handover the module loading request in node.js to 'require' without breaking the original code in some apps/scripts is that already using this package.

resolves #14

this will fix some compatibility issues with some node.js use-cases. This fix will handover the module loading request in node.js to 'require' without breaking the original code in some apps/scripts is that already using this package.
Copy link
Member

@jsumners jsumners left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Please follow the linting standard of the project.
  2. Please do not use a ternary for this as it is not very easy to read.
  3. Please include unit tests to cover new and changed code.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a unit test?

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a unit test?

@@ -1,7 +1,13 @@
/* eslint-disable no-new-func, camelcase */
/* globals __non_webpack__require__ */

const realImport = new Function('modulePath', 'return import(modulePath)')
const realImport = typeof process === "undefined" ? new Function('modulePath', 'return import(modulePath)') : async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a commonjs module and this can't be null.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then the check for the process variable should be made in the runtime right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the question. In Node.js, the process global is always available.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But isn't this module also imported in the browser? I am sorry if I get this module wrong.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This module is not supposed to be used inside browsers.

@zc16607
Copy link

zc16607 commented Apr 24, 2023

I also encountered this problem.
Platform: Windows

pino: 8.1.0
pino-pretty: 8.1.0

This is my source code file logger.js:

const pino = require('pino')
require('pino-pretty')
const dayjs = require('dayjs')

const log = pino({
  transport: {
    target: 'pino-pretty',
    options: {
      colorize: true
    }
  },
  base: {
    pid: false,
  },
  timestamp: () => `,"time":"${dayjs().format('YYYY/MM/DD HH:mm:ss')}"`,
})

module.exports = log

Then I use the pkg@5.8.1 package into an exe program, run the executable program get an error as follows:

node:internal/event_target:915
  process.nextTick(() => { throw err; });
                           ^

TypeError: Invalid host defined options
    at eval (eval at <anonymous> (C:\snapshot\backEnd\node_modules\real-require\src\index.js:4:20), <anonymous>:3:1)
    at start (C:\snapshot\backEnd\node_modules\thread-stream\lib\worker.js:33:19)
    at Object.<anonymous> (C:\snapshot\backEnd\node_modules\thread-stream\lib\worker.js:87:1)
    at Module._compile (node:internal/modules/cjs/loader:1112:14)
    at Module._compile (pkg/prelude/bootstrap.js:1933:32)
    at Module._extensions..js (node:internal/modules/cjs/loader:1166:10)
    at Module.load (node:internal/modules/cjs/loader:988:32)
    at Module._load (node:internal/modules/cjs/loader:834:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at MessagePort.<anonymous> (node:internal/main/worker_thread:194:24)
Emitted 'error' event on ThreadStream instance at:
    at destroy (C:\snapshot\backEnd\node_modules\thread-stream\index.js:349:12)
    at Worker.onWorkerMessage (C:\snapshot\backEnd\node_modules\thread-stream\index.js:164:7)
    at Worker.emit (node:events:537:28)
    at MessagePort.<anonymous> (node:internal/worker:233:53)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:645:20)
    at exports.emitMessage (node:internal/per_context/messageport:23:28)
    at [kOnExit] (node:internal/worker:267:5)
    at Worker.<computed>.onexit (node:internal/worker:199:20)

I tried several times and found the problem at this location real-require/src/index.js.

const realImport = new Function('modulePath', 'return import(modulePath)')

After using require instead of import, the repackaging program ran normally. But I don't know why import can't be used.

The value of modulePath needs to be processed before using require

const realImport = (modulePath) => {
  // the value of modulePath like this :
  // modulePath = `file:///C:/snapshot/backEnd/node_modules/pino-pretty/index.js`
  

  // This is the correct code
  // return require('pino-pretty/index.js')
  // or like this
  // return require('C:/snapshot/backEnd/node_modules/pino-pretty/index.js')
}

PS: This issue is not related to the packaged node version. I tested v14, v16, and v18, all of which reproduced the problem

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

Successfully merging this pull request may close these issues.

incompatibility with pkg
4 participants