Getting error MODULE NOT FOUND in express.js - javascript

const express = require("express");
const app = express();
const path = require("path");
app.use(express.static(staticPath));
let staticPath=path.join(__dirname, "..");
these line of code is giving error: Module not found

You need to make sure that Express is installed.
Run the following command
npm i express --save
Also check if you have package.json file in your App's root directory. If there is not then run the following command.
npm init
npm i express --save

First of all, make you have initialized npm that will end up creating the package.json file:
npm init
then install the express library
npm i express
after that, you are trying to run a file called index.js
Your Node CLI cannot find it in the respected directory
Make sure to fix your run command as follow
node .\{FILE_NAME}.js

Related

Creating directory on root doesn't work via npm but does running node directly

app.js (root is not file owner)
var fs = require('fs');
fs.mkdirSync('/root/tmp');
package.json
{
"scripts": {
"app": "node app.js"
}
}
When I run as root npm run app I got
> app
> node app.js
node:internal/fs/utils:344
throw err;
^
Error: EACCES: permission denied, mkdir '/root/tmp'
But it works when I run just node app.js. Which setting/option of npm can help me run npm run app without errors?
Why npm run change user? How to force npm not to change user?
I can replicate the behavior you see on Linux if I run the commands in a shell I've run via sudo, like this: sudo bash. As in the question, npm run app fails but node app.js works.
The difference is that npm run app runs the executable JavaScript file npm-cli.js from the npm package, which looks like this:
#!/usr/bin/env node
require('../lib/cli.js')(process)
That tells us it's running node via env.
So I got to thinking: What user does that run as? So I added "who": "whoami" to package.json and did npm run who. The answer is: It runs as my normal account, not as root, even when I run that from a shell I've launched via sudo bash. My normal user account can't (of course) create subdirectories of root.

How to fix "express is not a function"?

I installed express but I have this error
var express= require ('express');
var app = express();
var ejs =require('ejs');
var port= 3000
app.get('/',function(req,res,next){
res.render('/form.ejs');
});
app.listen(3000);
module.exports= app;
The version of express you are using is most probably outdated.
Remove express": "^<version>" from package.json and use the following command to install the latest version of express.
npm install --save express
Or you could also set express": "latest" in the package.json file and run the command nom install to install the latest version.

Node Nodemon Error: Cannot find module 'C:\Program Files\Git\node_modules\nodemon\bin\nodemon.js'

I am a beginner at NodeJS stuff.
Environment is Windows 7 64 Bit.
Node is installed and working.
NPM is also working fine.
Nodemon is installed. (In App and also Globally)
Now when I run the command:
"nodemon server.js" or just "nodemon"
it gives the following error:
module.js:549
throw err;
^
Error: Cannot find module 'C:\Program Files\Git\node_modules\nodemon\bin\nodemon.js'
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
I don't understand what this means? And why is it looking inside the Git folder?
Does it have to do anything with Environment Variable? But Node and NPM are both working fine.
Any ideas/suggestion/solution?
Below is my "server.js" file in case you need for reference.
var express = require("express");
var bodyParser = require("body-parser");
var morgan = require("morgan");
var path = require("path");
var app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(function (req, es, next) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "GET, POST");
res.setHeader("Access-Control-Allow-Headers", "X-Requested-With, content-type, Authorization");
next();
});
app.use(morgan("dev"));
app.use(express.static(__dirname + "/app"));
app.get("*", function (req, res) {
res.sendFile(path.join(__dirname + "/index.html"));
});
app.listen(8080);
console.log(">>>>> App is Running <<<<<");
remove node_modules in your project and than install reagain nodemon module, run below commands;
rm -rf node_modules
npm install -g npm#latest
npm i nodemon
In my case it was same issue I solved it by mentioned method hope it will
help you so in package.json in script tag
"start":"nodemon index.js"
and in terminal with this command run
nodemon start
you must validate that the server name (servers.js) is called the same inside the folder, ( package.json ) validate main and start you should not create folders for nodemon.
make sure you are running the server.js is in the root directory and run this command in root directory of your project
nodemon ./server.js
Also you can rename server.js to index.js and just run
nodemon
More on nodemon:
https://www.npmjs.com/package/nodemon
Use command: npm run server and not nodemon server.js you will get exact error that which module is missing.
I think this is a typical path problem. You need to fix your environment variable PATH for npm to execute correctly.
the path you need to add looks like: C:\Program Files\nodejs\node_modules\npm\bin
Find the PATH env var and add the path you expect your npm packages to install from!
Or you can navigate to that directory and execute: npm config set prefix
Nodemon is looking for the main config in the package.json, Which may be missing.
try to add main property in package.json with the key of your entry file.
for example:-
"main":"src/main.ts"
"start": "nodemon src/app.js"

Express-generator is not working

I suspect i could have changed where node modules are installed.I have installed express-generator globally.
D:\chirp> npm install express-generator -g
When i try running
D:\chirp> express --ejs
An error popsup in the Command prompt saying
'express' is not recognised as an internal or external
command,operable program or batch file
Here is a screenshot image
Run
npm bin -g
To see where npm installs it's global binaries. Then add that directory to your PATH.
SET PATH=%PATH%;[new directory]
And add it permanently using the way that you should on your system.
simply use
npx express-generator [your projectName]

Can't start express app on Windows 7/64bit

I have installed node for windows/64bit and I can run server with example form node homepage(node is working). But when I setup express app it's not able to start server. Express is install with npm install -g express command, and express test, npm install commands instal all necessary files but when I type node app.js in cmd it just skippes line. All path variables are setup and everything is working exept this.
instead of runnig node app.js try npm start.
express generator now put file to start the server in bin/www so you have to say node bin/www to start the app.
If you will see your package.json you will get something like this
"scripts": {
"start": "node ./bin/www"
}
so to start the server you can say npm start or node bin/www.

Categories