"trying to overwrite '/usr/share/man/man1/node.1.gz', which is also in package nodejs-legacy 0.10.25~dfsg2-2ubuntu1"

I am trying to install nodejs but it is repeatedly failing. Attached is the log

$ sudo apt-get install nodejs
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libc-ares2 libv8-3.14.5
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
  rlwrap
The following NEW packages will be installed:
  rlwrap
The following packages will be upgraded:
  nodejs
1 upgraded, 1 newly installed, 0 to remove and 11 not upgraded.
Need to get 0 B/4,391 kB of archives.
After this operation, 14.9 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Selecting previously unselected package rlwrap.
(Reading database ... 263979 files and directories currently installed.)
Preparing to unpack .../rlwrap_0.37-5_amd64.deb ...
Unpacking rlwrap (0.37-5) ...
Selecting previously unselected package nodejs.
Preparing to unpack .../nodejs_0.10.31-1chl1~trusty1_amd64.deb ...
Unpacking nodejs (0.10.31-1chl1~trusty1) over (0.10.25~dfsg2-2ubuntu1) ...
dpkg: error processing archive /var/cache/apt/archives/nodejs_0.10.31-1chl1~trusty1_amd64.deb (--unpack):
 trying to overwrite '/usr/share/man/man1/node.1.gz', which is also in package nodejs-legacy 0.10.25~dfsg2-2ubuntu1
dpkg-deb: error: subprocess paste was killed by signal (Broken pipe)
Processing triggers for man-db (2.6.7.1-1) ...
Errors were encountered while processing:
 /var/cache/apt/archives/nodejs_0.10.31-1chl1~trusty1_amd64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)

Potentially Fixing the Installation

As Charles said in the comments, I would try to run sudo apt-get update and sudo apt-get upgrade. You may also want to run sudo apt-get autoremove to remove packages that you no longer need.

The error mentions the nodejs-legacy package. If you've ever installed that, you may want to remove it.

A better way to install Node on Ubuntu

Unless you are planning on using your computer as a production server, I'd strongly recommend installing Node.js using the NVM tool. Installing from the repository requires you to use sudo, which leads to messy permissions when you try to install Node modules using NPM.

As a bonus, NVM lets you install multiple versions of Node, and it lets you keep your copy of Node up-to-date, even if the official repositories are behind.

Grab the latest copy of NVM

curl https://raw.githubusercontent.com/creationix/nvm/v0.15.0/install.sh | bash

Tell your shell to use nvm

source ~/.nvm/nvm.sh

Then install a node version

nvm install 0.10

And tell nvm which version of Node you want to use

nvm use 0.10

One thing which i had missed was to purge the previous installation and reinstall. I used the following command to remove the previous install

sudo apt-get purge nodejs-legacy nodejs

and

sudo apt-get install nodejs

to reinstall nodejs.

Linking the SO answer which helped me