Node.js - cannot find module - node.js

I'm using Node Boilerplate and it all worked fine until I decided create another project on top of it(in another dir).
Now I have exactly the same code base(this project AS IS) in two different folders. I can run one of it without any problems but another one is failing with:
% node app.js
node.js:116
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Cannot find module 'connect/middleware/router'
at Function._resolveFilename (module.js:299:11)
at Function._load (module.js:245:25)
at require (module.js:327:19)
at Object.<anonymous> (/home/gotts/Sites/nodejs-uploadr/lib/express/lib/express/server.js:17:14)
at Module._compile (module.js:383:26)
at Object..js (module.js:389:10)
at Module.load (module.js:315:31)
at Function._load (module.js:276:12)
at require (module.js:327:19)
at Object.<anonymous> (/home/gotts/Sites/nodejs-uploadr/lib/express/lib/express/index.js:28:31)
There is similar problem explained here - https://github.com/visionmedia/express/issues/535 which says that the problem is in the incompatible version of connect/express.
But how is this possible? Source code is exactly the same in two folders and it work fine in one copy and fails in another?

Do the two folders share a common parent? Node looks for modules in ~/.node_modules and /node_modules in your app dir.

OK, let me answer my own question:
Sorry for misinformation - those two folders were not exactly the same(I'm gonna need to learn unix diff better). And there it require("connect") in code
Local checkout of connect into /lib folder takes precedence but if it's not there - connect will be loaded from the ~/.node_modules as Ricardo previously set.

#see node.js express module not loading properly in the last comment, maybe it helps
the basic idea behind it is to install the module inside your application folder, not on a system level

Related

Can't run script of MongoDB Atlas URL using node to database

I'm currently following along with a MDN tutorial Express Local Library Part 3. I've done everything correctly, that I know of, so far. I made a database then a cluster with MongoDB Atlas and am now trying to upload my data from a file populatedb.js by command node populatedb <my MongoDB Atlas URL here> but it doesn't work at all. I've linked it to the apps.js file with the correct URL (or so I think).
I'm getting this returned in the console:
___________-MacBook-Air express-locallibrary-tutorial % node populatedb 'mongodb+srv://_________:_______________#cluster0.mhzo6.mongodb.net/local_library?retryWrites=true&w=majority'
internal/modules/cjs/loader.js:905
throw err;
^
Error: Cannot find module '/Users/________/Desktop/express-locallibrary-tutorial/populatedb'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
at Function.Module._load (internal/modules/cjs/loader.js:746:27)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
I've even deleted my database, cluster, organization and everything and remade them exactly the same but still to no avail.
Here is the answer:
[for those of you stumped on this tutorial this will most likely be the cause!]
The tutorial made it very easy to misunderstand and create a secondary folder of the same name as the root in the root...[or perhaps and more likely its that we are still beginners and its very easy to misunderstand and make this simple mistake.] In creating the initial express project https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/skeleton_website at tutorial 2 "creating the project" I had accidentally made the simple mistake of creating my own express-locallibrary-tutorial folder/project in VS code then ran all of the cmds from the integrated terminal instead of the external terminal. This is what caused the double folder.
I was easily able to work around this just by making sure I was in the correct folder when running node/nodemon and made sure to path correctly for all routes just to "cover up" the mistake which I believed was no big deal... This caused me to have to trash my entire project. I had actually finished this tutorial but once it came to launching it to Heroku I quickly realized that it wasn't worth it to attempt to fix everything and best to restart it from the beginning because Heroku can only run scripts from the /root/ as it should [at least so far since this beginner/noob has been troubleshooting that is!]
So watch out for that simple mistake all! If you did it too and caught it early it is definitely worth the headache of restarting because it will help you in the long run and is a good lesson of a simple mistake, or it is for me!!

proxies not supported on this platform

i'm trying to make(i don't know what it's called, hot load? hot reload?) Meteor-like real-time loading of data, but by using node.js not Meteor.
and i'm using the ddp module for the client(=browser, i have not tried it yet) and ddp-reactive-server, well, for the server.
server.js is like this:
var DDPServer = require('ddp-server-reactive');
var server = new DDPServer();
var todoList = server.publish('todolist');
after that i run the server using the command node server.js --harmony_proxies(notice i'm already using the flag) this is what i get:
[aseds#localhost ~]$ node server.js --harmony_proxies
/home/aseds/Desktop/projeh/css-goodness/node_modules/harmony-reflect/reflect.js:2049
throw new Error("proxies not supported on this platform. On v8/node/iojs, make sure to pass the --harmony_proxies flag");
^
Error: proxies not supported on this platform. On v8/node/iojs, make sure to pass the --harmony_proxies flag
at global.Proxy (/home/aseds/Desktop/projeh/css-goodness/node_modules/harmony-reflect/reflect.js:2049:13)
at publish (/home/aseds/Desktop/projeh/css-goodness/node_modules/ddp-server-reactive/lib.js:211:32)
at Object.<anonymous> (/home/aseds/Desktop/projeh/css-goodness/ddpserver.js:10:23)
at Module._compile (module.js:397:26)
at Object.Module._extensions..js (module.js:404:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:429:10)
at startup (node.js:139:18)
at node.js:999:3
my nodejs version v5.4.1.
i'm not even sure if that's actually possible to make the automatic reload feature of Meteor this way but i'm trying! :)
Thanks in advance for any help you are able to provide.
I came across this thread with regards to --harmony-proxies:
https://github.com/tvcutsem/harmony-reflect/issues/56
The relevant bit:
I released version 1.4.0 which, when loaded twice according to the script outlined above, loads correctly.
Note that loading v1.3.1 of this library followed by v1.4.0 will still fail (the other way around works fine). So it's important that your dependencies upgrade to the latest version.
It appears that if harmony-proxies is loaded as a node dependency twice, with different versions required, and a version before 1.4.0 is loaded first, then you will see this error.

is it possible to set up amdefine in tests so that I don't have to define it in all my module files?

I have a set of objects that are used browser side but tested server side with mocha. I'm using require.js for AMD loading. The Require.js site suggests using amdefine on server-side to get the defined modules to work in node.js with this bit of code:
if (typeof define !== 'function') {
var define = require('amdefine')(module)
}
OK. But I have to put that into every module that I want to use in Node. In my case that means I have to strip it out of any code that I'm using client side (most of it).
I'm wondering if there's any way to put that chunk of code in my test instead so that I don't have to put it in my client side code. It seems silly to have code in my files that will only be needed for the tests -- makes more sense to put it in the test code. However, when I do that I get an error:
Error: amdefine with no module ID cannot be called more than once per file.
at runFactory (/home/vmplanet/dev/alpha/web/node_modules/amdefine/amdefine.js:159:23)
at define (/home/vmplanet/dev/alpha/web/node_modules/amdefine/amdefine.js:275:13)
at Object.<anonymous> (/home/vmplanet/dev/alpha/web/assets/src/coffee/delta/dataLayer.coffee:4:3)
at Object.<anonymous> (/home/vmplanet/dev/alpha/web/assets/src/coffee/delta/dataLayer.coffee:158:4)
at Module._compile (module.js:456:26)
at Object.loadFile (/usr/lib/node_modules/coffee-script/lib/coffee-script/coffee-script.js:179:19)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
It's an odd error, since the amdefine code is only in one place -- the top of the test file. Is there a way to put this amdefine code in my test and only my test and still get the tests to run server side -- without having to strip out the amdefine code for client side?
If you use amd-loader, you can do this:
require("amd-loader");
var datatypes = require("../build/dist/lib/salve/datatypes");
var name_resolver = require("../build/dist/lib/salve/name_resolver");
That's it. You just require amd-loader first and then you can load AMD-style modules at will. (In the example above the two modules loaded after amd-loader are AMD-style modules.) And the AMD-style modules can load other AMD-style modules.
The snippet above is actual code from one of my test suites which tests a library designed AMD-style so that it can be loaded with RequireJS but is tested in Node.js.

Nodejs installation error /home/ubuntu/public_html/node_test.js'

I am new to nodejs. I managed to install node.js on amazon EC2 server using this tutorial http://iwearshorts.com/blog/how-to-install-node-js-on-your-server/
But when I run the code I get this error. anyone know why this is happening and what I need to solve this.
node.js:197
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Cannot find module '/home/ubuntu/public_html/node_test.js'
at Function._resolveFilename (module.js:332:15)
at Function._load (module.js:279:25)
at Array.0 (module.js:481:10)
at EventEmitter._tickCallback (node.js:188:41)
You should post your primary .js file that you are running in node so we can find your exact problem.
My guess is you are passing the wrong value to require. Pass a relative file path without the filename extension like this: var node_test = require("./public_html/node_test");

NODE_PATH is being ignored or not working

I'm trying to run my node app on a new server and am having some issues with the NODE_PATH environment variable. The app works fine on my local machine (OSX Lion) but not on the server (Redhat linux). When starting my app with node app.js from within my project directory /development/nodeproject, I get the following error :
Error: Cannot find module 'mod/core/models/Category'
at Function._resolveFilename (module.js:334:11)
at Function._load (module.js:279:25)
at Module.require (module.js:357:17)
at require (module.js:368:17)
at /development/nodeproject/app.js:57:5
at Object.<anonymous> (/development/nodeproject/app.js:258:1)
at Module._compile (module.js:432:26)
at Object..js (module.js:450:10)
at Module.load (module.js:351:31)
at Function._load (module.js:310:12)
mod/core/models/Category is the first require() in my app.js and looks like this: var Category = require('mod/core/models/Category'). So apparently node is not looking for modules in my project directory.
I'm not sure why though, because I made the following changes (that are working just fine on my local machine).
added export NODE_PATH=/development/nodeproject to my ~/.bash_profile
ran source ~/.bash_profile
if I run env I see NODE_PATH=/development/nodeproject listed there
in my app.js if I console log process.env.NODE_PATH I get /development/framework (should this output an array instead of a string?)
Other information that might be relevant:
I'm on node v0.6.7
I'm doing all of this as root (sudo su -)
At this point I'm not sure what else I can do. Any help would be greatly appreciated.
NODE_PATH used for modules, not for solutions files.
Try
module.paths.push("/development/nodeproject", "one/more/path");
before any require() call. And you really should use a relative require like require('./mod/core/models/Category') for files in your nodeproject directory
The functionality you are looking for was removed. Use the node_modules directory or a relative require like require('./mod/core/models/Category').
This answer has more info: NODE_PATH error with node.js while attempting to setup jsctags for vim

Resources