npm install less but can't require via node - node.js

mac os x 10.10.5
my node and npm version.
node -v
v4.2.1
npm -v
2.14.7
then refer to official website I install less.
sudo npm install -g less
sudo npm install -g less-plugin-clean-css
and I confirm that successfully installed in node_modules
ls /usr/local/lib/node_modules/
appcelerator generator-webapp ios-sim yo
bower grunt-cli less
cordova gulp less-plugin-clean-css
express ionic npm
but when I use node command line to require less, some error occured..
> var less = require('less')
Error: Cannot find module 'less'
at Function.Module._resolveFilename (module.js:337:15)
at Function.Module._load (module.js:287:25)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at repl:1:12
at REPLServer.defaultEval (repl.js:164:27)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
at REPLServer.<anonymous> (repl.js:393:12)
at emitOne (events.js:82:20)
>
I google it but find nothing which could solve it, please some help ..

require doesn't load global modules by default. the modules must be located in the local node_modules folder.
If you really want to use global modules, you can set the NODE_PATH env variable (but this is bad practice in my opinion):
export NODE_PATH=/usr/local/lib/node_modules/
Here is some background information.

i think your less module in not install perfectly,
As you used command sudo npm install -g less It install Less module globally,
Now you redirect your terminal to your project folder and Use the Following Command sudo npm install less ,remember use it without using -g.
For more information you can visit the enter link description here

I'm install it locally, without '-g' flag in working folder and now it works.
You should open issue on their gitbug if you want to install it correctly globally.

Related

Cannot find module `dist/bin/x.js` when trying to use the command that comes with the package after npm global install

You did npm install -g aVeryCoolPackage and when you want to use aVeryCoolPackage's command in your shell you get an error like this:
Error: Cannot find module '/usr/local/lib/node_modules/aVeryCoolPackage/dist/bin/cli.js'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at loadAVeryCoolPackage (/usr/local/lib/node_modules/aVeryCoolPackage/bin/aVeryCoolPackage.js:30:3)
at /usr/local/lib/node_modules/aVeryCoolPackage/bin/aVeryCoolPackage.js:44:5
at LOOP (fs.js:1758:14)
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
After you cd into /usr/local/lib/node_modules/thePackage to your surprise you see that the folder dist does not exist at all. This is strange. You tried npm uninstall -g aVeryCoolPackage and then npm install -g aVeryCoolPackage again but you run into the same problem when trying to use its command. Everyone else on github is not running into this problem. What is going on?
In my case I had cloned the repo from github. And I did npm install -g aVeryCoolPackage at the same directory as where the repo is, so it actually installed my local copy of it where it has .gitignore the dist folder. As a result I didn't have dist in /usr/local/lib/node_modules/aVeryCoolPackage/ and it threw the Cannot find module error every time it tried to require files in it.
Fun fact: You would have no idea how the error arises if you npm uninstall -g aVeryCoolPackage and npm install -g aVeryCoolPackage at a different directory where the clone of the repo is not there and getting it fixed as a result. It would become one of those mysteries in node development where sometimes you get strange errors and then you stop seeing them.

npm cannot find module

I've been all over Google and StackOverflow, viewing the various threads related to similar issues, but so far nothing has worked. My problem is that npm seems to be installing improperly...or something, I honestly have no idea: Node and npm are a dependency I use to support the Sage Wordpress theme buildout I'm working on on a different machine.
So the issue is that after uninstalling Node (to be safe), and reinstalling, Node seems to be working fine. node -v returns v5.1.0. Great. npm should be installed with it, but when I run npm -v I get the following lovely bit of error:
$ npm
module.js:340
throw err;
^
Error: Cannot find module 'readable-stream'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:289:25)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (/Users/Daniel/.nvm/versions/node/v5.1.0/lib/node_modules/npm/node_modules/ npmlog/node_modules/are-we-there-yet/index.js:2:14)
at Module._compile (module.js:425:26)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Module.require (module.js:366:17)
Running any npm command (even just npm) gives me this error, so as suggested in other solutions, I cannot simply run npm install readable-stream. I have also gone through various solutions (removing the node_modules folder, installing via brew, installing via brew without npm, and several others), but none seem to be working.
Any thoughts would be much appreciated.
I was just wrestling with this problem for half an hour (on Mac) after a failed npm update. I'm using Homebrew and I found I had to do the following:
sudo chown -Rv $USER /usr/local/lib/node_modules/ with my username
Then I reinstalled node via Homebrew:
brew uninstall node
brew install node
By this point npm was working again and I went ahead with a successful npm i -g npm
If you're having problems with NPM and you want to reinstall on UNIX I found this to be the easiest solution:
curl -L https://npmjs.org/install.sh | sudo sh
This will reinstall NPM and get it setup to use the latest NPM Version. This will almost always resolve any issues regarding NPM.
When the process is complete you should receive a message saying It worked.
You can run with command line as below:
npm install // you need remove node_modules folder, run npm install

Yo not working on Windows machine.

I am working on a windows machine.
Here are my node / npm versions
C:\dev\my-yo-project>node --version
v4.1.1
C:\dev\my-yo-project>npm --version
3.5.2
I am trying to install yeoman. Everything goes fine, but the yeoman doctor complains.
Yeoman Doctor
Running sanity checks on your system
√ Global configuration file is valid
√ NODE_PATH matches the npm root
× Node.js version
Your Node.js version is outdated.
And when I try to run any yo command it looks at some odd place and complains that it could not find the required js.
C:\dev\my-yo-project>yo webapp
module.js:338
throw err;
^
Error: Cannot find module 'C:\Users\MYUSERNAME\AppData\Roaming\npm\node_modules\yo\lib\cli.js'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:286:25)
at Function.Module.runMain (module.js:475:10)
at startup (node.js:117:18)
at node.js:951:3
It should not look at the place it is looking at. It should look at
C:\nodejs\4.1.1\node_modules\npm
which is set in the PATH variable.
Any help please.
Try this:
$ npm cache clean -f
$ npm install -g npm
$ npm install -g yo

npm installing itself without dependancies

I am trying to install the latest npm, following the instructions on the npm doc site.
My problem is that using command:
npm install -g npm
to get the latest version of NPM does not seem to work: it does not seem to install any of the needed dependencies, so when I run it I get errors such as:
module.js:340
throw err;
^
Error: Cannot find module 'are-we-there-yet'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/home/harmic/.node_modules/lib/node_modules/npm/node_modules/npmlog/log.js:2:16)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
I am using a Centos 6 machine, so I started by installing node + npm from EPEL, like this:
yum install nodejs npm
That went OK. I then went to install the latest npm, as recommended at the above link and many other pages I've seen. My first attempt
sudo npm install -g npm
was not so great, for two reasons:
It installed in /usr/lib/node_modules, over the top of the original RPM installation. I judged this to be extremely poor practice, because the next time I did yum update on the system there was every chance the newly installed files would be overwritten by whatever incoming RPM packages contained, leaving an unholy mess.
It did not work anyway: I could not start npm at all, it constantly complained of missing dependancies as described above.
So I uninstalled the RPMs, removed all the left over files, then reinstalled the RPMs again. This time I created a ~/.npmrc file and put this in it:
prefix = ${HOME}/.node_modules
I also set NODE_PATH environment variable to include that path. After this, npm did install itself in my desired location, but did not install any dependencies:
$ npm install -g npm
npm http GET https://registry.npmjs.org/npm
npm http 304 https://registry.npmjs.org/npm
/home/harmic/.node_modules/bin/npm -> /home/harmic/.node_modules/lib/node_modules/npm/bin/npm-cli.js
npm#3.3.3 /home/harmic/.node_modules/lib/node_modules/npm
Running it from the new installed path gave the error at the top of the page.
Despite thinking that I must be doing something seriously wrong, I set about trying to install the unmet dependencies, one at a time:
npm install -g are-we-there-yet
npm install -g gauge
npm install -g os-tmpdir
npm install -g os-homedir
npm install -g is-absolute
npm install -g asap
npm install -g path-is-absolute
npm install -g builtins
npm install -g validate-npm-package-license
npm install -g concat-stream
npm install -g json-parse-helpfulerror
npm install -g readdir-scoped-modules
npm install -g debuglog
npm install -g lodash._baseclone
npm install -g lodash._bindcallback
npm install -g lodash._baseflatten
npm install -g lodash._baseuniq
...
and still it complains about needing more.
Finally I have concluded that I must have missed something important, so I decided to ask for the help of the SO community...
The initially installed versions of node & npm RPMs are:
nodejs-0.10.36-3.el6
npm-1.3.6-5.el6
Oh and in case you are wondering, the reason I am trying to update npm in the first place is because when trying to install this package I ran into a build error, the solution to which is apparently to install an updated npm.
First of all I would suggest you to forget to install node with RPM/APT etc, the reason for this is because normally they have outdate versions. Infact 0.10 is very old, 0.12 is nearly a year old and now there's 4.1 already. So my suggestion is to use nvm https://github.com/creationix/nvm
nvm is a Node Version Manager. the cool think is that you can switch version with a single command and stay always updated. After installing the node version that you want, you can also upgrade npm.
You can even select a node version for a certain folder in your system (if you have code that runs just in an old version for example)

Why is this error about Grunt when starting sails

I am using node 0.12.2 and sails 0.10.5
After doing
sudo npm install
sails fails to lift. I have to
sudo npm install cookie
and then
sudo npm install connect
to make sails lift. Then it shows some error like this:
error: Grunt :: module.js:338
throw err;
^
Error: Cannot find module '/home/ec2-user/someuser/node_modules/sails/node_modules/grunt-cli/bin/grunt'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:278:25)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
and
sudo npm install grunt-cli
does not do anything. nor does installing it globally.
sudo npm -g install grunt-cli
/usr/local/bin/grunt -> /usr/local/lib/node_modules/grunt-cli/bin/grunt
/usr/local/lib
└── grunt-cli#0.1.13
[ec2-user#ip-172-*****]$ sails lift
info: Starting app...
error: Grunt :: module.js:338
throw err;
^
Error: Cannot find module '/home/ec2-user/someuser/node_modules/sails/node_modules/grunt-cli/bin/grunt'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:278:25)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
Sails still can lift but where does this error come from and how to get rid of it?
The error message pretty describe what's wrong
Error: Cannot find module '/home/ec2-user/someuser/node_modules/sails/node_modules/grunt-cli/bin/grunt'
grunt-cli is missing. You need to install it using (might need sudo)
npm install grunt-cli -g
Edit: Make sure you are running sails lift from your sails project folder. (to me it looks like you are running it from root which is probably the cause of the issue)
Otherwise, I leave my other suggestions here:
Install sails globally: npm -g install sails
Install grunt globally: npm -g install grunt-cli
If the problem still exists after running npm install it is very likely to be related to permission issues. You can fix this permission problems using one of two options:
Change the permission to npm's default directory.
Change npm's default directory to another directory.
I recommend the latter.
Here is an official guide from NPM people on how to do both:
https://docs.npmjs.com/getting-started/fixing-npm-permissions

Resources