CentOS7服务器无法安装Express?Node.js版本不符问题求助
Hey there, let's break down what's going on here and get your Express installation sorted out step by step.
The Node.js 8.11.1 you installed isn't being prioritized by your system. CentOS 7 comes with an older Node.js version (v6.3.1) pre-installed from its default repositories. Plus, the path to your manually installed Node isn't at the top of your system's PATH environment variable—so the system is calling the old version first. That's why your version checks show v6.3.1 instead of v8.11.1, and it's the reason your Express install is failing (due to version incompatibilities or npm path/permission issues).
1. Verify Current Node/NPM Paths
First, let's confirm where the system is pulling node and npm from right now:
which node which npm
You'll probably see paths like /usr/bin/node or /usr/local/bin/node—these are the pre-installed old versions. Your manually installed node is in ~/bin (since you copied it there earlier), so note that path for later.
2. Remove the Old Node.js Version (Optional but Recommended)
If you don't need the v6.3.1 version, uninstall it to avoid future confusion:
sudo yum remove nodejs npm -y
3. Set Your Manual Node Installation as the Default
We need to make sure your ~/bin directory (where your v8.11.1 node lives) is at the front of your PATH so the system finds it first. Edit your bash profile:
vi ~/.bash_profile
Find the line starting with PATH= and adjust it to put $HOME/bin first, like this:
PATH=$HOME/bin:$PATH:$HOME/.local/bin:$HOME/.bin
Save and exit, then reload the profile to apply the changes:
source ~/.bash_profile
4. Confirm the Correct Versions
Now check your node and npm versions again—they should show the v8.11.1 you installed:
node --version npm --version
5. Install Express Successfully
With the correct Node/NPM versions active, you can install Express. For global access (to use the Express generator tool), run:
npm install -g express express-generator
Verify the install with:
express --version
If you need Express as a local dependency for your project, navigate to your app directory first:
cd /home/admin/web/my-domain.com/public_html npm install express --save
- If you can't uninstall the old Node version, you can still use your v8.11.1 directly with absolute paths to install Express:
But setting up the/home/admin/bin/node /home/admin/bin/npm install -g expressPATHis a cleaner, long-term fix. - Double-check that your
~/bindirectory has proper permissions:chmod 755 ~/bin
内容的提问来源于stack exchange,提问作者reswe teryuio

