In a context file using use client I’m importing buffer:
import { Buffer } from 'buffer';
but Sonarqube in VSC is citing:
Node.js built-in modules should be imported using the “node:” protocol (typescript:S7772)
with docs of:
Why is this an issue?
When importing Node.js built-in modules, using the node: protocol makes it explicitly clear that you’re importing a core Node.js module rather than a third-party package from npm.
Without the node: prefix, it can be ambiguous whether import fs from 'fs' refers to the built-in file system module or a potential npm package named ‘fs’. This ambiguity can lead to confusion, especially for developers who are new to a codebase or when reviewing code.
The node: protocol was introduced in Node.js 12.20.0 and became the recommended approach for importing built-in modules. It provides several benefits:
- Clarity: Makes it immediately obvious that the import refers to a Node.js built-in module
- Security: Prevents potential confusion attacks where malicious packages could have names similar to built-in modules
- Future-proofing: Aligns with Node.js best practices and ESM standards
- Consistency: Creates a uniform way to reference all built-in modules
This practice is particularly important in larger codebases where the distinction between built-in modules and external dependencies needs to be clear at a glance.
What is the potential impact?
Using imports without the node: protocol can lead to confusion about whether a module is built-in or external. In rare cases, this could create security vulnerabilities if malicious packages with names similar to built-in modules are installed. The lack of explicit protocol also makes code less self-documenting and harder to understand for new team members.
How to fix?
Add the node: prefix to ES6 import statements when importing Node.js built-in modules.
Assumption
but my understanding this is wrong for buffer:
- Use
import { Buffer } from 'buffer'in code that must run in the browser (client components). - The
"node:buffer"specifier refers to the Node.js core module namespace and is intended for Node runtime usage — it is effectively Node-only and may not be available or polyfilled in browser bundles.
Why
node:bufferis the Node builtin namespace import (same family asnode:fs,node:path). It explicitly points at the Node runtime implementation and signals the bundler/runtime to resolve the Node core module. Browsers do not provide Node core modules, so importingnode:bufferin client-side code will usually fail unless you have a bundler/polyfill that maps it to a browser shim.- The npm package
bufferis a browser-compatible shim that implements the same API. Importing from'buffer'resolves to that package (or a bundler-provided polyfill) and is the correct choice for code that may run in the browser.
is this assumption not right and is this an error in Sonarqube?