Problems with running node module - node.js

So, I am getting the infamous error:
module.js:340
throw err;
^
Error: Cannot find module '/home/bc/projects/nodejs/pg-connect.js'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
here is the 2 line source causing it:
var pg = require('pg');
console.log("Hello world!");
Before asking for help here I have tried several things all of which didn't work:
1)I tried setting it to the absolute path, in my case:
var pg = require(/home/bc/node_modules/pg);
2)I tried copy/paste the above directory into my project directory.
3) I tried to install the module locally using:
npm install pg
However instead of the current directy it installed in the path /hone/bc/node_modules
I have no idea what more I can try to get it to work.

As I mention in my comment, it appears that you've successfully installed the module, but in the wrong place.
A quick fix would be to copy the contents of HOMEDIR/node_modules into CURRENTDIR/node_modules.
Then you should be able to
var pg=require('pg');
To find "require-able" modules, Node will check the current directory for its module directory, unsuprisingly named "node_modules", and if it cannot find the module there, Node will move back up the directory path until it either finds a node_modules directory containing the required module or it hits the root. So, assuming your current directory is /home/bc/projects/nodejs, Node will look for the pg module in the following directories (in order):
/home/bc/projects/nodejs/node_modules/pg
/home/bc/projects/node_modules/pg
/home/bc/node_modules/pg
/home/node_modules/pg
/node_modules/pg

Related

New to node.js found a possible PATH variable error

Fairly new to node.js and suddenly this error happened, I tried researching it and found out it could be PATH, but I tried adding it and could not find the solution.
C:\Windows\system32>node app.js --harmony
internal/modules/cjs/loader.js:584
throw err;
^
Error: Cannot find module 'C:\Windows\system32\app.js'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)
at Function.Module._load (internal/modules/cjs/loader.js:508:25)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
I have no idea why your current working directory is C:\Windows\system32 but you really shouldn't be in there running Node code. That's for Windows specific system files only.
You'll need to change to the directory with app.js in it, or you'll need to specify the full path when running Node:
node C:\path\to\app.js --harmony
Like all command-line tools, the node program will look in the current directory unless told otherwise.

Cannot find module app.js?

I am starting node app.js in the server directory, and I get the following:
module.js:544
throw err;
^
Error: Cannot find module 'server/server/app.js'
at Function.Module._resolveFilename (module.js:542:15)
at Function.Module._load (module.js:472:25)
at Function.Module.runMain (module.js:682:10)
at startup (bootstrap_node.js:191:16)
at bootstrap_node.js:613:3
[nodemon] app crashed - waiting for file changes before starting...
How to fix this? It was working before, and now it's looking for a weird path /server/server instead of just /server.
I just reinstalled node to a more up-to-date version, from 5. to latest stable.
Most likely you try to load your module like so:
require('server/server/app.js')
Node.js then tries to find it somewhere below node_modules. But I suppose, the file is not a dependency but part of your own code. Normally, you would then require it like so (consider the point at the beginning):
require('./server/server/app.js')
This makes Node.js look for the file beginning at the current directory ('.').
This would work in an environment with the following folder structure:
/myfiles/main.js <-- this file contains the `require` statement
/myfiles/server/
/myfiles/server/server/
/myfiles/server/server/app.js <-- this file is being `require`d
I encountered with this issue because I deleted build folder and then try to build the project.
Solution - in my case
I run the tsc --watch that compiled all .ts files to .js
maybe you want to access file app.js,
require('./server/server/app.js')
// to be like this
require('server/server/app.js')
use can using this package
npm i app-module-path

Error: Cannot find module '.../webpack'

I'm just starting with node, and server stuff in general. I was following along with a well-reviewed youtube video and am at a loss here. I installed webpack and webpack-dev-server both globally and in my project folder. Now if I try to run webpack (typing nodejs webpack in the terminal) or webpack-dev-server, it just give me this error:
andrew#AndrewLaptop:~/Documents/mean-todo-app$ nodejs webpack
module.js:340
throw err;
^
Error: Cannot find module '/home/andrew/Documents/mean-todo-app/webpack'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
Shoudln't it be looking in mean-todo-app/node_modules/webpack instead of mean-todo-app/webpack?
Again, new to all of this so I could be missing something stupid, but I did search to no avail.
Thanks
edit: guess I could have mentioned I'm on Ubuntu if that matters
try using
npm install webpack
and do this in the root directory of your app.
var webpack = require('webpack');
you can use './webpack' only if you have something.js in the same directory
if you require modules installed from third party source like npm use only require('webpack');
Directly use webpack on your terminal. You don't need to refer nodejs here. Refer this documentation for executing webpack via command line as well as npm scripts: http://survivejs.com/webpack/developing-with-webpack/getting-started/#adding-a-build-shortcut
I had this problem as well. Struggled a few hours trying to figure this out. I'm on windows 10, so hopefully this will help you...
First, you need to run 2 separate processes. In Windows Power Shell, you run: node server
THEN... in another WPS, you run: webpack-dev-server
From Chrome, type in: localhost://8080
You don't want to run "node webpack-dev-server" in the second shell. That kept throwing me off.

Node.js appends command to current working directory and throws an error

I just installed Node.js on my Windows 7 machine, added it to the PATH and tried to use it. npm works perfectly (at least for now) and node --version prints v4.1.2.
However, when I try to execute another command like node update it appends the command to the current working directory and, of course, fails :
$ node help
module.js:338
throw err;
^
Error: Cannot find module 'c:\path\to\working\directory\help'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:286:25)
at Function.Module.runMain (module.js:475:10)
at startup (node.js:118:18)
at node.js:952:3
What can I do?

Sudo node server return error, OsX

There is the case:
mac:~ username$: sudo node server
module.js:340
throw err;
^
Error: Cannot find module '/Users/username/server'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
So here is 2 questions:
How to specify modules folder in node.js(node on my machine is taking place on /usr/local/bin/node); may be u could give me a advice how I may organise my project files.
How could I prevent this type of errors in future? What could I read about it?
I think you don't get what exactly Node will do with your command.
With this command, Node is trying to load a script file (in your current folder) with no extension named server.
It looks like this file doesn't exist in your current directory.
Let's take a simple example: you have a file named app.js in your current folder which contains a simple output in the console.
// In the app.js file
console.log("Output in the console by NodeJS");
Now, run the command node app.js. You should have the output in your console.
Talking about organisation, it depends of your workflow and projects. You could find lot of informations about this by reading the source code of popular projects on Github.

Resources