i'm trying to run this airbnb clone: https://github.com/ricardovasconcelos/AirbnbClone made with react, node, socket.io and mongodb, when it comes to frontend, it runs perfectly, but I'm having an error when trying to run the backend.
This is my package.json:
{
"name": "backend",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"dev": "nodemon src/server.js",
"start": "nodemon src/server.js"
},
"license": "MIT",
"devDependencies": {
"nodemon": "^1.19.4"
},
"dependencies": {
"colors": "^1.4.0",
"cors": "^2.8.5",
"express": "^4.17.3",
"mongoose": "^5.7.3",
"multer": "^1.4.2",
"socket.io": "^2.4.1",
"socket.io-client": "^4.4.1"
}
}
This is my database.js:
const mongoose = require('mongoose')
mongoose.connect('mongodb+srv://airbnb:airbnb#cluster0-lmape.mongodb.net/airbnbClone?retryWrites=true&w=majority', {
useNewUrlParser:true,
useUnifiedTopology: true
})
This is my server.js:
const express = require('express')
const routes = require('./routes')
const cors = require('cors')
const path = require('path')
const socketio = require('socket.io')
const http = require('http')
require('./database/database')
const server = express()
const app = http.Server(server)
const io = socketio(app)
const connectedUsers = {}
io.on('connection', socket => {
const { user_id } = socket.handshake.query
connectedUsers[user_id] = socket.id
})
server.use((req,res,next) => {
req.io = io
req.connectedUsers = connectedUsers
return next()
})
server.use(cors())
server.use(express.json())
server.use('/files', express.static(path.resolve(__dirname, '..','uploads')))
server.use(routes)
app.listen(3333, ()=>console.log('running on 3333'))
This error appears:
$ nodemon src/server.js
[nodemon] 1.19.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node src/server.js`
(node:59368) Warning: Accessing non-existent property 'count' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)
(node:59368) Warning: Accessing non-existent property 'findOne' of module exports inside circular dependency
(node:59368) Warning: Accessing non-existent property 'remove' of module exports inside circular dependency
(node:59368) Warning: Accessing non-existent property 'updateOne' of module exports inside circular dependency
running on 3333
node:internal/errors:464
ErrorCaptureStackTrace(err);
^
Error: querySrv ENOTFOUND _mongodb._tcp.cluster0-lmape.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (node:dns:213:19) {
errno: undefined,
code: 'ENOTFOUND',
syscall: 'querySrv',
hostname: '_mongodb._tcp.cluster0-lmape.mongodb.net'
}
[nodemon] app crashed - waiting for file changes before starting...
I have installed all the dependencies, I would appreciate if someone can help me.
Thanks
Related
I'm trying to run my node server run, but suddenly i have got this error. How can i solve this error? when i command npm run start-dev show me this error. i have change this port, but got same error. Why happend this error? Please help me out, how can i solve this?
here is the error,
node:events:491
throw er; // Unhandled 'error' event
^
Error: listen EACCES: permission denied 30000;
at Server.setupListenHandle [as _listen2] (node:net:1468:21)
at listenInCluster (node:net:1533:12)
at Server.listen (node:net:1632:5)
at Function.listen (D:\ecommerce-api\node_modules\express\lib\application.js:635:24)
at StartDB (D:\ecommerce-api\app.js:34:9)
at Object.<anonymous> (D:\ecommerce-api\app.js:42:1)
at Module._compile (node:internal/modules/cjs/loader:1159:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
at Module.load (node:internal/modules/cjs/loader:1037:32)
at Module._load (node:internal/modules/cjs/loader:878:12)
Emitted 'error' event on Server instance at:
at emitErrorNT (node:net:1512:8)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
code: 'EACCES',
errno: -4092,
syscall: 'listen',
address: '5000;',
port: -1
}
Node.js v18.12.1
[nodemon] app crashed - waiting for file changes before starting...
my env file
PORT=5000;
MONGO_URI=mongodb://127.0.0.1:27017/ecommerce
App js file
require("dotenv").config();
const express = require("express");
const app = express();
const port = process.env.PORT || 5000;
//Database
const connectDB = require("./DB/ConnectDB");
//Paths
const authRoute = require("./Routes/AuthRoute");
//Middleware
app.use(express.json());
app.get("/", (req, res) => {
res.send("Ecommerce API!");
});
app.use("/api/v1/auth", authRoute);
const StartDB = async () => {
try {
connectDB(process.env.MONGO_URI);
app.listen(port, () => {
console.log(`App listening on port ${port}`);
});
} catch (error) {
console.log(error);
}
};
StartDB();
the package json file below.
my pacakge json file
{
"name": "ecommerce-api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node app.js",
"start-dev": "nodemon app.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"express-async-errors": "^3.1.1",
"http-status-codes": "^2.2.0",
"jsonwebtoken": "^9.0.0",
"mongoose": "^6.8.4",
"morgan": "^1.10.0",
"validator": "^13.7.0"
}
}
I got the same error , do not add any spaces in (.env) file , then you should get the answer
I am having issues deploying to Heroku. I have uploaded the relevant env variables under "Config Vars" within Heroku Dashboard but am wondering why I am still getting hit with the following error below:
2022-07-22T06:47:59.068313+00:00 heroku[web.1]: Starting process with command `npm start`
2022-07-22T06:48:00.732711+00:00 app[web.1]:
2022-07-22T06:48:00.732804+00:00 app[web.1]: > project-management-webapp#1.0.0 start
2022-07-22T06:48:00.732804+00:00 app[web.1]: > npm install && cd server && node index.js
2022-07-22T06:48:00.732805+00:00 app[web.1]:
2022-07-22T06:48:01.693914+00:00 app[web.1]:
2022-07-22T06:48:01.693932+00:00 app[web.1]: up to date, audited 128 packages in 601ms
2022-07-22T06:48:01.694072+00:00 app[web.1]:
2022-07-22T06:48:01.694106+00:00 app[web.1]: 14 packages are looking for funding
2022-07-22T06:48:01.694126+00:00 app[web.1]: run `npm fund` for details
2022-07-22T06:48:01.695406+00:00 app[web.1]:
2022-07-22T06:48:01.695406+00:00 app[web.1]: found 0 vulnerabilities
2022-07-22T06:48:02.296149+00:00 heroku[web.1]: State changed from starting to crashed
2022-07-22T06:48:02.083051+00:00 app[web.1]: Server running on port 24830
2022-07-22T06:48:02.087994+00:00 app[web.1]: /app/node_modules/mongodb-connection-string-url/lib/index.js:86
2022-07-22T06:48:02.088006+00:00 app[web.1]: throw new MongoParseError('Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"');
2022-07-22T06:48:02.088007+00:00 app[web.1]: ^
2022-07-22T06:48:02.088007+00:00 app[web.1]:
2022-07-22T06:48:02.088013+00:00 app[web.1]: MongoParseError: Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"
2022-07-22T06:48:02.088013+00:00 app[web.1]: at new ConnectionString (/app/node_modules/mongodb-connection-string-url/lib/index.js:86:19)
2022-07-22T06:48:02.088014+00:00 app[web.1]: at parseOptions (/app/node_modules/mongodb/lib/connection_string.js:209:17)
2022-07-22T06:48:02.088014+00:00 app[web.1]: at new MongoClient (/app/node_modules/mongodb/lib/mongo_client.js:62:63)
2022-07-22T06:48:02.088015+00:00 app[web.1]: at /app/node_modules/mongoose/lib/connection.js:796:16
2022-07-22T06:48:02.088015+00:00 app[web.1]: at new Promise (<anonymous>)
2022-07-22T06:48:02.088016+00:00 app[web.1]: at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:793:19)
2022-07-22T06:48:02.088016+00:00 app[web.1]: at /app/node_modules/mongoose/lib/index.js:379:10
2022-07-22T06:48:02.088016+00:00 app[web.1]: at /app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
2022-07-22T06:48:02.088016+00:00 app[web.1]: at new Promise (<anonymous>)
2022-07-22T06:48:02.088021+00:00 app[web.1]: at promiseOrCallback (/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
2022-07-22T06:48:02.228355+00:00 heroku[web.1]: Process exited with status 1
Below is the relevant code:
INDEX.JS
const express = require('express')
const colors = require('colors')
const { graphqlHTTP } = require('express-graphql')
const cors = require('cors')
const schema = require('./schema/schema')
const connectDB = require('./config/db')
require('dotenv').config()
const app = express()
// MongoDB
connectDB()
// CORS
app.use(cors())
// GraphQL
app.use(
'/graphql',
graphqlHTTP({
schema: schema,
graphiql: process.env.NODE_ENV === 'development',
})
)
app.listen(
process.env.PORT || 4000,
console.log(`Server running on port ${process.env.PORT}`)
)
DB.JS
const mongoose = require('mongoose')
const connectDB = async () => {
const conn = await mongoose.connect(process.env.MONGODB_URI)
console.log(`MongoDB Connected: ${conn.connection.host}`.cyan.underline.bold)
}
module.exports = connectDB
PACKAGE.JSON
{
"name": "project-management-webapp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "npm install && cd server && node index.js",
"dev": "nodemon server/index.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/BIGWALDOR/project-management-webapp.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/BIGWALDOR/project-management-webapp/issues"
},
"homepage": "https://github.com/BIGWALDOR/project-management-webapp#readme",
"dependencies": {
"colors": "^1.4.0",
"cors": "^2.8.5",
"dotenv": "^16.0.1",
"express": "^4.18.1",
"express-graphql": "^0.12.0",
"graphql": "^15.8.0",
"mongoose": "^6.4.5"
},
"devDependencies": {
"nodemon": "^2.0.19"
}
}
I'm definitely at a loss here so any help would be much appreciated!
In a barebones node.js app I am unable to connect to either a local mongoose database or a cloud mongoDB database.
Here is the error:
Error: No valid exports main found for 'D:\Documents\code\node-api\node_modules\mongodb-connec
tion-string-url'
←[90m at resolveExportsTarget (internal/modules/cjs/loader.js:625:9)←[39m
←[90m at applyExports (internal/modules/cjs/loader.js:502:14)←[39m
←[90m at resolveExports (internal/modules/cjs/loader.js:551:12)←[39m
←[90m at Function.Module._findPath (internal/modules/cjs/loader.js:657:22)←[3
9m
←[90m at Function.Module._resolveFilename (internal/modules/cjs/loader.js:960
:27)←[39m
←[90m at Function.Module._load (internal/modules/cjs/loader.js:855:27)←[39m
←[90m at Module.require (internal/modules/cjs/loader.js:1033:19)←[39m
←[90m at require (internal/modules/cjs/helpers.js:72:18)←[39m
at Object.<anonymous> (D:\Documents\code\node-api\node_modules\←[4mmongodb←[24m\lib\connec
tion_string.js:6:41)
←[90m at Module._compile (internal/modules/cjs/loader.js:1144:30)←[39m {
code: ←[32m'MODULE_NOT_FOUND'←[39m
}
[nodemon] app crashed - waiting for file changes before starting...
Here are 2 code snippets that both yield the error:
local:
const mongoose = require('mongoose');
module.exports = async () => {
try{
await mongoose.connect(process.env.DB_URL, {useNewUrlParser: true});
console.log("Database connected");
}catch(error)
{
console.log("error: ", error);
throw new Error(error);
}
}
and cloud:
const { MongoClient } = require('mongodb');
const uri = "mongodb+srv://user1:<[mypasswordgoeshere]>#cluster0.vl2la.mongodb.net/myFirstDatabase?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
client.connect(err => {
const collection = client.db("test").collection("devices");
// perform actions on the collection object
client.close();
});
There are many questions here with a similar error, and I've followed the steps for as many as possible, including reinstalling the packages, delete node_modules and reinstalling, installing autoprefixer, and downgrading versions of various packages. It's important to note that I'm on Windows 7 and therefore cannot update my node.js version any further.
Here is my packages.json file:
{
"name": "node-api",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"autoprefixer": "^9.8.0",
"cors": "^2.8.5",
"dotenv": "^10.0.0",
"express": "^4.17.1",
"mongoose": "^6.0.8"
},
"devDependencies": {
"nodemon": "^2.0.13"
}
}
I had the same issue with mongo-connection-string-url package. Was running node 13.3.0, upgrading to 14.17.5 resolved the issue.
For some reason, starting the server in Node.js terminal produces this error. Doing everything from Windows 7 cmd works perfectly without this error.
I've been stuck on this error for a couple days now, so here I am looking for some insight. I have a PERN stack application that I am trying to deploy for the first time to Heroku. My app builds seemingly fine. My app runs perfectly fine locally in development. I can run it serving static files from react build and I can run the front and backend on two ports and it works. I think the issue has too do with how Sequelize-CLI tries to parse the DATABASE_URL config variable that Heroku sets in production. If I log process.env.DATABASE_URL I get the URI string (noted in error stack below) from Heroku.
I made sure that the PG add-on was configured correctly.
I have all my config variables set in both the .env file and heroku dashboard
My node_modules, .env file are not committed to my repo.
I have deleted my node_modules and package-lock.json and reinstalled them
I have logged out process.env.DATABASE_URL and it successfully logs the URI
I keep getting this error stack:
enter code here2020-07-19T00:56:44.000000+00:00 app[api]: Build succeeded
2020-07-19T00:57:10.671364+00:00 heroku[web.1]: Starting process with command `npm start`
2020-07-19T00:57:14.245382+00:00 app[web.1]:
2020-07-19T00:57:14.245410+00:00 app[web.1]: > locals#1.0.0 start /app
2020-07-19T00:57:14.245411+00:00 app[web.1]: > node server.js
2020-07-19T00:57:14.245411+00:00 app[web.1]:
// this is the console.log(process.env.DATABASE_URL)
2020-07-19T01:09:25.213184+00:00 app[web.1]: postgres://<user>:<password><omitted-info>:<port>/<app>
2020-07-19T00:57:14.903836+00:00 app[web.1]: internal/validators.js:107
2020-07-19T00:57:14.903838+00:00 app[web.1]: throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
2020-07-19T00:57:14.903839+00:00 app[web.1]: ^
2020-07-19T00:57:14.903839+00:00 app[web.1]:
2020-07-19T00:57:14.903841+00:00 app[web.1]: TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. Received type undefined
2020-07-19T00:57:14.903841+00:00 app[web.1]: at validateString (internal/validators.js:107:11)
2020-07-19T00:57:14.903841+00:00 app[web.1]: at Url.parse (url.js:155:3)
2020-07-19T00:57:14.903842+00:00 app[web.1]: at Object.urlParse [as parse] (url.js:150:13)
2020-07-19T00:57:14.903843+00:00 app[web.1]: at new Sequelize (/app/node_modules/sequelize/lib/sequelize.js:187:28)
2020-07-19T00:57:14.903843+00:00 app[web.1]: at Object.<anonymous> (/app/models/index.js:14:15)
2020-07-19T00:57:14.903844+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:759:30)
2020-07-19T00:57:14.903844+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)
2020-07-19T00:57:14.903845+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:628:32)
2020-07-19T00:57:14.903845+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:555:12)
2020-07-19T00:57:14.903845+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:666:19)
2020-07-19T00:57:14.903846+00:00 app[web.1]: at require (internal/modules/cjs/helpers.js:16:16)
2020-07-19T00:57:14.903846+00:00 app[web.1]: at Object.<anonymous> (/app/server.js:11:16)
2020-07-19T00:57:14.903847+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:759:30)
2020-07-19T00:57:14.903847+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)
2020-07-19T00:57:14.903847+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:628:32)
2020-07-19T00:57:14.903848+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:555:12)
2020-07-19T00:57:14.903848+00:00 app[web.1]: at Function.Module.runMain (internal/modules/cjs/loader.js:826:10)
2020-07-19T00:57:14.903849+00:00 app[web.1]: at internal/main/run_main_module.js:17:11
2020-07-19T00:57:14.915610+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-07-19T00:57:14.916028+00:00 app[web.1]: npm ERR! errno 1
2020-07-19T00:57:14.917598+00:00 app[web.1]: npm ERR! locals#1.0.0 start: `node server.js`
2020-07-19T00:57:14.917834+00:00 app[web.1]: npm ERR! Exit status 1
2020-07-19T00:57:14.918113+00:00 app[web.1]: npm ERR!
2020-07-19T00:57:14.918349+00:00 app[web.1]: npm ERR! Failed at the locals#1.0.0 start script.
2020-07-19T00:57:14.918598+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-07-19T00:57:14.952443+00:00 app[web.1]:
2020-07-19T00:57:14.952709+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-07-19T00:57:14.952798+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2020-07-19T00_57_14_920Z-debug.log
2020-07-19T00:57:15.047526+00:00 heroku[web.1]: Process exited with status 1
2020-07-19T00:57:15.094041+00:00 heroku[web.1]: State changed from starting to crashed
2020-07-19T00:57:15.951511+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=locals-deploy.herokuapp.com request_id=f27a1f1b-5d62-4d98-9383-6d5e46242789 fwd="71.92.89.136" dyno= connect= service= status=503 bytes= protocol=https
Now if I try to run a db:migrate on heroku in production mode I get another error regarding the URL.
heroku run sequelize db:migrate --env production --app locals-deploy
Error:
Running sequelize db:migrate on ⬢ locals-deploy... up, run.7436 (Free)
Sequelize CLI [Node: 12.0.0, CLI: 5.5.1, ORM: 5.22.3]
Loaded configuration file "config/config.js".
Using environment "production".
ERROR: Error parsing url: undefined
package.json
{
"name": "locals",
"version": "1.0.0",
"description": "A place for travelers to meet locals.",
"main": "server.js",
"scripts": {
"start": "node server.js",
"heroku-postbuild": "cd client && npm install && npm run build"
},
"author": "Name Name",
"license": "MIT",
"dependencies": {
"axios": "^0.19.2",
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"cloudinary": "^1.22.0",
"config": "^3.3.1",
"cors": "^2.8.5",
"express": "^4.17.1",
"express-fileupload": "^1.1.7-alpha.3",
"express-validator": "^6.5.0",
"jsonwebtoken": "^8.5.1",
"nodemon": "^2.0.4",
"pg": "^8.2.1",
"pg-hstore": "^2.3.3",
"sequelize": "^5.21.11",
"sequelize-cli": "^5.5.1"
},
"engines": {
"node": "12.0.0"
}
}
server.js
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const PORT = process.env.PORT || 5000;
const upload = require('express-fileupload');
app.use(upload({useTempFiles: true}));
const cors = require('cors');
const path = require('path');
const models = require('./models');
// variable to enable global error logging
const enableGlobalErrorLogging =
process.env.ENABLE_GLOBAL_ERROR_LOGGING === 'true';
// Enable All CORS Requests
app.use(cors());
//Init Middleware
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
extended: true,
})
);
//app.use(express.static(path.join(__dirname, 'client/build')));
console.log(process.env.NODE_ENV);
//if running in production mode then it serves static files from build in client
if (process.env.NODE_ENV === 'production') {
//points to index.js in client
app.use(express.static(path.join(__dirname, 'client/build')));
}
//Routes
app.use('/users', require('./routes/users'));
app.use('/auth', require('./routes/auth'));
app.use('/posts', require('./routes/posts'));
app.use('/api/profile', require('./routes/profile'));
app.use('/adventure', require('./routes/adventure'));
app.use('/review', require('./routes/review'));
app.use('/favorites', require('./routes/favorites'));
app.use('/upload', require('./routes/uploadImage'));
//catch all method redirects to build folder
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});
// send 404 if no other route matched
app.use((req, res) => {
res.status(404).json({
message: 'Route Not Found',
});
});
// setup a global error handler
app.use((err, req, res, next) => {
console.log('I am catching the error');
if (enableGlobalErrorLogging) {
console.error(`Global error handler: ${JSON.stringify(err.stack)}`);
}
if (err.name === 'SequelizeUniqueConstraintError') {
return res.status(400).json({
errors: ['Email for user already exists'],
});
}
// if (err.name === 'SequelizeDatabaseError') {
// return res.status(404).json({errors: 'Oh no! Page not found.'});
// }
res.status(err.status || 500).json({
message: err.message,
name: err.name,
error: {},
});
});
//Sets Port and Listens
return models.sequelize.sync().then((result) => {
app.listen(process.env.PORT || 5000, () => {
console.log(`App running on port ${process.env.PORT || 5000}.`);
});
});
config.js
if (process.env.NODE_ENV !== 'production') require('dotenv').config();
//PG_HOST = 127.0.0.1
console.log(process.env.DATABASE_URL);
module.exports = {
development: {
username: process.env.PG_USER,
password: process.env.PG_PASSWORD,
database: process.env.PG_DATABASE,
host: process.env.PG_HOST,
dialect: 'postgres',
port: process.env.PG_PORT,
operatorsAliases: 0,
},
test: {
username: process.env.PG_USER,
password: process.env.PG_PASSWORD,
database: process.env.PG_DATABASE,
host: process.env.PG_HOST,
dialect: 'postgres',
port: process.env.PG_PORT,
operatorsAliases: 0,
},
production: {
use_env_variable: process.env.DATABASE_URL,
dialect: 'postgres',
dialectOptions: {
ssl: true,
},
},
};
Procfile
web: node server.js
EDIT
I made a bug report and I actually think I may have fixed the issue in the node_modules package where the error was being thrown. Now I have to figure out how to make that change inside Heroku since we don't commit node_modules to see if it works. I can run it just find locally now in production mode without reproducing the error.
Bug Report if anyone is interested
https://github.com/sequelize/sequelize/issues/12528
Try this in your config file
production: {
use_env_variable: "DATABASE_URL",
dialect: 'postgres',
dialectOptions: {
ssl: true,
},
},
Since you are using use_env_variable you should be able to just have the name of the variable as the value.
So as it turns out there is an error in my node_modules/sequelize package. I traced into into /node_modules/sequelize/lib/sequelize.js:187:28. Whats happening is that my config file is submitting the URI string under production from process.env.DATABASE_URL and it is attached to the username parameter, or index 1. The issue is how url.parse is written and what argument is passed. If the user submits the DATABASE_URL URI it passed as if it is an array, however, the first argument is always undefined since its username that it is attached to, which is index 1. I changed the parsing arguments to const urlParts = url.parse(options.use_env_variable, true); and this seemed to clear up all my issues by directly taking the URI to be parsed instead of worrying about index values.
constructor(database, username, password, options) {
let config;
if (arguments.length === 1 && typeof database === 'object') {
// new Sequelize({ ... options })
options = database;
config = _.pick(options, 'host', 'port', 'database', 'username', 'password');
} else if (
(arguments.length === 1 && typeof database === 'string') ||
(arguments.length === 2 && typeof username === 'object')
) {
// new Sequelize(URI, { ... options }) <--this was already commented out
config = {};
options = username || {};
// url.parse wants to access the object like an array, however, first item is always undefined and it
// needs to access the use_env_variable, which is attached to username argument
// const urlParts = url.parse(options.use_env_variable, true);
const urlParts = url.parse(arguments[0], true);
options.dialect = urlParts.protocol.replace(/:$/, '');
options.host = urlParts.hostname;
...
After that I downloaded npm patch-package and I pushed my patched package to git and heroku and it works.
I'm deploying an node.js app with deps/modules like : stylus, express, socket.io on heroku.
The beginning of the server.js
/**
* Bootstrap app.
*/
// I've tried with and without that line... not sure what it does
require.paths.unshift(__dirname + '/../../lib/');
/**
* Module dependencies.
*/
// I've tried with "socket.io", "./socket.io" and "Socket.IO"
var express = require('express')
, stylus = require('stylus')
, nib = require('nib')
, sio = require('socket.io');
The file package.json
{
"name": "test.io" , "description": "blabla" , "version": "0.0.1" , "dependencies": {
"express": "2.3.11"
, "jade": "0.12.1"
, "stylus": "0.13.3"
, "nib": "0.0.8"
} }
So the one and only heroku web worker crash. Here's the log :
2011-10-24T09:15:27+00:00 heroku[slugc]: Slug compilation finished
2011-10-24T09:15:35+00:00 heroku[web.1]: Unidling
2011-10-24T09:15:35+00:00 heroku[web.1]: State changed from down to created
2011-10-24T09:15:35+00:00 heroku[web.1]: State changed from created to starting
2011-10-24T09:15:39+00:00 heroku[web.1]: State changed from starting to crashed
2011-10-24T09:15:39+00:00 heroku[web.1]: State changed from crashed to created
2011-10-24T09:15:39+00:00 heroku[web.1]: State changed from created to starting
2011-10-24T09:15:41+00:00 heroku[web.1]: Starting process with command `node app.js`
2011-10-24T09:15:41+00:00 app[web.1]:
2011-10-24T09:15:41+00:00 app[web.1]: node.js:134
2011-10-24T09:15:41+00:00 app[web.1]: throw e; // process.nextTick error, or 'error' event on first tick
2011-10-24T09:15:41+00:00 app[web.1]: ^
2011-10-24T09:15:41+00:00 app[web.1]: Error: Cannot find module 'socket.io'
2011-10-24T09:15:41+00:00 app[web.1]: at Function._resolveFilename (module.js:320:11)
2011-10-24T09:15:41+00:00 app[web.1]: at Function._load (module.js:266:25)
2011-10-24T09:15:41+00:00 app[web.1]: at require (module.js:348:19)
2011-10-24T09:15:41+00:00 app[web.1]: at Object.<anonymous> (/app/app.js:18:11)
2011-10-24T09:15:41+00:00 app[web.1]: at Module._compile (module.js:404:26)
2011-10-24T09:15:41+00:00 app[web.1]: at Object..js (module.js:410:10)
2011-10-24T09:15:41+00:00 app[web.1]: at Module.load (module.js:336:31)
2011-10-24T09:15:41+00:00 app[web.1]: at Function._load (module.js:297:12)
2011-10-24T09:15:41+00:00 app[web.1]: at Array.<anonymous> (module.js:423:10)
2011-10-24T09:15:41+00:00 app[web.1]: at EventEmitter._tickCallback (node.js:126:26)
2011-10-24T09:15:41+00:00 heroku[web.1]: Process exited
2011-10-24T09:15:44+00:00 heroku[web.1]: State changed from starting to crashed
So, any ideas ? Ever happened to anyone ?
I believe you should add socket.io in dependencies in package.json.
{ "name": "test.io" , "description": "blabla" , "version": "0.0.1" , "dependencies": { "express": "2.3.11" , "jade": "0.12.1" , "stylus": "0.13.3" , "nib": "0.0.8" , "socket.io" : "0.8.5" } }
Or just do this:
npm install socket.io --save
Which will install the latest version of socket.io and add it to the dependencies.
npm install socket.io
did the trick for me. If you are under windows make sure you are running it as administrator.
These are solutions to socket.io related problems
I hope i will work
Ports in your (index.js or server.js) & (index.html and your client.js) port must be different. (refer below code)
=============your index.js file ======================
(port here is 8000)
const express = require("express")
var app = express();
const http = require('http')
var server = http.createServer(app);
const port = process.env.PORT || 8000
server.listen(port,()=>
{
console.log("Listening at port => "+port)
});
var io = require('socket.io')(server, {
cors: {
origin: '*',
}
});
const cors = require("cors")
app.use(cors())
=============your client.js file ======================
port here is 8080
const socket = io.connect('https://localhost:8080/')
=============your index.html file ======================
port here is 8080
<script defer src="https://localhost:8080/socket.io/socket.io.js">
</script>
Remember your "server.js or index.js" port should be different from "client.js" port (Rememeber this is important)
(index.html and your client.js) port must be same
You should always use 'http' while working with socket.io (refer above code)
U may not included cors as it allows u to have more resourses , without cors heroku prevent some dependencies to not install in heroku (refer above code)
Try replacing "io" to "io.connect"
const socket = io.connect('https://localhost:8080/')
Must write tag at the end in the HTML
U may forget to add this code which is must in "socket.io"
It is required in your html file
delete "node_modules" and "package-lock.json"
and write "npm i" in cmd
This should be in package.json 's scripts
"start":"node index.js",
I am not talking about nodemon , use simple node over here
May be version is creating a problem , u can avoid it by copying all "devDependencies" to "dependencies" in "package.json" and put "*" in version like this
"dependencies": {
"cors": "*",
"express": "*",
"nodemon": "*",
"socket.io": "*"
},
"devDependencies": {}
Are you sure you have socket io installed? Try running:
npm install socket.io
This will install the latest socket.io available in nodes packet repository.