Ubuntu – How to get jshint to work


I installed jshint using sudo npm install -g jshint, and it seems to have worked:

$which jshint

Yet running jshint or its full path equivalent doesn't seem to do anything. It doesn't give an error, either–it just does nothing:

$jshint --help
$jshint --verbose
$jshint --debug

$jshint admin.js
$jshint admin.js --verbose
$/usr/local/bin/jshint admin.js

I also tried symlinking nodejs per this answer:

$sudo ln -s /usr/bin/nodejs /usr/bin/node

$jshint admin.js --verbose
$jshint admin.js
$jshint --help
$man jshint
No manual entry for jshint
See 'man 7 undocumented' for help when manual pages are not available.

What am I missing here?

Edit: here's the output from apt-cache policy nodejs npm. I'm just using the standard Ubuntu 14.04 packages, so there's nothing very special about this:

└─>apt-cache policy nodejs npm
  Installed: 0.10.25~dfsg2-2ubuntu1
  Candidate: 0.10.25~dfsg2-2ubuntu1
  Version table:
 *** 0.10.25~dfsg2-2ubuntu1 0
        500 http://us.archive.ubuntu.com/ubuntu/ trusty/universe amd64 Packages
        100 /var/lib/dpkg/status
  Installed: 1.3.10~dfsg-1
  Candidate: 1.3.10~dfsg-1
  Version table:
 *** 1.3.10~dfsg-1 0
        500 http://us.archive.ubuntu.com/ubuntu/ trusty/universe amd64 Packages
        100 /var/lib/dpkg/status

Best Answer

The official repos are a mess...

Installing Node and npm through the official repositories has always given me trouble. I suspect that this could be due to tools depending on node, when the actual command on Ubuntu is nodejs, which is why creating a symlink should work. It worked for me in my quick test in a virtual machine, but since it didn't work for you, let's try something else...

So, install Node.JS a different way

I generally prefer to install Node on Ubuntu using NVM. This avoids the issue of the node command not being defined on Ubuntu, and it avoids the permissions mess that requires using sudo to install npm modules.

Do the following to get back to a clean slate:

Remove any global modules you've installed with npm (instructions here):

sudo npm list -g --depth=0. | awk -F ' ' '{print $2}' | awk -F '@' '{print $1}'  | sudo xargs npm remove -g

Remove symlinked node folder:

sudo rm /usr/bin/node

Uninstall the copies of nodejs and npm that you installed through Ubuntu's repositories:

sudo apt-get remove --purge nodejs npm

Clean up any other cruft:

sudo apt-get autoremove

At this point, which node, which nodejs, which npm, and which jshint should all return nothing.

Reinstall Node, skipping the official repositories

Grab the latest copy of NVM (you may need to sudo apt-get install curl first):

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.12 # or whatever the latest version is

And tell nvm which version of Node you want to use

nvm use 0.12

Now you should be able to run Node with the node command, you should be able to install modules globally without sudo, and you should end up with a working copy of jshint.

If you don't want to run nvm use v0.12 every time you start a new terminal session, you can add nvm use v to your ~/.bashrc. The v isn't special, it will just match v* and find the latest version. Alternatively you could hard-code a specific version.

Related Question