Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange SyntaxError in MongoDB after upgrading Mongoose to v7 #13204

Closed
2 tasks done
JamesJansson opened this issue Mar 23, 2023 · 8 comments
Closed
2 tasks done

Strange SyntaxError in MongoDB after upgrading Mongoose to v7 #13204

JamesJansson opened this issue Mar 23, 2023 · 8 comments
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@JamesJansson
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

7.0.2

Node.js version

18.13.0

MongoDB server version

5.1.0

Typescript version (if applicable)

4.8.4

Description

Issue after upgrading to Mongoose V7. In the MongoDB library

SyntaxError: Invalid or unexpected token

The toke is ?. which makes no sense since ?. operator appears to be normal and has worked in much younger versions of node.

Multiple reports appear on stack overflow, with the only recommendation being "downgrade to v6":
https://stackoverflow.com/questions/75681631/why-node-js-gives-this-error-from-the-npm-modules
https://stackoverflow.com/questions/75675925/syntax-and-database-connection-error-in-node-js

Steps to Reproduce

Upgraded from v5 to v7 (any version 7). I have no MongoDB installed through package.json (only installed courtesy of Mongoose) After building in TS, running the server using nodemon, I get the following error:

Stack trace:

node_modules/mongodb/lib/collection.js:70
            pkFactory: db.options?.pkFactory ?? utils_1.DEFAULT_PK_FACTORY,
                                  ^
SyntaxError: Invalid or unexpected token
    at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)

I'm working in Ubuntu 22.04.2

Downgrading to v6 doesn't have this problem.

Expected Behavior

No response

@vkarpov15
Copy link
Collaborator

Can you please double check your Node.js version using node -v? This should only be an issue if you're running on Node.js 12 or older.

If you are indeed on Node.js 18, can you please provide your tsconfig.json? Could be a build step issue.

@vkarpov15 vkarpov15 added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Mar 23, 2023
@ronenteva
Copy link

Same issue here, node.js v18

@vkarpov15
Copy link
Collaborator

@ronenteva your comment is not very helpful. Please provide more detailed instructions on how to repro this issue.

@vkarpov15 vkarpov15 added needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary labels Apr 4, 2023
@ronenteva
Copy link

@vkarpov15 I'm sorry, I still couldn't find exactly how to reproduce the issue, but I'm pretty sure it has something to do with esm installed.

@vkarpov15
Copy link
Collaborator

We'll try out with esm. But just a thought - why do you need an esm bundler if Node.js supports esm natively?

@vkarpov15 vkarpov15 added this to the 7.0.5 milestone Apr 6, 2023
@vkarpov15 vkarpov15 added needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue and removed needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity labels Apr 6, 2023
@ronenteva
Copy link

I use it for older projects, so I can have both require and import

@zhe1ka
Copy link

zhe1ka commented Apr 9, 2023

i can confirm that the issue is appeared when using esm like this
require = require('esm')(module, { cjs: true }); module.exports = require('./app.js');
fixed by:

  1. removed esm
  2. added "type": "module" to package.json
  3. changed all require with import; i suppose module.exports => export (default) requires too.
  4. all url of imported modules should be with an extension (ie .js)

@vkarpov15
Copy link
Collaborator

Confirmed this is a known issue in esm: standard-things/esm#918 (comment) . Also standard-things/esm#909. TLDR; esm hasn't been maintained in some time, doesn't work with more recent additions to the JavaScript language spec.

You would need to use a fork of esm, like esm-wallaby.

@vkarpov15 vkarpov15 removed this from the 7.0.5 milestone Apr 17, 2023
@vkarpov15 vkarpov15 added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Apr 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

4 participants