Home > Software design >  How to deploy the node.js application on server with different node version
How to deploy the node.js application on server with different node version

Time:07-13

I want to deploy my node app on server. but when I upload the node app to our server and then add node.js instalation then It is not opening the file. the index.js file is

const connectToDb = require("./DBconfig");
const express = require('express')
const router = express.Router();
var cors = require('cors');

connectToDb();
const app = express();
const port = process.env.PORT | 5000;
app.use(cors());

app.use(express.json());
connectToDb();

app.use( '/api/user' ,require('./routes/user') );
app.use( '/api/post' ,require('./routes/post') );

app.get('/', (req, res) => {
  res.send('The site is start now')
})

if(process.env.NODE_ENV === "production"){
  app.use(express.static("client/build"));
  const path = require("path");
  app.get("*",(req,res)=>{
    res.sendFile(path.resolve(__dirname,'client','build','index.html'))
  })
}

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

The node version in my local machine is v16.14.0 but the node version that the NameCheap is given is v14.18.3

the node installation in Namecheap that I configure is the installation of node

the error message is triggered, when I hit the URL of API API's URL, is the error message. I am unable to find the solution to solve this problem. I read some articles in which the writer tells that this type of error occurs when we use the different node versions in the local machine and on the server machine but I can't do anything because I restrict to use of the v14.18.3 on the server-side but I used the v16.14.0 in my local machine. can anyone tell me the solution to solve this type of problem?

CodePudding user response:

webi is also a great tool. I recommend completely uninstalling node and installing it fresh with webi.

# this will install node and the webi cli
# Linux/Mac
curl -sS https://webinstall.dev/node | bash

# Windows
curl.exe -A "MS" https://webinstall.dev/node | powershell

# then you can run the following to switch versions
webi node@14

See https://webinstall.dev/node/ for more info.

CodePudding user response:

nvm is another useful tool that can help you to control the node version itself, you can use nvm commands to upgrade the node version quite easily.

nvm install <version-number>
nvm use 0.10

using nvm use -nodeversion you can switch your node js version to your server node js version.

  • Related