Why doesn't node.js load the module I require? - javascript

I have node.js install on linux, and file.js. In the same directory I have node_modules directory with lru-cache module.
file.js does the following:
var lrucache = require('lru-cache')
But when I run it, it raises the following error:
Error: Cannot find module 'lru-cache'
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> (/opt/file.js:58:12)
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 Function.Module.runMain (module.js:497:10)
What is the problem? the same is working in other linux system.

Please try:
rm -rf node_modules && npm cache clean && npm install
Sometimes npm has an issue and the dependency lru-cache may not get properly installed.

Most node modules will have their own set of package dependencies so you cannot just copy the folder or clone the repository without making sure you are satisfying the module's dependencies.
The easiest way would be using npm for ALL package installations.
After you have run npm init in your project's root directory to set up your package.json use
$ npm install modulename --save
to install a package AND its dependencies. You can now safely use
var module = require('modulename');
throughout your whole project.
In case you cannot install your package via npm make sure all of its dependencies are installed as well by navigating to node_modules/modulename and running npm install (no arguments) here. This will install all dependencies that are listed in the modules own package.json file.

Related

Javascript Eslint Error: Cannot find module 'optionator'

We've got some error and we try to install optionator but nothing happened. We're using Linux
We're trying to npm i from package.json which include this dependencies
"optionalDependencies": {
"eslint-config-<name>": "github:<username>/<repo>"
}
then we tried to run npm i optionator but still get the same error
Error: Cannot find module 'optionator'
Require stack:
- /usr/share/nodejs/eslint/lib/options.js
- /usr/share/nodejs/eslint/lib/cli.js
- /usr/share/nodejs/eslint/bin/eslint.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:885:15)
at Function.Module._load (internal/modules/cjs/loader.js:730:27)
at Module.require (internal/modules/cjs/loader.js:957:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (/usr/share/nodejs/eslint/lib/options.js:12:20)
at Module._compile (internal/modules/cjs/loader.js:1068:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
at Module.load (internal/modules/cjs/loader.js:933:32)
at Function.Module._load (internal/modules/cjs/loader.js:774:14)
at Module.require (internal/modules/cjs/loader.js:957:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/usr/share/nodejs/eslint/lib/options.js',
'/usr/share/nodejs/eslint/lib/cli.js',
'/usr/share/nodejs/eslint/bin/eslint.js'
Anyone who experienced this, please help us. Thanks
So if you have not done an npm init then that is the issue. npm init initializes the project and will add the node_modules folder to the working directory, this will set it up as a node project. Then to install a dependency just run a npm install <dependency> although I do recommend doing a --save afterwards as well as it just ensures that the package is put in your current directory
$ npm init
$ npm install optionator --save
npm-init | npm Docs
Nothing worked until when I initialized the project in a new directory and reinstalled all the dependent modules afresh.
Need to give full access to package-lock.json
If you are using linux do this
sudo eslint init

Gulp - Cannot find module './gulp/uild.js'

When I compile with gulp i got an error like below.
Operating system Windows 10
gulp serve
module.js:471
throw err;
^
Error: Cannot find module './gulp/uild.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 D:\test\gulpfile.js:13:3
at Array.map (native)
at Object.<anonymous> (D:\test\gulpfile.js:12:4)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
Could we see your gulpfile.js file?
Inside, it must have something like this:
var gulp = require('gulp');
gulp.task('default', function() {
// place code for your default task here
});
Another option is to run npm install.
Taken from the docs:
npm install (in package directory, no arguments):
Install the dependencies in the local node_modules folder.
In global mode (ie, with -g or --global appended to the command), it installs the current package context (ie, the current working directory) as a global package.
By default, npm install will install all modules listed as dependencies in package.json.
With the --production flag (or when the NODE_ENV environment variable is set to production), npm will not install modules listed in devDependencies.
Looks like you are missing b letter in build in ./gulp/uild.js in D:\test\gulpfile.js at line 13

module.js:327 Error: Cannot find module 'express'

Hello I am new in a nodejs and when I run any file in command prompt like:-
C:\demoData>node demo.js
I get error li9ke this
module.js:327
throw err;
^
Error: Cannot find module 'express'
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (C:\demoData\shahzad.js:1:77)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
I did R&D and get the solution
npm install express
And my Problem has solved using this. But Why I required this I have already express folder globally.this path
C:\Users\broswire\AppData\Roaming\npm\node_modules;
I want to use this node_modules, I don't want locally node_modules.
Node encourages using locally installed modules. Generally, using modules installed with npm install --global or npm install -g should be reserved for cli tools and things used systemwide. So to use express in a project, it is recommended to npm install express in that directory. It is also useful to add a package.json file to save the versions of your dependencies. Then you can just navigate to your project and run npm install and it will install the required dependencies.

Cannot find module 'iap_verifier'

I`m installing this server - https://github.com/surespot/web-server. I installed node.js, npm, CoffeScript and all dependencies
apt-get install nodejs npm
npm install -g coffee-script#1.6.3
npm install
When I execute command coffee server/cluster.coffee, I get the error:
Error: Cannot find module 'iap_verifier'
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> (/var/www/web-server/server/cluster.coffee:35:15, <js>:64:17)
at Object.<anonymous> (/var/www/web-server/server/cluster.coffee:1:1, <js>:4482:4)
at Module._compile (module.js:456:26)
but module files exist in node_modules dir.
I also try manualy install module
npm install iap_verifier
and
npm install iap_verifier -g
Make sure you required the module in the main app.js file and try running sudo npm install iap_verifier -g

zepto module in nodejs not found

I am a newbie with nodejs.
I installed globally modules zepto and jsdom :
npm install jsdom -g
npm install zepto -g
I checked with npm list -g. These modules are installed.
But when I reference these two modules in a script using require:
var jsdom = require("jsdom");
var Zepto = require("zepto");
I got an error message :
Error: Cannot find module 'zepto'
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/user554/dev/exchange/bin/getExchangeData.js:6:13)
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 Function.Module.runMain (module.js:497:10)
I tried with only jsdom and only zepto. Only Zepto fails.
What am I missing ? Thank you for your help.
The zepto package is not a proper Node package. It installs a minified version of the Zepto lib, but it looks like it's meant for browser usage (why would it be in the NPM repository? I have no idea).
Instead, try the zepto-node package.
The real problem here is that you're installing globally. Installing with -g is meant only for modules that provide some kind of command-line scripts (e.g. express-generator provides the express command that you can use from your shell prompt).
Generally you install the module without -g and it will get installed locally and you can require() it just fine. However, as pointed out by #robertklep, the zepto module currently does not export anything (missing "lib" in package.json), so installing locally would not help for that particular module.

Categories

Resources