Node Server Unable to Start - node.js

Issue: Unable to start Node server
Platform: Bash on Ubuntu on Windows 10
Currently following tutorial that builds a blog on MEAN
Commands ran after installation:
apt-get install npm
npm init
npm install express --save
node server.js
Last command node server.js supposed to start Node server.
But nothing happens on my bash command line.
server.js codes:
command node-v (node version not found):
However, Node is already installed:
Accessing localhost:3000

On Ubuntu (and Debian as well), the correct package to install for Node.js is called nodejs:
$ sudo apt-get install nodejs
However, my experience is that this generally installs an outdated version of Node, which isn't great. It also installs the interpreter with the executable name nodejs, not node as you would expect.
Alternatively, you can add the official binary distribution repositories for Node.js, which allows you to install more recent versions of Node (v6 or even v7). These are also kept up-to-date much better than the Ubuntu/Debian repositories.
The process is documented here, but if you want to install Node v6, you would run this code:
$ curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
$ sudo apt-get install -y nodejs
It's probably best to remove any previously installed packages (npm, node, nodejs) before running the commands above.

Related

Node versions do not match: node vs. sudo node -v ... WSL2 Ubuntu 22.04.1

As you can see in the image below, I see two different versions of node depending on which command I run.
I need the newer version, but npm sees the old version.
Many times I've removed, purged, reinstalled, etc.
I tried installing nvm as root and setting the node version there, but that didn't help either.
If I use apt install nodejs instead of nvm, it tells me that I already have the newest version, which it believes is 12.22.9.
How do I get npm to recognize the newer version (18.12.1) of node that I installed via nvm?
It is because you have a different version of the Node.js which is installed for the root and that particular user.
First, you need to uninstall Node.js:
sudo apt-get remove nodejs
or
sudo npm rm npm -g
If you have any problem with the above commands, then after running which node command, go to that directory, and run the following commands:
rm -r bin/node bin/node-waf include/node lib/node lib/pkgconfig/nodejs.pc share/man/man1/node.1
Do the same thing for the current user if needed.
The default Ubuntu/Debian package manager does not have the latest Node.js, and that's why whenever you try to install Node.js with apt install nodejs it says you have the latest version.
According to the official Node.js documentation, for installing the latest version, you should follow these steps:
Using Ubuntu
curl -fsSL https://deb.nodesource.com/setup_19.x | sudo -E bash - &&\
sudo apt-get install -y nodejs
P.S: No need to run npm or node with sudo. Therefore, I highly recommend you to not use every command with sudo.

Can not Install NPM along side with the ROS melodic?

I am using ROS melodic for my current project but I need to install nodejs for my college work in ubuntu 18.04. When I install nodejs by using the following commands
sudo apt install nodejs
sudo apt install npm
it gives me an error like this.
error
So I installed that dependency but after installing that dependency my ros packages removed. How can I install the npm without losing the ROS melodic?
Thanks in advance !!
I found something on the internet that take me to the solution,
I installed python virtual environment
https://linuxize.com/post/how-to-create-python-virtual-environments-on-ubuntu-18-04/
in that virtual environment I installed nodeenv with the help of pip
https://pypi.org/project/nodeenv/#id73
I created node.js virtual environment in which I installed the NPM without getting any error.
And we can also install nodeenv with the global environment too!
I faced the same problem and solutions were deleting whole ros melodic.
found the solution as follows:
$ sudo apt-get purge nodejs npm
after this command if you installed nodejs it's going to be deleted.
enable the NodeSource repository by running the following curl command:
$ curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
then:
$ sudo apt-get install -y nodejs
the nodejs package contains both the node and npm binaries.

Can somebody please tell me if I am using the wrong node js version?

I have been trying to update nodejs for a long time now. When I run node -v, it tells me I am using: v0.10.40. But when I look at nodejs.org it says the v7.4.0?
I have cleaned the npm cache and done a reinstall with sudo n stable but still v0.10.40, and the last time that I updated npm it prompted me that my node version is too old and outdated.
What am I doing wrong. And can somebody tell me if v0.10.40 is the current or an outdated version?
You likely have two versions of node installed and in your path. If you are on OSX or linux, run which node (if on Windows, you may npm i -g #raider/which, and then run which node). This should give you the location of the old version of node.
Then run mv path/to/old/node path/to/old/node.bak, replacing with the actual path, and rerun node --version to see if it picks up the right version now.
UPDATE: If you are on Ubuntu or another Debian based Linux, you should install the latest with the following commands:
curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -
sudo apt-get install -y nodejs
UPDATE: If you are using nvm, run nvm install node followed by nvm use node in a new terminal to get the latest that nvm supports.

How to install nodejs 0.12.7

I've trying to install the latest version of nodejs
sudo apt-get nodejs
installs the version 0.12.25
When I downloaded the tar.gz from https://nodejs.org/ and installed
my node version upgraded to 0.12.7 but not nodejs version.
And when I tried like this
sudo apt-get install nodejs=0.12.7
E: Version '0.12.7' for 'nodejs' was not found
This error popped up. What can I do such that on entering
nodejs -v
I can get 0.12.7
Since there are a lot of Node.js versions, and there is also iojs, I suggest you to use a straightforward Node.js version manager, like the very good n.
So, first install n, then install Node.js 0.12.7 with:
$ n 0.12.7
When you install node.js from source by default it will install with node as name of the executable
node -v
Will show 0.12.7
If you need node.js for development purposes on your machine you can use nvm, it allows to install different versions of node.js and io.js, and easily switch between them
Remove node with command sudo apt-get remove node, then remove nodejs using sudo apt-get remove nodejs. After that try to install nodejs again with version you need sudo apt-get install nodejs=0.12.7.

Installed node.js ver 0.8 but node --version still shows previous version 0.6.12

I tried installing node ver 0.8 on my ubuntu 12.04.It already has a node ver 0.6.12.The installation went suceesfully but when i type in
node --version
it still shows previous version.
i tried to remove previous version using sudo apt-get remove node but it says package node is not installed.But on trying node --version it shows 0.6.12
Why is it so??
The problem is, you need to replace the new location for node with the old in your PATH variable. If you have an old manual install, find the old path to node by running echo $PATH. Then run this command:
export PATH=${PATH%$OLD_NODE_PATH/bin*}$NEW_NODE_PATH/bin${PATH#$*OLD_NODE_PATH/bin}
Or if you are using an install from the apt-get repository, just run:
export PATH=$NEW_NODE_PATH/bin
And that should fix your problem. But there is a better way! The best tool to manage your node.js environment is NVM. It exactly like RVM for ruby and similar to virtualenv for python, if you are familiar with those tools. It allows you to switch versions of node and download new ones extremely efficiently, and is easy to use. Download and install with:
curl https://raw.github.com/creationix/nvm/master/install.sh | sh
Then add this line to your bash (assuming you are running a bash shell) where it will be loaded (I prefer .bash_login for the personal stuff although it is not loaded by default):
[[ -s $HOME/.nvm/nvm.sh ]] && . $HOME/.nvm/nvm.sh
Source your bash script or restart the terminal then enter this command:
nvm install 0.8.0 && nvm use 0.8.0
This should set you up just fine. Although not necessary, you should probably get rid of all the other node installs, for the sake of tidiness. Check out their github page but to get you started here is a quick overview:
nvm ls # list all installed versions of node
nvm ls-remote # list all available versions of node
nvm install 0.9.8 # download and install node v0.9.8
nvm use 0.8.0 # switch current environment to use node v0.8.0
nvm alias default 0.8.0 # set 0.8.0 as default, you can use 'nvm use default'
nvm deactivate # use system install of node
nvm run default app.js # run app.js with default node version
I had this issue until I followd the directions on
https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager
which included running:
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
first. Then running sudo apt-get install nodejs npm got me to 0.8.x
Also see: http://apptob.org/
Seem like you install nodejs package from Ubuntu repo and manually install node 0.8 after?
Try remove nodejs package.
The way to get a more recent version of Node.js is to add a PPA (personal package archive) maintained by NodeSource. This will probably have more up-to-date versions of Node.js than the official Ubuntu repositories.
First, you need to install the PPA in order to get access to its contents:
curl -sL https://deb.nodesource.com/setup | sudo bash -
The PPA will be added to your configuration and your local package cache will be updated automatically. After running the setup script from nodesource, you can install the Node.js package using the below command.
sudo apt-get install nodejs
You can check the node by using this command
node -v

Resources