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.
Related
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
To start things off, please know that I'm incredibly new to the whole web development world, so if I say anything that doesn't make sense, please let me know. So I've been following some guide on how to develop a simple restful app, but I hit a wall when I tried using 'npm start' to set up (the first version of) the restful app. The error says that it can't locate "./bin/www" - which is fair enough, www (whatever that is) doesn't exist, and neither does the bin directory. I've set up the 'skeleton' using the express command, and I've done minor modifications but they have all been according to this tutorial, and I haven't touched the placed in the json file where it states the starting directory (and neither has the person in the tutorial). Any help would be incredibly appreciated.
omar#omar-S550CM:~/Desktop/Projects/WebPages/nodetest2$ ls -a
. .. app.js data node_modules package.json public routes views
omar#omar-S550CM:~/Desktop/Projects/WebPages/nodetest2$ npm start
> nodetest2b#0.0.0 start /home/omar/Desktop/Projects/WebPages/nodetest2
> node ./bin/www
module.js:340
throw err;
^
Error: Cannot find module '/home/omar/Desktop/Projects/WebPages/nodetest2/bin/www'
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
npm ERR! weird error 8
npm WARN This failure might be due to the use of legacy binary "node"
npm WARN For further explanations, please read
/usr/share/doc/nodejs/README.Debian
npm ERR! not ok code 0
Note: npm and nodejs seem to be working fine otherwise.
Ahhh....after looking into it for quite a bit, it seemed like my whole nodejs environment might be a bit botched; and it certainly was. I uninstalled all nodejs elements and re-installed everything using nvm (via this guide: https://www.digitalocean.com/community/tutorials/how-to-install-express-a-node-js-framework-and-set-up-socket-io-on-a-vps), and it works perfectly now.
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.
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
I made an application on my machine, and it works well. I uploaded it to the server, and it is crashing with the following error:
node.js:116
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Cannot find module 'xml2js'
at Function._resolveFilename (module.js:289:11)
at Function._load (module.js:241:25)
at require (module.js:317:19)
at Object.<anonymous> (/var/www/node/price/index.js:3:14)
at Module._compile (module.js:373:26)
at Object..js (module.js:379:10)
at Module.load (module.js:305:31)
at Function._load (module.js:271:10)
at Array.<anonymous> (module.js:392:10)
at EventEmitter._tickCallback (node.js:108:26)
This is how my app starts:
var express=require('express');
var http=require('http');
var xml2js = require('xml2js');
var sys = require('sys');
var util = require('util');
I have installed both express and xml2js using npm. I have the exact same version (v0.4.0) for node on my machine and my server.
I have made sure that the path wher xml2js and express reside (/usr/local/lib/node/) is included in the paths where node looks for modules. (I edited the file 'module.js' to print the paths where it is looking for modules.)
node.js:116
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Cannot find module 'xml2js', in paths: /root/.node_modules,/root/.node_libraries,/usr/local/lib/node,/var/www/node/price/node_modules,/var/www/node/node_modules,/var/www/node_modules,/var/node_modules,/node_modules
at Function._resolveFilename (module.js:289:11)
at Function._load (module.js:241:25)
at require (module.js:317:19)
at Object.<anonymous> (/var/www/node/price/index.js:3:14)
at Module._compile (module.js:373:26)
at Object..js (module.js:379:10)
at Module.load (module.js:305:31)
at Function._load (module.js:271:10)
at Array.<anonymous> (module.js:392:10)
at EventEmitter._tickCallback (node.js:108:26)
So what is wrong? I have the right path, the module is there. Why can't node find it? And the exact same code runs smoothly on my local machine. If it matters, my machine is a Mac, and the server runs CentOS.
require.paths.push('/usr/local/lib/node_modules');
is no longer valid for node v0.8.1 and above. Instead of using require.paths.push, you can set the environment variable NODE_PATH
export NODE_PATH=/usr/local/lib/node_modules
or if you install npm modules in your home directory, then
export NODE_PATH=~/.npm
As spmason mentioned, Node changed how modules are resolved. I've had the same issue as you and resolved it by installing all modules globally (--global) and adding /usr/local/lib/node_modules to the require before requiring any module:
require.paths.push('/usr/local/lib/node_modules');
require('blah'); // it works!
Node 0.4 changed how modules are resolved and it appears that this breaks xml2js.
Node 0.4 looks in ./node_modules for modules.
For me it did help to just link the modules directory to my project directory with ln -s /usr/local/lib/node node_modules
Personally, I found that the XML2JS module needs to be installed via npm locally. While I've only tried this on Windows, I've written a blog post here
Try installing it in the project instead of the global package directory.
If you're using a package.json to manage dependancies then you can just run npm bundle in the project directory and then add require.paths.unshift('./node_modules') at the top of your app file. In my opinion this is the best practice for all projects (especially considering the speed that the development of node takes place).
I guess the simple answer is the current packages for xml2js and xml2js-xpat are busted.
I ended up using node-xml instead. I wish xml2js hadn't been my the first npm module I tried to install.
just make ln -s /usr/local/lib/node /usr/local/lib/node_modules, but before move the content of node_modules into the original node library node - it helped me :)
Try to install the npm package with the -g flag to make it global
npm install -g package_name