how to setup nodejs
install nodejs for a single user
nodejs apps needs to be run under user not glabally or even root this will encounter errors
add the user in sudoers file
usermod -aG sudo username
passwd username
then
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
strap5887@vmi728455:~/public_html/strapi-backend$ sudo mkdir -p /home/strap5887
if you make anychnges to bashrc just do relogin changes does not work without relogin or exit
Install nodejs globally
# Install node
sudo apt install -y curl
curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
# Set node global packages folder as /usr/local/lib/node_modules
NPM_PACKAGES="/usr/local/lib/node_modules"
sudo mkdir -p $NPM_PACKAGES
sudo chmod 777 $NPM_PACKAGES
# Update the path and manpath to read from npm packages
echo "export PATH="\""\$PATH:$NPM_PACKAGES/bin"\""
export MANPATH="\""\${MANPATH-\$(manpath)}:$NPM_PACKAGES/share/man"\""
npm config set prefix $NPM_PACKAGES" | sudo tee '/etc/profile.d/node-path.sh'
source /etc/profile.d/node-path.sh
to check all node version
nvm ls-remote
nvm install v14.17.3
How can the default node version be set using NVM?
nvm alias default 6.11.5
if you want it pegged to that specific version.
You can also do nvm alias default 16
.
Either way, you'll want to upgrade to the latest version of nvm
(v0.33.11 as of this writing)
$ nvm alias default 16.14.2
# nvm set default node.js version 16.14.2
$ node -v
# v16.14.2
root@server:/home/cloudvariation.in/public_html/blogar# nvm alias default 16.18.0
default -> 16.18.0 (-> v16.18.0)
nvm use default
root@server:/home/cloudvariation.in/public_html/blogar# nvm alias default node
default -> node (-> v16.18.0)
root@server:/home/cloudvariation.in/public_html/blogar# nvm use default
Now using node v16.18.0 (npm v8.19.2)
root@server:/home/cloudvariation.in/public_html/blogar# node -v
v16.18.0
This will set the default to be the most current version of node
nvm alias default node
and then you'll need to run
nvm use default
or exit and open a new tab
for a specific version
nvm use v18.12.1
there is file in src > server.js
server.js
const http = require("http");
const app = require("./app");
const port = process.env.PORT || 8088;
// const port = process.env.PORT || 8088;
// const port = process.env.PORT || 8061;
const server = http.createServer(app);
server.listen(port);
console.log("listening at port ", port);
How to Setup Apache As Frontend Proxy for Node.js
and add the following content.
1 2 3 4 5 6 7 | <VirtualHost *:80> ServerName example.com ProxyPreserveHost On ProxyPass / http://localhost:3000/ ProxyPassReverse / http://localhost:3000/ </VirtualHost> |
Install pm2
sudo apt-get install node-express
npm install express
npm install jade
npm install socket.io
Error: Cannot find module 'Cors'
cors c was in capital that why gettting error
Error: getaddrinfo ENOTFOUND api.krishit.com
make local entry in /etc/hosts
192.227.112.108 api.krishit.com vms.krishit.com
root@server:~/.nvm/versions/node# ls
v14.0.0 v14.17.3 v16.17.1 v16.18.0
root@server:~/.nvm/versions/node# pwd
/root/.nvm/versions/node
PM2 using wrong node.js version
autostart nodejs on boot
root@vmi1093744:~# cat /lib/systemd/system/app.service
[Unit]
Description=app
After=multi-user.target
[Service]
Type=idle
ExecStart=/root/.nvm/versions/node/v16.13.2/bin/node /home/alphawa/public_html/api.alpha-media.marketing/app.js
#WorkingDirectory=
User=root
[Install]
WantedBy=multi-user.target
sudo chmod 644 /lib/systemd/system/app.service
sudo systemctl daemon-reload
sudo systemctl enable app.service
sudo systemctl start app.service
sudo systemctl status app.service
to check all node version
nvm ls-remote
nvm install v14.17.3
to check node js installed verions
nvm list
remove nvm
nvm uninstall 18.10.0
For later fellow googlers:
pm2 update
This helped me greatly. Just in case future readers, you can check which node version pm2 is using with this command: pm2 show <appId/ appName>
pm2 monit
is another way to check the node version
Comments
Post a Comment