EPERM error while trying to read fullchain.pem - node.js

I'm trying to host an HTTPS website with Node.js on my Windows 10 computer, but Node.js has suddenly stopped being able to read the fullchain.pem I obtained using Let's Encrypt's Certbot.
This is my Node.js code:
const fs = require("fs")
const https = require("https")
const FS_OPTIONS = {
encoding: "utf-8",
flag: "r",
}
const key = fs.readFileSync("C:/Certbot/live/subdomain.example.com/privkey.pem", FS_OPTIONS)
const cert = fs.readFileSync("C:/Certbot/live/subdomain.example.com/fullchain.pem", FS_OPTIONS)
https.createServer({ key, cert }, (req, res) => {
// do stuff...
}).listen(8888)
However, when I run it, I receive the following error:
Error: EPERM: operation not permitted, open 'C:/Certbot/live/subdomain.example.com/fullchain.pem'
at Object.openSync (fs.js:476:3)
at Object.readFileSync (fs.js:377:35)
at Object.<anonymous> (D:\Users\...\index.js:8:17)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47 {
errno: -4048,
syscall: 'open',
code: 'EPERM',
path: 'C:/Certbot/live/subdomain.example.com/fullchain.pem'
}
The only way I can bypass this is by running as administrator, but this is very strange, as this was not required before.
How can I restore this to its previous behavior?

you just need to change the access rights of the folder and subfolders of : C:/Certbot
add access to the user running nodejs. for example following this :
https://v2cloud.com/tutorials/how-to-change-folder-permissions-on-windows-2016

Related

Having issue while connecting NodeJS with SQL

since 2 weeks I am trying to connect Microsoft SQL server with NodeJS. Please help me out with this
Am available on Instagram : shaziyali8
Thanks in advance
Here is my Code#
const mysql = require("mysql");
const connection = mysql.createConnection({
host: "HP\SQLEXPRESS", // I tried things such as : localhost, Id address: 127.0.0.1, . (a dot)
user: "root",
password: "",
database: "testEmp",
// port: 8889,
});
// console.log(connection);
connection.connect((err) => {
if (err) {
console.log(err);
} else {
console.log("connect");
}
});
Got error
Error: getaddrinfo ENOTFOUND HPSQLEXPRESS
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26)
--------------------
at Protocol._enqueue (F:\NodeJs\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (F:\NodeJs\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (F:\NodeJs\node_modules\mysql\lib\Connection.js:116:18)
at Object.<anonymous> (F:\NodeJs\MySql\index.js:13:12)
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)
at node:internal/main/run_main_module:17:47 {
errno: -3008,
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'HPSQLEXPRESS',
fatal: true
}
From what I can see, you are trying to connect to a Microsoft SQL server but you are using a MySQL client npm package for the connection. The correct package to connect to a MSSQL server in Node.js is mssql
Install this and check if the connection issue still persists. Also, if the MSSQL server is on your local then using localhost as host name with correct port number would work most of the time.
There are also Connection Strings that you can use to connect to the MSSQL server.
See the documentation here

Node script "Cannot find module"

I have the following file structure:
/avro.js
/node_modules/...
/package.json
/package-lock.json
I have run:
npm install javro yargs
In my avro.js I do:
#!/usr/bin/env node
const { javro, SchemaRegistryAvroFetcher } = require('javro');
const yargs = require("yargs");
const options = yargs
.scriptName("javro")
.positional('inpath', {
type: 'string',
describe: 'path to JSON Schema file to convert'
})
.positional('outpath', {
type: 'string',
describe: 'path to save converted Avro Schema'
})
.help()
.argv;
When I try to run the script I get:
./avro.js --help
internal/modules/cjs/loader.js:883
throw err;
^
Error: Cannot find module 'javro'
Require stack:
- /Users/anentropic/Documents/Dev/Personal/myproj/avro.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
at Function.Module._load (internal/modules/cjs/loader.js:725:27)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (/Users/anentropic/Documents/Dev/Personal/myproj/avro.js:2:46)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/Users/anentropic/Documents/Dev/Personal/myproj/avro.js' ]
}
Node.js isn't my first language, but I thought this should work?

Error: listen EACCES: permission denied 8085;

I tried running the backend server but when I run the command node backend/server.js in my cmd, I'm getting this error:
D:\theshoppingcart>node backend/server.js
events.js:187
throw er; // Unhandled 'error' event
^
Error: listen EACCES: permission denied 8085;
at Server.setupListenHandle [as _listen2] (net.js:1284:19)
at listenInCluster (net.js:1349:12)
at Server.listen (net.js:1448:5)
at Object.<anonymous> (D:\theshoppingcart\backend\server.js:16:8)
at Module._compile (internal/modules/cjs/loader.js:959:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
at internal/main/run_main_module.js:17:11
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1328:8)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'EACCES',
errno: 'EACCES',
syscall: 'listen',
address: '8085;',
port: -1
}
this is my backend/server.js file:
require('dotenv').config()
const express = require("express");
const connectDB = require('./config/db');
connectDB();
const app = express();
const PORT = process.env.PORT || 8085;
app.listen(PORT, ()=> console.log(`server running on port ${PORT}`));
I had the same issue, you should remove the semicolon ; from the .env file. That has worked for me.
Open the .env file and check ";".
PORT = 5050;
You need to remove the ";" from the .env file. make it:
PORT = 5050

node.js module not found in subfolder

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.

Error while deploying to Google App Engine

I am trying to deploy an application to Google App Engine. Its a Node.js application with MySQL Backend. I am using Sequelize ORM. Both the Cloud SQL Instance and the App Engine are in the same project. When I try to deploy and start the application I am getting the following error.
Updating service [default] (this may take several minutes)...failed.
ERROR: (gcloud.app.deploy) Error Response: [9]
Application startup error:
> teachhub#1.0.0 start /app
> node server
/app/node_modules/sequelize/lib/sequelize.js:236
throw new Error('The dialect ' + this.getDialect() + ' is not supported. Supported dialects: mssql, mysql, postgres, and sqlite.');
^
Error: The dialect my-nodejs-codelab-1217-190421 is not supported. Supported dialects: mssql, mysql, postgres, and sqlite.
at new Sequelize (/app/node_modules/sequelize/lib/sequelize.js:236:15)
at Object.<anonymous> (/app/models/index.js:13:18)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/app/routes/apiRoutes.js:1:72)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/app/server.js:3:14)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! teachhub#1.0.0 start: `node server`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the teachhub#1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2018-07-08T15_03_28_369Z-debug.log
Given below is my models/index.js...
"use strict";
var fs = require("fs");
var path = require("path");
var Sequelize = require("sequelize");
var basename = path.basename(module.filename);
var env = process.env.NODE_ENV || "development";
let keys = require(__dirname + "/../config/config.js");
keys = keys.dbConfig[env];
var db = {};
if(process.env.INSTANCE_CONNECTION_NAME){
var sequelize = new Sequelize(process.env.INSTANCE_CONNECTION_NAME, {dialect:"mysql"});
} else if (process.env.JAWSDB_URL) {
var sequelize = new Sequelize(process.env.JAWSDB_URL);
} else if (keys.use_env_variable) {
var sequelize = new Sequelize(process.env[keys.use_env_variable], keys);
} else {
var sequelize = new Sequelize(
keys.database,
keys.username,
keys.password,
keys
);
}
Can someone please help?
It seems that it is missing a definition to the sequelize dialect. In your case ‘key.dialect’, assuming that you have defined the dialect in your config file. The dialect defines the kind of database used on your project as you can confirm in the following documentation: http://docs.sequelizejs.com/manual/installation/usage.html
In the Documentation:
const sequelize = new Sequelize({
database: 'db_name',
username: 'username',
password: null,
dialect: 'mysql'
});
In your code:
enter codvar sequelize = new Sequelize(
Keys.database,
keys.username,
keys.password,
keys.dialect
);

Resources