I’m working on a React/Firebase project with some teammates through GitHub. The weird thing is that everyone else can run the project fine, but I’m getting a strange issue on my machine.
The Problem
In my package.json file, I have this script:
"start": "node ./app/index.js"
When I run npm start, I get this error:
RangeError [ERR_BUFFER_OUT_OF_BOUNDS]: “length” is outside of buffer bounds at proto.utf8Write (node:internal/buffer:1066:13)
But here’s the crazy part - if I copy that exact same command node ./app/index.js and run it directly in my terminal, everything works perfectly!
What I’ve Tried
Changed Node versions using nvm to match my teammates
Updated npm to the latest version
Deleted and re-cloned the repository multiple times
Completely removed and reinstalled Node.js
Cleared node_modules and package-lock.json repeatedly
The only thing that helped temporarily was deleting the repo completely, but the error came back after a while. Has anyone seen this kind of behavior where npm scripts fail but the direct command works fine?
It seems you might be encountering an issue with environment variables differing when you run commands from npm as opposed to directly in the terminal. When executing npm start, it could be loading a different PATH or settings that interfere with your application’s buffer handling. A similar problem arose for me in the past when npm invoked an outdated version of a native dependency related to buffers. I recommend running npm config list and comparing it against node -p "process.env" to identify discrepancies. Additionally, check for any globally installed npm packages that could be causing interference. Often, conflicts arise from npm lifecycle scripts or configurations in .npmrc that modify your execution context. Since the problem seems to occur after multiple clones, it’s possible that something is accumulating in your npm cache or local configuration that triggers the issue.
Classic npm execution context issue. I’ve hit this exact problem before - npm handles environment variables differently than your shell does. That buffer error pops up when there’s a string encoding mismatch between npm’s process and running it directly in terminal. Create a fresh .npmrc file in your project root or check for global npm configs that might be messing things up. Also verify npm isn’t using a different working directory - just add pwd before your node command in package.json to test. npm sometimes changes execution context in weird ways that break buffer allocation, especially with Firebase projects doing heavy string processing. Since it works after fresh clones but breaks later, something’s getting corrupted in your persistent config.
npm’s probly passing extra flags or env vars that mess up buffer allocation. Try npm start --verbose to check what’s happening - npm sometimes adds hidden stuff that breaks things even if the base command runs fine.