express icon indicating copy to clipboard operation
express copied to clipboard

Buffer: keep `nodejs` best practice

Open shivarm opened this issue 7 months ago • 3 comments

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

shivarm avatar May 22 '25 03:05 shivarm

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

bjohansebas avatar May 29 '25 00:05 bjohansebas

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 https://github.com/nodejs/node/pull/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.

krzysdz avatar May 29 '25 09:05 krzysdz

@krzysdz Thanks for the references

bjohansebas avatar May 29 '25 14:05 bjohansebas

@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: Buffer
      message: Use `import { Buffer } from "node:buffer"` instead of the global Buffer.

Phillip9587 avatar Jun 18 '25 21:06 Phillip9587

@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.

wesleytodd avatar Jun 19 '25 15:06 wesleytodd

@wesleytodd Done 👍

shivarm avatar Jun 20 '25 16:06 shivarm