在Ubuntu上使用Node.js进行开发,可以按照以下步骤进行:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash nvm install --lts node -v npm -v sudo apt update sudo apt install nodejs npm node -v npm -v curl -sL https://deb.nodesource.com/setup_current.x | sudo -E bash - sudo apt install nodejs node -v npm -v npm install mkdir ~/.npm-global npm config set prefix '~/.npm-global' echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc source ~/.bashrc 例如,创建一个简单的Node.js服务器:
const http = require('http'); const server = http.createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello World '); }); server.listen(3000, () => { console.log('Server running on port 3000'); }); 保存为server.js,然后在终端中运行:
node server.js 打开浏览器,访问http://localhost:3000,你应该会看到“Hello World”的消息。
以上步骤可以帮助你在Ubuntu上成功设置Node.js开发环境,并开始进行开发。