NPM Passport Strategy LINKED Cannot find module - node.js

I have created a custom private passport strategy in a separate module called passport-x
I did
npm link .
in that project directory
and
npm link passport-x
in the consuming project.
In my passport.js file, I have
var LocalStrategy = require('passport-local').Strategy;
var XStrategy = require('passport-X').Strategy;
..
module.exports = function(passport) {
passport.use('local-signup', new LocalStrategy({
....
passport.use('x-login', new XStrategy({
....
However, I am getting this error
Error: Cannot find module 'passport-x'
despite the fact I can see passport-x in my node_modules in the consuming project.
Anything perhaps I am not aware about when using more than 1 strategy at the same time or using custom strategies?

The problem was to do with the structure of the lib folder I was importing...
I had used
lib/
moduleX/
index.js
strategy.js
when it was looking for an index.js in the lib directory itself:
lib/
index.js
strategy.js
The presence of an index.js file allows you to do this in the consuming code:
var XStrategy = require('passport-X').Strategy;

Related

Cannot find module '…/models/user' nodejs

I just found a cool project that I want to clone and explore, but I getting this weird error.
I tried to read some similar solution here but couldn't find any.
after executing npm start, i getting this error.
You can clone the project from here.
https://github.com/didinj/mern-stack-authentication
I have installed as described.
From the Err message:
Error: Cannot find module '../models/user'
It simply cannot resolved the path to User.js file.
In your auth.js and passport.js file rename or change this import line of code
var User = require('../models/user');
to
var User = require('../models/User');
I notice the file name is User.js not user.js
First of all you need to execute
npm install
This will install all of project's dependencies.
Your file name is User not user, so please update auth.js and replace
var User = require("../models/user");
with
var User = require("../models/User");
It is not able to find the module named ../models/user because your file name is User.js not 'user.js' inside Models folder.
you can either change the name of the file from 'User.js' to 'user.js'
or
you can change the require('../models/user') to require('../models/User') in your 2 files- auth.js and passport.js

How to get the root of project which installed my npm module?

I am developing an NPM package. Now I want to know the easiest way to get the root of the project which utilizes my custom package.
Within my package itself it is pretty easy to get the root in node.js like so:
__dirname
This points to the root of my package itself though. I would like to get the root of the project which actually performed the npm install of my package though.
Obviously, I could simply go up from the node_modules directory and assume that the root directory is there but this sounds really bad.
Is there a standard way to achieve this?
you could use app-root-path which is an npm module and its pretty neat.
npm i -S app-root-path
after that just do
var reqlib = require('app-root-path').require;
var myModule = reqlib('/lib/my-module.js');
You can use the info from: require.main
The Module object representing the entry script loaded when the
Node.js process launched. See "Accessing the main module".
const path = require('path');
console.log(path.dirname(require.main.filename));
Having this folder structure:
/app
entry.js
node_modules/
my-package/
index.js
app/entry.js
const myPackage = require('my-package');
app/node_modules/my-package/index.js
const path = require('path');
console.log(path.dirname(require.main.filename));
And when you call: node entry.js
You will get: /app printed to stdout.
KISS
const fullPath = path.dirname(require.main.filename);
const regexResp = /^(.*?)node_modules/.exec(fullPath);
const appRoot = regexResp ? regexResp[1] : fullPath;
console.log(appRoot); // outputs the directory which is the root of this project

Node.JS Express - Importing a folder As A Module

I am new in express.
What I am trying to do is import a folder as a module so that in future I can use other files in the folder can be used as modules.
So, what I have done is change this-
var routes = require('./routes/index');
var users = require('./routes/users');
app.use('/', routes);
app.use('/users', users);
to this-
var routes = require('./routes');
app.use('/', routes.index);
app.use('/users', routes.users);
But I am getting error like this-
Can anyone please help, what can I do to solve this issue so that I can use all files inside route folder as modules?
Update-
Regarding to #AnubhavSrivastava , my requirement is OK because I have seen this in MVA tutorial. Link is here and time is 1:32:58.
Code is like this-
Thanks in advance for helping.
When you do require over a folder, it returns index.js module by default, you wont get route.user, unless you have a variable that is exported in index.js.
Extract from node js official documentation here - https://nodejs.org/api/modules.html#modules_folders_as_modules
Folders as Modules
It is convenient to organize programs and libraries into self-contained directories, and then provide a single entry point to that library. There are three ways in which a folder may be passed to require() as an argument.
The first is to create a package.json file in the root of the folder, which specifies a main module. An example package.json file might look like this:
{ "name" : "some-library",
"main" : "./lib/some-library.js" }
If this was in a folder at ./some-library, then require('./some-library') would attempt to load ./some-library/lib/some-library.js.
This is the extent of Node.js's awareness of package.json files.
Note: If the file specified by the "main" entry of package.json is missing and can not be resolved, Node.js will report the entire module as missing with the default error:
Error: Cannot find module 'some-library'
If there is no package.json file present in the directory, then Node.js will attempt to load an index.js or index.node file out of that directory. For example, if there was no package.json file in the above example, then require('./some-library') would attempt to load:
./some-library/index.js
./some-library/index.node

Nodejs "requiring" files from directory as modules . Confused about file-path prefix

I am trying to require some files into another file as modules . As a neophyte to the NodeJS I am somewhat perplexed as I thought require statements could only call upon modules defined in the modules directory !
Home>src>middleware> {:current file}
var UserModel = require('src/models/user');//`Home>src>models>user.js`
var userDAO = require('src/dao/user');//`Home>src>dao>user.js`
I have downloaded a "bootstrapped" version of HapiJS and having trouble getting started . Github '/sample-hapi-rest-api/' . I am not very experienced with NodeJS.
One way to require files is to use relative pathing. If your current file is in Home>src>middleware> then you want
var UserModel = require('../models/user');//`Home>src>models>user.js`
var userDAO = require('../dao/user');//`Home>src>dao>user.js`

Require dependency of another dependency in node modules

I've got a simple node app that has single dependency on another app on github. The dependency installs just fine with npm install, but when I try to require something installed there, it says it's not available. For example, the github app installs Mongoose as a dependency. I thought that this parent app would be able to access that module since it is in a child:
var mongoose = require('mongoose')
The structure looks something like this:
/app
/node_modules
/github_dependency [parent module]
/node_modules
/mongoose [child module]
Do I just have to include mongoose as a dependency as well in the parent app or is there a way of getting access to that module by way of the child?
Do I just have to include mongoose as a dependency as well in the parent app or is there a way of getting access to that module by way of the child?
While it's possible for you to e.g. require('github/node_modules/mongoose'), the standard practice is to install all of your dependencies explicitly (i.e., you should include mongoose as a dependency of your app) and require('mongoose').
For a more robust case, which is good in situations such as testing, you can use the following function:
var Module = require('module');
var path = require('path');
function requireFrom(self, parent, name) {
var pPath = Module._resolveFilename(parent, self);
var m = new Module(pPath, module);
m.filename = pPath;
m.paths = Module._nodeModulePaths(path.dirname(pPath));
return m.require(name);
}
which can be used as follows
requireFrom(module, 'github_dependency', 'mongoose')

Resources