Skip to content

8.11.0 - Return outside of function #2980

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

Closed
bryantqo opened this issue May 17, 2023 · 13 comments
Closed

8.11.0 - Return outside of function #2980

bryantqo opened this issue May 17, 2023 · 13 comments

Comments

@bryantqo
Copy link

Starting in version 8 a line was added to which a return happens outside of a function. I think this was added in 5532ca5 located in packages/pg/lib/crypto/utils.js. This recently popped up in our jest tests with an error of

SyntaxError: /node_modules/pg/lib/crypto/utils.js: 'return' outside of function. (7:2)

Reverting to 8.10.0 solves the problem for now

@Jolg42
Copy link

Jolg42 commented May 17, 2023

I also noticed this today in a renovate PR upgrade of pg package.

@charmander
Copy link
Collaborator

charmander commented May 23, 2023

This can be changed in pg for everyone’s convenience, but that sounds like a bug in whatever tool isn’t recognizing a legitimate (and not infrequently used) feature of CommonJS modules.

@darrinholst
Copy link

Just ran into this on an esm project that we were still using jest for. You have to jump through some hoops to get jest working with esm. Switching to vitest fixed it for us.

@lucgagan
Copy link

Confirmed that pinning version v8.10.0 fixes the issue.

For what it is worth, Next.js team is refusing to fix this (CC @shuding) calling this pg issue – making pg unusable with Next.js.

@charmander charmander changed the title Bug: 0.8.11 - Return outside of function 8.11.0 - Return outside of function Jun 15, 2023
@gronka
Copy link

gronka commented Jul 24, 2023

@lucgagan
How do you pin versions? I added this to package.json and ran rm -rf node_modules && npm install

	"overrides": {
		"node-postgres": "8.10.0"
	}

When I set "pg": "8.10.0", I get the same error

@orlein
Copy link

orlein commented Jul 24, 2023

I just commented out the if clause using patch-package because I'm using node v18.12.0

  1. comment out the problematic code in node_modules/pg/lib/crypto/utils.js
  2. run npx patch-package pg
  3. add "postinstall": "patch-package" inside package.json's scripts
  4. run yarn add -D patch-package

and your project will automatically comment out the if clause whenever you and your colleague run yarn install.

check out for the further info: https://www.npmjs.com/package/patch-package

@marinamashina
Copy link

We ran into the same issue but on 8.5.1 while this was not happening before. Looking for any practical explanation and solution to this

@onethread
Copy link

This can be changed in pg for everyone’s convenience, but that sounds like a bug in whatever tool isn’t recognizing a legitimate (and not infrequently used) feature of CommonJS modules.

Hm, I must admit, I haven't personally returned outside of a function in possibly ever. Is that really legitimate, or more of a convention that stuck around? The ECMAScript standard seems to consider that a mistake.

@Enalmada
Copy link

Here is the build error when trying to use pg with Next.js:

Failed to compile.

./node_modules/.pnpm/[email protected]/node_modules/pg/lib/crypto/utils.js
Error:
  × Return statement is not allowed here
    ╭─[\node_modules\.pnpm\[email protected]\node_modules\pg\lib\crypto\utils.js:4:1]
  4 │ if (useLegacyCrypto) {
  5 │   // We are on an old version of Node.js that requires legacy crypto utilities.
  6 │   module.exports = require('./utils-legacy')
  7 │   return
    ·   ──────
  8 │ }
  9 │
 10 │ const nodeCrypto = require('crypto')
    ╰────

Caused by:
    Syntax Error

I believe the average new user importing pg for the first time in some tutorial is going to struggle to know what is going on or how to work around. Could this be fixed?

@Jolg42
Copy link

Jolg42 commented Jul 31, 2023

It looks like this was fixed in #3033, now waiting for a release 🎊

@brianc
Copy link
Owner

brianc commented Aug 1, 2023

Sorry for the delay in releasing this! I got a new patch version up last night....should be good to go now. 😄

@tiavina-mika
Copy link

I'm having this issue. Is it fixed?

@charmander
Copy link
Collaborator

@tiavina-mika Yes, it is fixed. See the comment above yours.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.