-
Notifications
You must be signed in to change notification settings - Fork 1
Load CJS instead of ESM #157
Comments
const out = parser.default('ObjectId()')
console.log(out instanceof ObjectId) // false, should be true
console.log(typeof out) // object
console.log(out.constructor.name) // ObjectId
const outBson = new ObjectId()
console.log(outBson instanceof ObjectId) // true
console.log(typeof outBson) // object
console.log(outBson.constructor.name) // ObjectId
console.log(typeof out === typeof outBson) // true
console.log(out.constructor.name === outBson.constructor.name) // true |
Implementation of instanceof: function instanceOf(object, constructor) {
while (object !== null) {
if (object === constructor.prototype) { //object is instanceof constructor
return true
} else if (typeof object === 'xml') { //workaround for XML objects
return constructor.prototype == XML.prototype
}
object = object.__proto__ //traverse the prototype chain
}
return false //object is not instanceof constructor
} Related problem: console.log(out.__proto__ === ObjectId.prototype) // false, should be true
console.log(outBson.__proto__ === ObjectId.prototype) // true Imply: console.log(out.__proto__ === outBson.__proto__) // false, should be true |
This just seems to be happening because ejson-shell-parser is loading the CommonJS version of It’s expected that these are different. You can fix this by manually loading the CommonJS version from your code as well (if you ensure that the package gets deduplicated during Generally speaking, this package doesn’t make guarantees about the specific prototypes that its returned values have; it only returns bson objects from a |
Can I do a PR to covert this to ESM? Edit: |
|
@addaleax I imported the ESM version (removing default from parse function) and it works. import { ObjectId } from 'bson'
import parser from './ejson-shell-parser.esm.js' // same as file in dist folder
const out = parser('ObjectId()')
console.log(out instanceof ObjectId) // output is true I can confirm that ejson-shell-parser is imported as CJS instead of ESM. Edit:This can be fixed using Diff in - "main": "dist/ejson-shell-parser.cjs.js",
+ "main": "dist/ejson-shell-parser.cjs",
- "module": "dist/ejson-shell-parser.esm.js",
+ "module": "dist/ejson-shell-parser.mjs",
...
+ "exports": {
+ "import": "./dist/ejson-shell-parser.mjs",
+ "require": "./dist/ejson-shell-parser.cjs"
+ } import { ObjectId } from 'bson'
import parser from 'ejson-shell-parser.esm.js'
const out = parser('ObjectId()')
console.log(out instanceof ObjectId) // output is true I opened preconstruct/preconstruct#576 |
Description
Instance of parsed ObjectId, using ejson-shell-parser (version >= 1.2.1), is different from bson.ObjectId instance (bson version >= 5.0.0-alpha.0).
Correctly work:
bson
v4 and v5bson
v4Same problem with Timestamp, BSONSymbol, DBRef, MinKey, MaxKey.
Steps to reproduce:
yarn init -y
yarn set version canary
yarn add bson ejson-shell-parser
"type": "module"
in pakcage.jsonindex.js
fileyarn node index.js
Versions:
bson >= 5.0.0-alpha.0
ejson-shell-parser >= 1.2.1
Node: 16.17.0
Actual
log is false
Expect
log is true
The text was updated successfully, but these errors were encountered: