I followed a node.js tutorial online and I got this error when I runned the script.
node:internal/modules/cjs/loader:936 throw err; ^ Error: Cannot find module 'C:\Users\sem20\Downloads\testing\API\cmusic-api\index.js' at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:17:47 { code: 'MODULE_NOT_FOUND', requireStack: [] }
I installed every package I needed
Here is the index.js
const express = require('express')
const axios = require('axios')
const cheerio = require('cheerio')
const app = express()
app.listen(PORT, () => console.log(`Listening on port ${PORT}`))
Can somebody help me fix this?
I installed every package I needed
Related
Here is my server.js file. I try to dockerizing my node server but I got an Error: Cannot find module './router/Seller'. How can I solve this problem?
const express = require("express");
const cors = require("cors")
const app = express();
const mongoose = require("mongoose");
const dotenv = require("dotenv")
const port = 5000;
const SellerRouter = require('./router/Seller');
dotenv.config();
mongoose.connect(`mongodb+srv://name:pass#cluster0.mcg5e.mongodb.net/Databasename?retryWrites=true&w=majority`).then(()=>console.log("DB connected successfully")).catch((error)=>{
console.log(error)
})
app.use(cors());
app.use(express.json());
app.use("/api/seller" , SellerRouter)
app.listen(port, ()=>{
console.log(`SERVER IS RUNNING at http://localhost:${port}`)
})
Here is my Dockerfile:
FROM node:16
WORKDIR /Backend
ENV PATH="./node_modules/.bin:$PATH"
COPY . .
COPY package*.json ./
RUN npm install
CMD [ "node", "index.js" ]
ENV MONGO_URL = mongodb+srv://name:Your password#cluster0.mcg5e.mongodb.net/Databasename?retryWrites=true&w=majority
ENV JWT_SEC = Your password
ENV PASS_SEC = Yourr password
Here is my result which means error
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/Backend/index.js:9:22)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/Backend/index.js' ]
}
PS C:\Users\khadk\OneDrive\Desktop\dd>
I tried following this stackoverflow post to try and see my folder on my static webpage but no luck:https://stackoverflow.com/a/31274417
Here is my code:
const express = require('express');
const app = express();
const db = require('./persistence');
var fs = require('fs');
var files = fs.readdirSync('./static/reports');
app.use(require('body-parser').json());
app.use(express.static(__dirname + '/static'));
db.init().then(() => {
app.listen(3000, () => console.log('Listening on port 3000'));
}).catch((err) => {
console.error(err);
process.exit(1);
});
const gracefulShutdown = () => {
db.teardown()
.catch(() => {})
.then(() => process.exit());
};
process.on('SIGINT', gracefulShutdown);
process.on('SIGTERM', gracefulShutdown);
process.on('SIGUSR2', gracefulShutdown); // Sent by nodemon
Here is a picture of what it looks like from vscode:
Here is a picture of the error from docker:
Here is the error from text:
internal/fs/utils.js:269
throw err;
^
Error: ENOENT: no such file or directory, scandir './static/reports'
at Object.readdirSync (fs.js:955:3)
at Object.<anonymous> (/app/src/index.js:5:16)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
at internal/main/run_main_module.js:17:47 {
errno: -2,
syscall: 'scandir',
code: 'ENOENT',
path: './static/reports'
}
Starting a file path with a / denotes that it's at the root of the filesystem. Don't use a /; instead, just use static/reports. You can also use ./static/reports if the code will be run from the src/ directory every time.
var fs = require('fs');
var files = fs.readdirSync('static/reports/');
I am building a basic web scraper using node, express, and puppeteer, and when I try to run node index.js. I get this error
alexskreen#Alexs-MacBook-Air WOD-Scraper2 % node server/index.js
internal/modules/cjs/loader.js:960
throw err;
^
Error: Cannot find module './server/scrapers'
Require stack:
- /Users/alexskreen/Desktop/WOD-Scraper2/server/index.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:957:15)
at Function.Module._load (internal/modules/cjs/loader.js:840:27)
at Module.require (internal/modules/cjs/loader.js:1019:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (/Users/alexskreen/Desktop/WOD-Scraper2/server/index.js:8:18)
at Module._compile (internal/modules/cjs/loader.js:1133:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
at Module.load (internal/modules/cjs/loader.js:977:32)
at Function.Module._load (internal/modules/cjs/loader.js:877:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/Users/alexskreen/Desktop/WOD-Scraper2/server/index.js' ]
}
Before adding my require statements everything is working:
const express = require("express");
const app = express();
const port = 3000;
const bodyParser = require("body-parser");
const scrapers = require('./server/scrapers');
const db = require('./server/db');
If module is in your subfolder you should use,
const something = require("./subfolder/module");
If it is not a subfolder module then make sure it is installed in node_modules.
i am still new in backend but i am facing an error since yesterday whenever i try to run my server. BELOW IS MY CODE AND ERROR
const express = require("express");
const app = express();
PORT = 8443;
app.listen("PORT", () => {
console.log("Server up and running")
});
AND HERE IS MY ERROR
events.js:288
throw er; // Unhandled 'error' event
^
Error: listen EACCES: permission denied PORT
←[90m at Server.setupListenHandle [as _listen2] (net.js:1292:21)←[39m
←[90m at listenInCluster (net.js:1357:12)←[39m
←[90m at Server.listen (net.js:1456:5)←[39m
at Function.listen (C:\Users\AbTorres9\Desktop\YelpCamp\node_modules\←[4mexpress←[24m\lib\application.js:618:24)
at Object.<anonymous> (C:\Users\AbTorres9\Desktop\YelpCamp\app.js:6:5)
←[90m at Module._compile (internal/modules/cjs/loader.js:1158:30)←[39m
←[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)←[39m
←[90m at Module.load (internal/modules/cjs/loader.js:1002:32)←[39m
←[90m at Function.Module._load (internal/modules/cjs/loader.js:901:14)←[39m
←[90m at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)←[39m
Emitted 'error' event on Server instance at:
←[90m at emitErrorNT (net.js:1336:8)←[39m
←[90m at processTicksAndRejections (internal/process/task_queues.js:84:21)←[39m {
code: ←[32m'EACCES'←[39m,
errno: ←[32m'EACCES'←[39m,
syscall: ←[32m'listen'←[39m,
address: ←[32m'PORT'←[39m,
port: ←[33m-1←[39m
}
[nodemon] app crashed - waiting for file changes before starting...
const express = require("express");
const app = express();
const PORT = 8443;
app.listen(PORT, () => {
console.log("Server up and running")
});
you had some errors first of all you did not initialized the PORT and second you passed PORT as string in app.listen()
You are missing a view normal JavaScript things. Like defining port. And the .listen function takes in the port variable rather than a string saying "PORT"
See working example:
const express = require('express');
const app = express();
const port = 8443;
app.listen(port, () => console.log(`Server running on port ${port}!`));
#Abhishek, I think on which port are you using that is block by your environment kindly allow it on the firewall or just disable the firewall and access again it works for you!!
You should use the variable and not the string in the app.listen() function. Try to use something like this:
app.listen(PORT, () => {
console.log("Server up and running")
});
Am using node Active directory for validating users. Here is my node code:
const express = require('express');
const async = require('async');
const activedirectory = require('activedirectory');
var underscore = require('underscore');
const ldap = require('ldapjs');
const bunyan = require('bunyan');
const app = express();
app.get('/',(req,res) => {
var config = { url: 'ldap://10.100.1.10',
baseDN: 'dc=HBCAudit,dc=COM',
username: 'traveldesk.dev',
password: '$oftvision#123' }
var ad = new activedirectory(config);
});
//Port Number
const port = 7000;
//CORS Middleware
app.use(cors());
//Start Server
app.listen(port, () =>{
console.log('Server started on port '+ port);
});
But am getting error in command prompt like this:
c:\spitravel>node app.js
module.js:471
throw err;
^
Error: Cannot find module 'once'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (c:\spitravel\node_modules\ldapjs\lib\client\client.js
:8:12)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
I don't know why am getting this error. Am new to NODEJS.
Its due to depencies of ldapjs module, please run npm install ldapjs and recheck