在虚拟主机上为Laravel安装Node.js遇问题求助
Hey there, let's work through this Node.js/NVM issue you're hitting on your shared hosting for your Laravel project—you mentioned you got it working a year ago, so we can definitely get things sorted again!
First, Let's Break Down the Root Problems
Your error log points to two main roadblocks:
- System Library Version Mismatch: The Node.js version you tried (v10.2.1) needs newer versions of
GLIBCandGLIBCXXthan your shared hosting provides. Shared hosts almost never let you upgrade system-level libraries, so we'll need to pick a Node version that plays nice with your host's existing setup. - NPM Prefix Conflict: NVM is throwing an error because your npm
prefixconfig is set to an empty value, which clashes with how NVM manages Node installations.
Step 1: Clean Up the Broken Node Installation
First, let's remove the incompatible Node version you tried to install:
nvm uninstall v10.2.1
Step 2: Install a Node Version Compatible with Your Host's Libraries
Older Node.js versions have lower system library requirements. For hosts running older OSes (like CentOS 6), Node.js 8.17.0 is a reliable choice—it's the final LTS release of Node 8 and works with older GLIBC/GLIBCXX versions.
Install it via NVM:
nvm install 8.17.0
Set it as your default Node version so it loads automatically whenever you log in:
nvm alias default 8.17.0
Step 3: Fix the NPM Prefix Conflict
After installing the compatible Node version, run the command NVM suggested to resolve the prefix mismatch:
nvm use --delete-prefix 8.17.0
To make this fix permanent, add this line to your ~/.bashrc or ~/.bash_profile file (whichever your host uses for shell configurations):
nvm use 8.17.0 > /dev/null 2>&1
Step 4: Verify Everything Works
Check if Node and npm are functioning correctly:
node -v npm -v
You should see output like v8.17.0 for Node and a corresponding npm version (around 6.13.4). Once that's confirmed, you can run Laravel's asset commands like npm run watch directly on your host.
Alternative: Go Back to Your Original Working Method
If you still run into snags with hosting-side Node, remember your original approach was totally valid:
- Compile all your Laravel assets locally using
npm run dev(for development) ornpm run prod(for production) - Upload the compiled files from your local
public/cssandpublic/jsdirectories to your virtual host - This way, you don't need Node.js installed on the host at all—all the heavy lifting happens on your local machine
内容的提问来源于stack exchange,提问作者OurBG

