JS-DevTools / ono

Wrap errors without losing the original message, stack trace, or properties
https://jstools.dev/ono/
MIT License
106 stars 11 forks source link

'isomorphic.node.ts' runtime error with webpack #9

Closed pcafstockf closed 4 years ago

pcafstockf commented 4 years ago

I could swear this was working earlier this year, but after upgrading to latest node / types / webpack, simply importing or requiring 'isomorphic.node' triggers a runtime error

Cannot read property 'inspect' of undefined

It looks like import util from "util"; at the top of 'isomorphic.node.ts' causes webpack to generate: const inspectMethod = util_1.default.inspect.custom || Symbol.for("nodejs.util.inspect.custom");

As opposed to the expected: const inspectMethod = util_1.inspect.custom || Symbol.for("nodejs.util.inspect.custom");

This could be resolved by changing the import to read import * as util from "util";

Note: The same thing occurs with the export const formatter = util.format; statement a few lines down.

JamesMessinger commented 4 years ago

Hi. Sorry for the delay, I've been away on vacation.

This is odd. Ono is tested on all major browsers using Webpack, and I haven't noticed any changes in behavior recently.

Are you using TypeScript in your project? If so, then perhaps there was a change to the --esModuleInterop option in your codebase recently?

pcafstockf commented 4 years ago

My project doesn't directly use ono (it uses swagger-parser which depends on ono), so the typescript compiler doesn't really come into play here. Just a quick note, this is isomorphic.node.js. Given my limited understanding of ono, I don't think you would notice any "browser" issues.

I'm thinking maybe the fact that it's an esm import of "util", which is a nodejs builtin module (and therefore cjs), might be the root issue?

JamesMessinger commented 4 years ago

Just a quick note, this is isomorphic.node.js. Given my limited understanding of ono, I don't think you would notice any "browser" issues.

Are you building a web app or a Node (CLI) app? You're using Webpack, so I assumed you were building a web app, but Webpack can also be used for Node apps, so I may have assumed incorrectly.

I assume that your Webpack config is also using a bundler, such as Babel or Rollup to bundle your source code and dependencies. If so, then you need to configure the bundler to use the correct modules for your target runtime environment. That will determine whether it loads the CJS or ESM modules and whether it loads isomorphic.node.js or isomorphic.browser.js.

pcafstockf commented 4 years ago

Got it! Your comments, along with reading the swagger-parser code (which uses ono), and this SO post What is the “module” package.json field for?, all helped me understand that my webpack config was wrong.
I had Webpack pulling in the ESM code from ono instead of the CJS code. All good now. Thanks for your patience.