Skip to content

fast-json-patch@2.2.0 is incorrectly presenting as a transpiled ES Module #233

@shockey

Description

@shockey

fast-json-patch@2.2.0 is breaking builds in Swagger Client (see swagger-api/swagger-js#1460).

Current behavior

The problem is that fast-json-patch is combining the __esModule hint with an exports object that follows the CJS exports pattern.

2.2.0 is setting an non-enumerable __esModule hint property that wasn't present before:

➜ node -e 'const fjp = require("fast-json-patch"); const util = require("util"); console.log(util.inspect(fjp, { showHidden: true, depth: 0 }));'
{ [__esModule]: true,
  applyOperation: [Object],
  applyPatch: [Object],
  applyReducer: [Object],
  getValueByPointer: [Object],
  validate: [Object],
  validator: [Object],
  JsonPatchError: [Object],
  deepClone: [Object],
  escapePathComponent: [Object],
  unescapePathComponent: [Object],
  unobserve: [Object],
  observe: [Object],
  generate: [Object],
  compare: [Object] }

This property is used in transpilers and module loaders to handle interoperability between CJS modules and ESM modules represented as JavaScript objects -- specifically, CJS modules need to be wrapped inside another object's default property. More info here.

All of this means that when I use import syntax to use fast-json-patch in my Babel-enabled project:

import fastJsonPatch from "fast-json-patch"

fastJsonPatch.applyOperation()

...Babel translates my import to:

var _fastJsonPatch = _interopRequireDefault(require("fast-json-patch"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

_fastJsonPatch["default"].applyPatch();

...which means at runtime, my code will see __esModule: true in fast-json-patch and therefore not wrap the exported result from it in a default key:

➜ node -e 'var _fastJsonPatch = _interopRequireDefault(require("fast-json-patch")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }; _fastJsonPatch["default"].applyPatch();'
[eval]:5
  _fastJsonPatch["default"].applyPatch();
                          ^

TypeError: Cannot read property 'applyPatch' of undefined
    at [eval]:5:27
    at ContextifyScript.Script.runInThisContext (vm.js:50:33)
    at Object.runInThisContext (vm.js:139:38)
    at Object.<anonymous> ([eval]-wrapper:6:22)
    at Module._compile (module.js:652:30)
    at evalScript (bootstrap_node.js:466:27)
    at startup (bootstrap_node.js:167:9)
    at bootstrap_node.js:612:3

Expected behavior

fast-json-patch should either be not setting __esModule (which was the previous behavior), or should be exporting an object that matches ESM syntax, e.g. { __esModule: true, default: { applyOperation: [Object], ... } }.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions