Node not finding Less module

2019-02-22 04:48发布

问题:

I am trying to install Node and Less on Ubuntu 12.1. Installing appears to work ok and I can see there is a Less folder created when I install Less using "sudo npm g install less" in:

/usr/local/lib/node_modules

However when I run "lessc -v" I get the below:

module.js:340
    throw err;
          ^
Error: Cannot find module 'less'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)
    at Object.<anonymous> (/usr/bin/lessc:8:12)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)

Any ideas where I am going wrong?

回答1:

You should create symlink to node modules directory into your home directory.

$ ln -s /usr/lib/node_modules/ ~/.node_libraries

(if you installed node from source it'll be /usr/local/lib/node_modules)



回答2:

Modules installed in the "global scope" (which is what happens when you use npm -g install or install node modules from Ubuntu repositories, for example - sudo apt-get install node-less) are loaded by populating the NODE_PATH environment variable.

Packages installed from Ubuntu repositories handle this automatically, but if you install manually you have to set this yourself. In your case, you'd need to add the following command to your .bash_profile:

export NODE_PATH="$NODE_PATH:/usr/local/lib/node_modules"


回答3:

sudo ln -s /etc/alternatives/node /usr/local/bin/node

It works at my Ubuntu 13.04