Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-4839

new Mongo.ObjectId() throws TypeError in typescript project

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
    • 1
    • Not Needed
    • Not Needed

      What problem are you facing?

      The following typescript code throws an TypeError:

      import Mongo from 'mongodb';
      
      const id = new Mongo.Object();

      What driver and relevant dependency versions are you using?

      Driver version 4.12.0
      Typescript: 4.2.4

      Typescript config:

      {
        "compilerOptions":

      {     "module": "commonjs",     "target": "es2018",     "lib": ["es2018"],     "sourceMap": true,     "moduleResolution": "node",     "forceConsistentCasingInFileNames": true,     "noImplicitReturns": true,     "noImplicitThis": true,     "noImplicitAny": true,     "importHelpers": true,     "esModuleInterop": true,     "strictNullChecks": true,     "noUnusedLocals": true,     "noUnusedParameters": true,     "strict": true,     "allowSyntheticDefaultImports": true,     "resolveJsonModule": true,     "incremental": true,     "rootDir": "./src",     "outDir": "./dist",     "baseUrl": ".",     "noUnusedParameters": false   }

      ,
        "include": ["src/**/*.ts"],
        "exclude": ["node_modules"]
      }

      Full package.json:
      {
        "name": "mongotest",
        "version": "2.11.0",
        "description": "Mongo driver test code.",
        "main": "dist/index.js",
        "scripts":

      {     "build": "tsc",     "clean": "rm -rf dist/* *.tsbuildinfo"   }

      ,
        "files": [
          "dist",
          "package.json",
          "yarn.lock"
        ],
        "engines":

      {     "node": "^16"   }

      ,
        "devDependencies":

      {     "@types/node": "14",     "ts-node": "^10.5.0",     "tslib": "^1.10.0",     "typescript": "4.2.4"   }

      ,
        "dependencies":

      {     "mongodb": "4.12.0"   }

      }

      Steps to reproduce?

      Compile the above code in typescript and then attempt to run it. Will throw the following exception:
      TypeError: Cannot read properties of undefined (reading 'ObjectId')

       

            Assignee:
            bailey.pearson@mongodb.com Bailey Pearson
            Reporter:
            michaelp@bsquare.com Mike Pumford
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: