Skip to content

chore: enforce explicit Buffer import and add lint rule - #6525

Merged
bjohansebas merged 1 commit into
expressjs:masterfrom
shivarm:explicitly-require-statement-buffer
Jun 28, 2025
Merged

chore: enforce explicit Buffer import and add lint rule#6525
bjohansebas merged 1 commit into
expressjs:masterfrom
shivarm:explicitly-require-statement-buffer

Conversation

@shivarm

Copy link
Copy Markdown
Contributor

explicitly import Buffer using const { Buffer } = require('node:buffer') for clarity and future compatibility, following Node.js best practices.

@bjohansebas

Copy link
Copy Markdown
Member

I'm not sure this is a good practice, Buffer is a global property

@krzysdz

Copy link
Copy Markdown
Contributor

I'm not sure this is a good practice, Buffer is a global property

Node.js docs say:

While the Buffer class is available within the global scope, it is still recommended to explicitly reference it via an import or require statement.

This sentence replaced the previous

The Buffer class is within the global scope, making it unlikely that one would need to ever use require('buffer').Buffer.

in nodejs/node#39043, but I can't find any arguments for why it is recommended. I think that it's just a cosmetic change for compatibility (or explicit incompatibility) reasons with other environments, but that's just a guess.

@bjohansebas

Copy link
Copy Markdown
Member

@krzysdz Thanks for the references

@wesleytoddwesleytodd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to keep consistency, so if anyone is willing I think we have a bunch of other packages that could use this change.

FWIW, it is a cosmetic change but is still worth it to do because it sets a good example for anyone reading this code to learn from.

@Phillip9587

Copy link
Copy Markdown
Member

@shivarm Could you please update all occurrences of Buffer?

@wesleytodd If we want to keep that consistent we should add a lint rule to prevent usage of global Buffer:

rules:
no-restricted-globals:
- error
- name: Buffermessage: Use `import { Buffer } from "node:buffer"` instead of the global Buffer.

@Phillip9587Phillip9587 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above

@wesleytodd

Copy link
Copy Markdown
Member

@wesleytodd If we want to keep that consistent we should add a lint rule to prevent usage of global Buffer:

I am against one off lint rule discussions on principal, but I agree it would be best if we had these things enforced by linting. Since we have an eslint config already in the repo, I am not opposed to adding one as a temporary step until we get a better setup overall. @shivarm you could add this rule so that you can find and fix all the other implicit global usage.

Signed-off-by: Shivam Sharma <meshivam81@gmail.com>
@shivarm
shivarmforce-pushed the explicitly-require-statement-buffer branch from 7611815 to bb8ac68CompareJune 20, 2025 16:25
@shivarm

Copy link
Copy Markdown
ContributorAuthor

@wesleytodd Done 👍

@shivarmshivarm changed the title Buffer: keep nodejs best practicechore: enforce explicit Buffer import and add lint ruleJun 20, 2025

@Hardanish-SinghHardanish-Singh left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@bjohansebas
bjohansebas merged commit 98c85eb into expressjs:masterJun 28, 2025
@shivarm
shivarm deleted the explicitly-require-statement-buffer branch June 28, 2025 05:02
@UlisesGasconUlisesGascon mentioned this pull request Nov 25, 2025
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@shivarm@bjohansebas@krzysdz@Phillip9587@wesleytodd@UlisesGascon@Hardanish-Singh