Uploaded image for project: 'MongoDB Shell'
  1. MongoDB Shell
  2. MONGOSH-1737

"Require is not defined" when running mongosh with --eval

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • 2.2.1
    • Affects Version/s: 2.2.0
    • Component/s: None
    • None
    • Environment:
      OS: Rocky Linux release 8.6 (Green Obsidian)
      node.js / npm versions: v18.14.2
      MongoDB: mongodb-org 7.0.6
      MongoDB Mongosh: 2.2.0
    • Developer Tools
    • Not Needed

      Problem Statement/Rationale

      When running the Javascript "require" method with the --eval flag, it fails with a reference error.

      $ mongosh --eval 'require'
      ReferenceError: require is not defined 

       

      But when using the interactive mode, everything works as expected.

       

      $ mongosh
      Current Mongosh Log ID:    65f6e158a958598112db83af
      Connecting to:        mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.0
      Using MongoDB:        7.0.6
      Using Mongosh:        2.2.0
      For mongosh info see: https://docs.mongodb.com/mongodb-shell/
      
      test> require
      [Function: require] {
        resolve: [Function: resolve] { paths: [Function: paths] },
        main: undefined,
        extensions: [Object: null prototype] {
          '.js': [Function (anonymous)],
          '.json': [Function (anonymous)],
          '.node': [Function (anonymous)]
        },
        cache: [Object: null prototype] {}
      } 

       

      This behavior changed 3 days ago, when the version 2.2.0 was uploaded to the MongoDB repository (https://github.com/mongodb-js/mongosh/releases)

       

       

      Current workaround I am using is to force install the 2.1.5 version.

      Also, this issue reproduced on Ubuntu 20.04 & 22.04

      65f6e2289e88d5cfdadb83af_log

      Steps to Reproduce

      Run the following:

       

      
      

      mongosh --eval 'require'

       

      Expected Results

       

      $ mongosh --eval 'require'
      Current Mongosh Log ID:    65f6e158a958598112db83af 
      Connecting to:        mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.0 Using MongoDB:        7.0.6 
      Using Mongosh:        2.2.0
      For mongosh info see: https://docs.mongodb.com/mongodb-shell/
      
      [Function: require] {
        resolve: [Function: resolve] { paths: [Function: paths] },
        main: undefined,
        extensions: [Object: null prototype] {
          '.js': [Function (anonymous)],
          '.json': [Function (anonymous)],
          '.node': [Function (anonymous)]
        },
        cache: [Object: null prototype] {}
      } 

       

      Actual Results

      $ mongosh --eval 'require' 
      ReferenceError: require is not defined  

            Assignee:
            Unassigned Unassigned
            Reporter:
            eliottkaspi@gmail.com Eliott Kespi
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: