Skip to content

[WORKAROUND]: Cannot read property 'name' of undefined in projects using esbuild #190

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

Closed
doneel opened this issue Oct 19, 2022 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@doneel
Copy link

doneel commented Oct 19, 2022

Version

5.0.0

Description

Leaving this workaround posted for other desperate visitors from google

This isn't actually a bug with typescript-json-serializer and can be closed (equally googleable).

I was running into the error that it looks like other have run into where you can't serialize anything, and if you're going through google, you're seeing the advice to add these to your .tsconfig:

    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,

My problem was that esbuild doesn't support the emitDecoratorMetadata flag.
Remix.run uses esbuild and probably some other project stacks, and if this is your issue, you need to do some work to enable esbuild plugins to monkeypatch in support for emitDecoratorMetadata.

  1. Install esbuild manually (if it's not already there) as well as plugins:
npm install -D esbuild
npm install -D @anatine/esbuild-decorators
  1. Create an esbuild-overrides.js file in your project root directory:
/* eslint-disable */
const esbuild = require(`esbuild`);
const Module = require(`module`);
const { esbuildDecorators } = require('@anatine/esbuild-decorators');


const originalRequire = Module.prototype.require;
const originalBuild = esbuild.build;

const build = (options) =>
  originalBuild({
    ...options,
    // add in your overrides here, making sure to preserve original nested options., e.g.
    plugins: [...options.plugins, esbuildDecorators()],
  });

Module.prototype.require = function (id) {
  // when remix requires esbuild, it will get our modified build function from above
  if (id === `esbuild`) {
    return { ...esbuild, build };
  }
  return originalRequire.apply(this, arguments);
};
  1. Modify your build scripts to use the esbuild-overrides.js file you just created. I changed
  "scripts": {
    "build": "run-s build:*",
    "dev": "run-p dev:*",
}

to

  "scripts": {
    "build": "cross-env NODE_ENV=production NODE_OPTIONS='-r ./esbuild-overrides' run-s build:*",
    "dev": "cross-env NODE_ENV=development NODE_OPTIONS='-r ./esbuild-overrides' run-p dev:*",
}

I found this solution from remix-run/remix#2208 (comment)

Error

`Cannot read property 'name' of undefined`


### Reproduction

The default remix-run setup.

### On which OS the bug appears?

macOS 12.6

### What is your project type?

Remix

### On which build mode the bug appears?

Dev & prod

### Anything else?

_No response_
@doneel doneel added the bug Something isn't working label Oct 19, 2022
@GillianPerard
Copy link
Owner

Hi thank you for using my lib and posting there a solution to enable decorators for other build tool.

Does it work properly in both dev and prod modes?

@doneel
Copy link
Author

doneel commented Oct 20, 2022

Yes- confirming that it works in dev and prod.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants