How to Solve QueryCursor.js Mongoose error with NVM

How to Solve QueryCursor.js Mongoose error with NVM

ยท

4 min read

TL;DR

This is my attempt at resolving this issue raised on GitHub. I discovered that it was a NodeJS incompatibility issue. Downgrading your NodeJS version to 12.x resolves this error.

If you are working with Mongoose version 6.3.2 and you had your NodeJS version updated to >14.x or =17.5.0 (this is the latest version as of when I am writing this post), you might have encountered this error:

TypeError: Cannot assign to read only property 'map' of object '#<QueryCursor>'
    at Object.<anonymous> (/Users/balonsom/Documents/repos/dungeon-master/node_modules/mongoose/lib/cursor/QueryCursor.js:144:27)
    at Module._compile (node:internal/modules/cjs/loader:1097:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1151:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/Users/balonsom/Documents/repos/dungeon-master/node_modules/mongoose/lib/query.js:12:21)
    at Module._compile (node:internal/modules/cjs/loader:1097:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1151:10)

The above error is caused by an incompatibility issue between Mongoose and NodeJS version 17.5. On the Mongoose GitHub repo where the issue #11377 was raised, several suggestions were offered which I tried all but none resolved the error for me. Also, issue #11377 was marked resolved by issue #11381 which was not the case for me.

How to Solve QueryCursor.js Mongoose error

Knowing that this is a compatibility issue between NodeJS and Mongoose, the best solution will be to downgrade my Node.js version to something earlier than version 14 ๐Ÿค”

But wait, I have other projects on my local machine that I am running which are incompatible with Node.js versions <16.x ๐Ÿ˜Ÿ

This is where NVM - (Node Version Manager) comes into play. NVM allows us to install and run multiple versions of NodeJS on our local machine. For an in-depth guide on how to install and use NVM, please refer to this tutorial by Digital Ocean.

Installing and Using NVM

In this guide, I shall walk you through the essentials of installing NVM and using Node.js v12 for your Mongoose project.

How to Install NVM

To install NVM, run the following commands

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash

or using wget

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash

Once installed, close your terminal application for changes to take effect. You will also need to add a couple of lines to your bash shell startup file. This file might have the name .bashrc, .bash_profile, or .zshrc depending on your operating system. To do this, reopen your terminal app and run the following commands:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"

Installing NodeJS v12.x

To install NodeJS 12, paste the following command into your terminal

nvm install 12

After running this command, this is the output that will display in your terminal app:

Output
Downloading and installing node v12.22.12...
Downloading https://nodejs.org/dist/v12.22.12/node-v12.22.12-darwin-x64.tar.xz...
######################################################################### 100.0%
Computing checksum with shasum -a 256
Checksums matched!
Now using node v12.22.12 (npm v6.14.16)

You have successfully installed NodeJS v12 on your local machine. This means that you are now running multiple versions of NodeJS.

List installed Node.js versions on your machine

With a handful of different versions of Node.js installed, we can run nvm with the ls argument to list out everything we have installed:

nvm ls

The output produced by running this command might look something like this:

v12.22.12
       v14.19.2
        v17.5.0
         system
default -> node (-> v17.5.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v17.5.0) (default)
stable -> 17.5 (-> v17.5.0) (default)
lts/* -> lts/gallium (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.12
lts/fermium -> v14.19.2
lts/gallium -> v16.15.0 (-> N/A)

Your output will definitely differ from mine depending on how many versions of Node.js you have installed on your machine.

The next step entails us specifying which version of NodeJS we want to use in our program or project. To do this, follow the below steps:

Using NodeJS v12 in our project

cd into the directory of your project by running this command

cd <path/to/your/project/directory>

Then run this command below to specify the NodeJS version want to use, in our case, v12

nvm use 12

You will get the following output

Now using node v12.22.12 (npm v6.14.16)

Congratulations ๐ŸŽ†๐ŸŽ‡๐ŸŽŠ๐ŸŽˆ

You are now using NodeJS version for this specific project, while also preserving the default active version of Node.js on your machine.

Further reading

  1. How To Run Multiple Versions of Node.js with Node Version Manager
  2. How do I install multiple node js version on the same machine

Follow Me

  1. On Twitter @eunit99
  2. On GitHub @eunit99

Did you find this article valuable?

Support Eunit Blog by becoming a sponsor. Any amount is appreciated!

ย