Cannot find module in Nodejs - node.js

module.js:340
throw err;
^
Error: Cannot find module './models/todo'
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> (C:\Users\Basel\Desktop\Todo List\routes\api.js:1:74)
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)
C:\Users\Basel\Desktop\Todo List>
Why this application won't start up? I've already tried a global npm install.

In ./models/todo, the period indicates that node will look in the same folder that api.js is in, which would look for \Todo List\routes\models\todo.js. This does not start from the root of the application. To require this, you'll need to us two periods to jump up a level, and specify the app path as well:
var todo = require('../app/models/todo');

maybe you did not set the system value : NODE_PATH; it should point to your global module location;
in Linux: export NODE_PATH=/usr/local/lib/node_modules/ works good for me;

in my case, the file name i had given in my require statement was wrong. I had my models file named posts.js and i was using require('./models/post'). It worked after i changed it to require ('.models/posts')

Related

Cannot find module 'm3u8stream/lib/parse-time'

is there anyway to fix this? I've been trying to resolve this myself but I give up
throw err;
^
Error: Cannot find module 'm3u8stream/lib/parse-time'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)
at Function.Module._load (internal/modules/cjs/loader.js:508:25)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (/rbd/pnpm-volume/71b5ddd8-2a78-46fc-aba4-68049f14880d/node_modules/.registry.npmjs.org/ytdl-core/1.0.0/node_modules/ytdl-core/lib/info-extras.js:5:21)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
This works for me, at the root of the project:
cp -r ./node_modules/m3u8stream/dist ./node_modules/m3u8stream/lib
I really don't understand why, I assume is some bug of versions, (mentioned in the previous comments) cause for some reason it needs the both folders dist and lib (with the same content apparently)
Solved by: Chills
Comment: did you install parse-time?
Answer: no I didn't, I did get it installed but also I've learned from a friend about ytdl-core update required dist instead of lib
Source: https://github.com/fent/node-ytdl-core/pull/520
Solution:
packages needed installing: m3u8stream and parse-time
code needed in main file:
const m3u8stream = require('m3u8stream');
const parseTime = require('m3u8stream/dist/parse-time');

Custom dependency require Electron

With the package.json, it's possible to do something like this
"dependencies": {
"WindowManager": "./lib/WindowManager"
},
But when trying to do this in Electron, it gives me this error:
App threw an error during load
Error: Cannot find module 'WindowManager'
at Module._resolveFilename (module.js:470:15)
at Function.Module._resolveFilename (.../desktop/node_modules/electron/dist/Electron.app/Contents/Resources/electron.asar/common/reset-search-paths.js:35:12)
at Function.Module._load (module.js:418:25)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (.../desktop/lib/init.js:15:35)
at Object.<anonymous> (.../desktop/lib/init.js:22:3)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
Is it because of Electron, or am I doing something wrong?
./lib/WindowManager is a folder with a package.json in it with a main pointing to the WindowManager.js, so that should work.
The case you try to do normally works, make sure that the dependency "WindowManager" has been properly copied by the build tools to either:
.../desktop/node_modules/electron/dist/Electron.app/Contents/Resources/app.asar/node_modules
or
.../desktop/node_modules/electron/dist/Electron.app/Contents/Resources/node_modules
If WindowManager does not exist in the disted node_modules there is something wrong with your build process, hard to be more precise than that.

problems with node paths in prod env

I have my first node server app running fine on localhost, but in a digitalocean (ubuntu) environment I can't get the paths to work.
This line
require("./lib/dbHelper").initilize();
Generate this error
Error: Cannot find module './lib/dbHelper'
at Function.Module._resolveFilename (module.js:339:15)
at Function.Module._load (module.js:290:25)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at Object.<anonymous> (/var/www/wsApp/app.js:18:1)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
And this line
let User = require("../models/User");
generates this error
Error: Cannot find module '../models/User'
at Function.Module._resolveFilename (module.js:339:15)
at Function.Module._load (module.js:290:25)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at Object.<anonymous> (/var/www/wsApp/routes/auth.js:5:12)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
I have looked here (https://gist.github.com/branneman/8048520) for solutions and I have tried suggestions 3, 4.2 and 6 but nothing changes the error messages.
Worth noting is that this line seems to work fine
app.use("/", require("./routes/auth.js"));
Being new to node I don't quite understand the path mappings, and it seems strange that this last one would work while the first one doesn't since they both use the dot syntax...
If you are developing on a case-insensitive filesystem (OS X or Windows), you may be getting tripped up by the fact that the linux filesystem is case sensitive. Check, for example, that the "H" in "dbHelper" is really uppercase in your git repository.

Cannot find module (a custom module)

I've following folder structure.
I am trying to access my custom module (core_programming/Constants.js) in other files.
I can access it in routes/index.js without any issue using following code.
var Constants = require('../core_programming/Constants.js');
But I am getting error when I try to access it inside core_programming/User.js with following statement.
var Constants = require('Constants.js');
It gives following error:
module.js:338
throw err;
^
Error: Cannot find module 'Constants.js'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:286:25)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (D:\nodeJsProjects\AutomateBuild\core_programming\User.js:3:18)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
1 Oct 11:56:35 - [nodemon] app crashed - waiting for file changes before starting...
I've tried different ways for defining path in require like ../core_programming/Constants.js and ./core_programming/Constants.js but nothing works out.
What is the correct way for loading custom modules from the same directory.
And, I am on Windows if that helps.
Try to use:
var Constants = require('./Constants.js');
This will force Node to figure out you are looking for a relative path and not a package in node_modules.
On a side note, windows paths use \, so consider trying it as well:
var Constants = require('.\Constants.js');

node.js cannot find a module in the same folder

Im trying to use a simple "colors" module to set cli-colors in my logs, nothing special.
Well, i have a module called colors.js in the path ./app/config/colors.js, the content:
var clc = require('cli-color');
var colors = {
ok: clc.cyan,
error: clc.red.bold,
warn: clc.yellowBright,
high: clc.white.bgGreen
};
module.exports = colors;
Simple. Well, when i require it in the server.js (at the root of the project, above of /app) it works fine, but, when i try to use it in the ./app/config/db.js it throws me an error:
Error: Cannot find module './app/config/colors.js'
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/nano/Dev/bears-api/app/config/db.js:3:14)
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)
14 Sep 10:21:00 - [nodemon] app crashed - waiting for file changes before starting...
Why if it works in the server.js?
You probably required the module using a relative path.
Relative paths are resolved in relation to the requiring module's location.
Quoting docs
A module prefixed with './' is relative to the file calling require().
That is, circle.js must be in the same directory as foo.js for
require('./circle') to find it.
So if you did a
var whatever = require('./app/config/colors.js');
inside a module located in ./app/config/ then node will look for ./app/config/app/config/colors.js and fail.
If both requiring and required module are in the same directory just use:
var whatever = require('./colors.js');
or even shorter:
var whatever = require('./colors');
The module should be in the "node_modules" folder to access it like you have described.

Resources