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

Fix error from undefined process.stderr #628

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

Fix error from undefined process.stderr #628

wants to merge 1 commit into from

Conversation

yehee
Copy link

@yehee yehee commented Nov 7, 2019

  • Fix Uncaught TypeError: Cannot read property 'write' of undefined

* Fix `Uncaught TypeError: Cannot read property 'write' of undefined`
@trentm
Copy link
Owner

trentm commented Jun 24, 2020

@alice-0-kim What environment are you running bunyan in which process.stderr isn't defined? Is this in a browser, perhaps?

(FYI on the delay in my responding:
#335 (comment))

joshkel added a commit to joshkel/node-bunyan that referenced this pull request Mar 22, 2021
Webpack 5 no longer automatically provides polyfills for Node APIs from
browser environments.  (See "Automatic polyfills for native Node.js
modules were removed" under the [Webpack 5 release announcement][1].)
This causes problems for Bunyan's use of Node.js-only globals like
`process` and `Buffer`.

As I understand it, one solution is to do an explicit `require` and, if
needed, [configure Webpack's resolve.fallback option][2] to specify how
to resolve the `require`d modules. (Bunyan does something similar to
this for the `os` and `fs` modules.)

The other solution is to check whether the modules are available before
continuing.

I opted for the second solution in this PR, to avoid introducing a
dependency on the [buffer][3] module.  This means that Bunyan won't
directly support Buffer objects in the browser.

There are two references to `process` that I didn't update:
`process.env` check and a reference to `process.stderr.write`.
`process.env` is specially handled by Node.js, and `process.stderr` is
addressed by trentm#628.

[1]: https://webpack.js.org/blog/2020-10-10-webpack-5-release/
[2]: https://stackoverflow.com/q/64557638/25507
[3]: https://www.npmjs.com/package/buffer
@joshkel
Copy link

joshkel commented Mar 22, 2021

Instead of checking whether process.stderr is defined, could you replace process.stderr.write(msg + '\n') with console.error(msg)? That should work in a browser as well as Node.js without special handling.

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.

None yet

3 participants