Win10 CMD执行docker-compose命令遇node 不存在错误,疑与CRLF/LF相关
node\r: No such file or directory Error in Docker + npm (CRLF/LF Issue) Hey there, that error you're hitting is absolutely tied to Windows' CRLF line endings clashing with your Linux-based Docker container. Let's break down what's going on and fix it step by step.
What's Causing This?
Windows uses CRLF (\r\n) as the default line ending, while Linux (which your Docker container runs on) uses LF (\n). When your project files—especially scripts in package.json or shebang lines in node_modules binaries like cross-env—are saved with CRLF, the Linux shell interprets the \r character as part of the command name. So it's looking for node\r instead of just node, hence the "No such file or directory" error.
Solutions to Try
1. Fix Line Endings at the Source (Git Settings)
This is the best long-term fix to prevent this issue from coming back:
- Global Git Config: Disable automatic line ending conversion globally on your Windows machine:
git config --global core.autocrlf false - Project-Specific .gitattributes: Create or update a
.gitattributesfile in your project root to enforce LF for relevant files:# Enforce LF for JavaScript, shell scripts, and package.json *.js text eol=lf *.sh text eol=lf package.json text eol=lf
After setting this, re-clone your project or reset your working tree to apply the correct line endings.
2. Fix Line Endings Directly in the Docker Container
If you need a quick fix right now, you can convert the problematic files inside the container:
- Spin up a bash shell in your container:
docker-compose run --rm bash - Install the
dos2unixtool to convert CRLF to LF:apt-get update && apt-get install -y dos2unix - Convert the key files causing the issue:
dos2unix node_modules/.bin/cross-env dos2unix node_modules/webpack/bin/webpack.js - Now run your dev command again:
npm run dev
3. Adjust Your npm Script to Bypass the Shebang Issue
Another workaround is to explicitly use node to execute the cross-env binary, avoiding the problematic shebang line:
Update the development script in your package.json to:
"development": "node node_modules/cross-env/dist/bin/cross-env.js NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
This way, you're directly calling node to run cross-env, skipping the shebang that's carrying the extra \r character.
Final Notes
I'd recommend starting with the Git settings fix—it'll prevent this from popping up every time you work on the project. The container-side fix is great for a quick test, but the Git config addresses the root cause.
内容的提问来源于stack exchange,提问作者Monia

