node.js require cannot find custom module - javascript

Here is the project structure:
/
app.js
package.json
/node_modules
/app
config.json
/frontend
assets and html tpls
/modules
couch.js
raeume.js
users.js
I require config.json, raeume.js and users.js from app.js and it all works fine.
var config = require('./app/config');
var raeume = require('./app/modules/raeume');
var users = require('./app/modules/users');
Then I require config.json and couch.js from user.js the same way and it won't find anything.
var couch = require('./app/modules/couch');
var config = require('./app/config');
I guess it should find it. After some research I saw a diverse landscape of problems inclusive how node is compiled. Thus included: I work on osx 10.8 with node v0.10.7.

The path is relative to the directory in which you are requireing the files, so it should be something like:
var couch = require('./couch');
var config = require('../config');
A bit of clarification, if you write
var couch = require('./couch');
you are trying to require the couch module which resides in the current directory, if you write
var couch = require('couch');
you are trying to require the couch module installed via npm.

The current accepted answer is correct and properly answers the question.
Although not directly related to the original question, I'd like to add another point here for all those people who got the Cannot find module error for local files although the correct relative path was specified.
Assuming a file with the name couch.js exists in the current directory,
On case insensitive filesystems (like NTFS on Windows), both these lines will work -
var couch = require('./couch');
var couch = require('./Couch');
However, on a case-sensitive filesystem (like ext4 on Linux), require('./couch') will work but require('./Couch') will not.
There is a page in the NodeJS docs regarding this.
I hope this helps someone whose perfectly working code stopped working after moving from Windows/Mac to Linux. 😅

Here is how you do it :
var users = require('./../modules/users');

It must be:
var config = require(../../app/config)
var couch = require(./couch) (same directory)

Also double check the extension on the file being imported. I accidentally used .ts in a JavaScript project, and this prevented it from being found. Once I changed the extension to .js, require was able to find it fine.

Related

Using require when electron app is packaged in an asar

In my main.js, I have:
var mainFrm = require('./MainFrm');
This works fine except when the application is packaged as an asar file. I get a 'Cannot find module' error.
The documentation states to use the following:
require('/path/to/example.asar/dir/module.js');
I tried that but got the same error. Where does the path begin when using the above? Does path start with electron.exe is? Also, if I use require('/resources/app.asar/MainFrm.js') what path do I use for OS X apps since the Resources folder is in a different location? What path should I use during development/debugging (i.e. not inside of an asar)
I think you may have 2 issues. First, you may need to be explicit about the file extension, thus looking for MainFrm.js, not just MainFrm. Second, try using resolve to determine the name relative to the current directory.
One way to resolve this is to resolve the path at runtime, like this:
var mainFrm = require("path").resolve(__dirname, "./MainFrm.js");
Try some combinations of that and see if it doesn't help.

I'd like to reuse code from another module in my Nodejs backend

I'm using the Yeoman Generator Angular Fullstack and I'd like to reuse JS code from different directories within my server directory. I'm referencing the file that has the functions I want like:
var something = require('/.path');
I get the error: "Cannot find module" in my terminal.
I tried a number of variations of the path working through all of the levels from server level down to file level where the given functions are contained. I looked at a few tutorials:
http://www.sitepoint.com/understanding-module-exports-exports-node-js/
AND
https://www.launchacademy.com/codecabulary/learn-javascript/node/modules
I clearly missed something. Each module of my nodejs has a controller with an exports.create function. All of my code for each module is contained within my exports.create function accept for other required modules. I have no problem requiring underscore or other libraries in my Node/Bower modules by the way.
To be as detailed as can be, I expected
var something = require('./directory/directory.controller.js');
var something = require('/.path');
The path you are using is likely incorrect. Probably you want to open the file called path.js contained in the same folder of the file from which you are importing it. In order to do that, you should change the import as follows :
var something = require('./path');
./path is a relative path where . stands for current directory.
/.path is an absolute path. In this case require is importing a hidden file in the root directory. I guess is not what you want.

Is it possible to seek file list in a certain public-directory from HTML/javascript?

I plan to implement module component system (plugin) for node.js client side.
For instance(simplified),
/www/index.html
index.js
modules/moduleA/module.js
some.html
moduleB/....
.....
It's surely possible to hardcode the list of module dir-names, and require (with browserify) components, but I would rather point them automatically depending on the modules directory structure.
In node.js, I do this easily as
var modules;
fs.readdir('./www/modules', function(err, modulesDir)
{
var modules = [];
modulesDir.map(function(modulename)
{
modules[modulename] = require('./www/modules/' + modulename + '/module');
});
}
However, in the client side, even with browserify, since fs is basically not supported in the browser side, the same manner is not possible as far as I tried.
How can we achieve to read through directory names under a certain directory path?
Thanks.
PS. I just found
https://github.com/brianloveswords/filesystem-browserify
but it looks a bit old, if you have a good recommendation, please let me know.

Requiring/running JavaScript outside of node-webkit application

Suppose I have the following app structure:
outer-folder/
├── my-app/
└── settings.js
where my-app/ is either the unbuilt directory that contains package.json or the packaged application my-app.exe or my-app.app.
I cannot package settings.js with the rest of my app, because I want this file to be editable by users, and loaded upon startup to configure my app. Moreover, I plan to allow settings.js to reside anywhere (for example, in the user's home directory) and be loadable from there.
This works fine when the app is unbuilt. I just have a relative path to the file and require() it like usual. But when the app is built (with grunt-node-webkit-builder if that makes a difference) this fails and I get the dreaded "Cannot find module" error.
I've searched the node-webkit wiki but can't find anything about this issue, am I missing something? What is the best way to load and run an external JavaScript file, like one would do with require(), from a packaged node-webkit app?
I suggest you to use the application data path.
See the documentation here
An exemple
var gui = require('nw.gui');
var path = require('path');
var yaml = require('js-yaml');
var fs = require('fs');
var confPath = path.join(gui.App.dataPath, 'conf', "dev-conf.yml");
try {
conf = yaml.load(fs.readFileSync(confPath, 'utf-8'));
} catch (err) {
throw new Error("Cannot read or parse configuration file '"+confPath+"': "+err);
}
It's a good pratice to separate code and configuration, and App.dataPath aims at the application specific folder in user's application data (different for each OS).
I generally use an installer to copy my configuration file into it.
Next tip: I prefer using YAML instead of JSON for my configuration or settings, because it allows you to insert comments inside the file.

Why can't my Titanium build app find a module even though it's clearly there?

I'm trying to use the sculejs module that I downloaded with GitHub. It seems like the com.scule.js-file should be placed in the same folder as app.js, according to the provided example. But the most recent versions of Titanium (I think) don't use that structure.
Here is what I add to the index.js-file located under app/controllers:
var scule = require('com.scule.min');
This results in the emulator giving me "Application Error Couldn't find module: com.scule.min."
Here is what my app's folder system looks like:
Anyone know what's wrong? Or maybe can push me in the right direction to get it to work?
Thanks
There should only be controllers in the controllers directory. So you should remove all but index.js.
Next, create a lib directory and put your commonjs libraries there. Also, remove com.scule.min.js from the app directory.
It's important to use the correct directory structure in Alloy. Otherwise, your files will be ignored.
Use This:
var scule = require('/controllers/com.scule.min');
This may be a little late for your needs - but I thought I'll just put an answer here anyway for others to see.
It seems you are using "Alloy" with Titanium - that is good! I am relatively new to Titanium but I am quite sure Alloy is the way to build apps in Titanium.
Within Alloy you need to put all your CommonJS libraries in a "lib" folder which is at the same level as controllers, views and styles. Create the folder if it does not already exist.
Once you have put your com.scule.js (which I use) - or com.scule.min.js in the "lib" folder then you can easily include it in the other modules (e.g. your controller or another of your own libraries in the lib folder). Just write:
var scule = require('com.scule') // Basically, the file name without the last ".js"
I don't know if your problem could be related to 'com.scule.min.js'? I haven't tried that - but I use 'com.scule.js' in my current app ;-)
/John

Categories

Resources