I'm using sequelize migrations. Here is my config.js:
require('dotenv').config()
module.exports = {
development: {
database: process.env.DEV_DATABASE,
host: process.env.DEV_HOST,
dialect: 'postgres'
},
test: {
database: process.env.TEST_DATABASE,
host: process.env.TEST_HOST,
dialect: 'postgres'
},
production: {
database: process.env.DATABASE_URL,
dialect: 'postgres'
},
}
When I deploy to heroku and run:
heroku run npx sequelize-cli db:migrate
I get the error:
Loaded configuration file "config/config.js".
Using environment "production".
ERROR: connect ECONNREFUSED 127.0.0.1:5432
I have tried changing my config to use_env_variable: DATABASE_URL and I get Error reading "config/config.js". Error: ReferenceError: DATABASE_URL is not defined
If I console.log(process.env.DATABASE_URL) I get something like:
postgres://bczwqcbnwftivi:80bbc89546c0953e0ffd221a950cac249d25d4ef1812127054af7c2f504b2c39#ec2-54-225-205-79.compute-1.amazonaws.com:5432/d8gsvl0n8qilcs
This worked:
{
"development": {
"database": "products_api_development",
"dialect": "postgres"
},
"test": {
"database": "products_api_test",
"dialect": "postgres"
},
"production": {
"use_env_variable": "DATABASE_URL",
"dialect": "postgres",
"dialectOptions": {
"ssl": true
}
}
}
Here's further documentation for configuring the sequelize config file.
More info on setting up environment variables on Heroku:
You can set up environment variables at https://dashboard.heroku.com/apps/[your-app-name]/settings. In the case of node.js those variables will then be available in your app using process.env.[variable-name].
Related
I am trying to deploy Strapi on Heroku
But it does not work. I get this log
2020-05-27T15:04:05.012958+00:00 app[web.1]: > strapi-oskogen-mongodb#0.1.0 start /app
2020-05-27T15:04:05.012959+00:00 app[web.1]: > node server.js
2020-05-27T15:04:05.012960+00:00 app[web.1]:
2020-05-27T15:04:08.188595+00:00 app[web.1]: (node:23) Warning: Accessing non-existent property 'count' of module exports inside circular dependency
2020-05-27T15:04:08.188639+00:00 app[web.1]: (Use `node --trace-warnings ...` to show where the warning was created)
2020-05-27T15:04:08.189164+00:00 app[web.1]: (node:23) Warning: Accessing non-existent property 'findOne' of module exports inside circular dependency
2020-05-27T15:04:08.189299+00:00 app[web.1]: (node:23) Warning: Accessing non-existent property 'remove' of module exports inside circular dependency
2020-05-27T15:04:08.189381+00:00 app[web.1]: (node:23) Warning: Accessing non-existent property 'updateOne' of module exports inside circular dependency
2020-05-27T15:04:12.308848+00:00 app[web.1]: [2020-05-27T15:04:12.308Z] error Error: listen EADDRNOTAVAIL: address not available 52.54.48.43:31639
2020-05-27T15:04:12.308857+00:00 app[web.1]: at Server.setupListenHandle [as _listen2] (net.js:1296:21)
2020-05-27T15:04:12.308858+00:00 app[web.1]: at listenInCluster (net.js:1361:12)
2020-05-27T15:04:12.308859+00:00 app[web.1]: at GetAddrInfoReqWrap.doListen [as callback] (net.js:1498:7)
2020-05-27T15:04:12.308859+00:00 app[web.1]: at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:68:8)
2020-05-27T15:04:22.927720+00:00 heroku[web.1]: Stopping all processes with SIGTERM
I use MongoDB on Atlas. It works well on localhost both dev and prod environment.
My production files:
server.js
database.js
response.js
app settings
Where did I miss something?
Which is the value of process.env.HOST. For some reason different from 0.0.0.0 (just a shot in the dark - https://strapi.io/documentation/3.0.0-beta.x/migration-guide/migration-guide-beta.19-to-beta.19.4.html#listened-host-changed)
I had to change server.js file (port 443):
module.exports = ({ env }) => ({
host: env('HOST', '0.0.0.0'),
port: env.int('PORT', 443),
admin: {
auth: {
secret: env('ADMIN_JWT_SECRET'),
},
},
});
My database.js file:
module.exports = ({ env }) => ({
defaultConnection: "default",
connections: {
default: {
connector: "mongoose",
settings: {
uri: env("DATABASE_URI"),
ssl: { rejectUnauthorized: false }
},
options: {
ssl: true,
authenticationDatabase: "",
useUnifiedTopology: true,
pool: {
min: 0,
max: 10,
idleTimeoutMillis: 30000,
createTimeoutMillis: 30000,
acquireTimeoutMillis: 30000
}
},
},
},
});
I hope it will help to somebody who has the same problem :-)
Not sure if it's what #Rochadsouza meant, but I got the same error and what finally solved the issue in my case was to set the host to 0.0.0.0. For some reasons, the host cannot be the app url, it needs to allow all hosts on Heroku. I was setting the host in server.js using env('HOST', '0.0.0.0') but had the HOST env var set to my app url on Heroku...
Hope it saves some time to others facing the same error.
I have 2 package.json scripts that look like this:
"start": "next start -p $PORT",
"pm2_staging": "pm2 restart ecosystem.config.js --env staging",
And an ecosystem.config.js that looks like this:
module.exports = {
apps: [
{
name: 'test.co.uk',
script: 'npm',
args: 'start',
env_staging: {
API: 'staging',
NODE_ENV: 'production',
PORT: 3001,
},
},
],
};
I then run the following:
TEST_VAR='test' npm run pm2_staging
I would expect the following to happen:
The PM2 restart command fires
ecosystem.config.js fires the npm start command and sets some environment variables
The app starts and all env vars are available, including TEST_VAR (set in the original command)
What actually happens is all the env vars from the ecosystem are correctly set, but TEST_VAR is not available in the app. Why is this and how do I go about setting secret keys from CI tools if I can't do this?
I ran into the same problem tonight. After looking every where I found the config needed. The env variable needs to go in your ecosystem.config.js like below. In my case I put it at the root of my server
module.exports = {
apps : [{
name: 'nextjs',
script: 'yarn',
args:"start",
cwd:"/var/www/myapp/",
instances: 2,
autorestart: true,
watch: false,
max_memory_restart: '1G',
env: {
NODE_ENV: 'development'
},
env_production: {
NODE_ENV: 'production',
API_URL: 'YOUR ENV URL',
PORT:8000
}
}]
};
package.json like so
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start -p 8000"
},
...
and executed something like this
#!/bin/bash
cd /var/www/myapp/
git pull && yarn install && yarn build
cd ~/
pm2 start ecosystem.config.js --env production
Then the API_URL would be available in const API_URL = process.env.API_URL in your app.
These urls helped
https://willandskill.se/en/setup-a-next-js-project-with-pm2-nginx-and-yarn-on-ubuntu-18-04/
https://pm2.keymetrics.io/docs/usage/application-declaration/
Another idea would be passing all the env parameters of PM2 to the server via the following.
ecosystem.config.js being:
module.exports = {
apps: [{
name: "my-service-1",
script: "dist/server.js",
env_production: process.env, // pass all the env params of the container to node
}],
};
#webface
The environment variables in your example will not be available in Nextjs. To be available to both the client and server, variables must be prefixed with NEXT_PUBLIC (ie. NEXT_PUBLIC_API_VERSION).
These environment variables must be passed into the build process to be available at runtime.
I'm using Nightwatch.js and trying to run E2E tests using the programmatic API as described here.
Here is my nightwatch.json file:
{
"src_folders": ["tests"],
"webdriver": {
"start_process": true,
"server_path": "node_modules/.bin/chromedriver",
"port": 9515
},
"test_settings": {
"default": {
"desiredCapabilities": {
"browserName": "chrome"
}
}
}
}
and index.js script:
const Nightwatch = require('nightwatch');
Nightwatch.runTests(require('./nightwatch.json')).then(() => {
console.log('All tests has been passed!');
}).catch(err => {
console.log(err);
});
When I run the script I get the error:
Error: An error occurred while retrieving a new session: "Connection refused to 127.0.0.1:9515". If the Webdriver/Selenium service is managed by Nightwatch, check if "start_process" is set to "true".
I feel it needs some configuration but the documentation isn't very helpful here.
I'm trying to deploy my express server on Heroku which needs to connect to the remote MySQL database.
I used 'heroku config:add DATABASE_URL=mysql://dbusername:dbpassword#databasehostIP:databaseserverport/databasename with the correct information but still it tries to connect through wrong address.
I also used 'heroku config:add EXTERNAL_DATABASE_URL=mysql://dbusername:dbpassword#databasehostIP:databaseserverport/databasename with the correct information but still it tries to connect through wrong address.
In my Heroku app panel under 'setting' in 'Config Vars' section I see that DATABASE_URL and EXTERNAL_DATABASE_URL appeared with correct information. but in heroku log I still see the wrong information
This is my sequelize variable on the express server:
const sequelize = new Sequelize('dbName', 'USER', 'Password', {
host:"hostAddress",
dialect: 'mysql'
}
But I see the following on Heroku log:
2019-02-16T18:31:42.231390+00:00 app[web.1]: Unhandled rejection
SequelizeAccessDeniedError: Access denied for user
'USER'#'ec2-54-162-8-141.compute-1.amazonaws.com' (using
password: YES)
How can I change 'ec2-54-162-8-141.compute-1.amazonaws.com' to the remote MySQL host address?
Try setting your variable with something like this:
if (process.env.DATABASE_URL) {
const sequelize = new Sequelize(process.env.DATABASE_URL, {
define: {
freezeTableName: true, // don't make plural table names
underscored: true // don't use camel case
},
dialect: 'mysql',
dialectOptions: {
ssl: true
},
logging: true,
protocol: 'mysql',
quoteIdentifiers: false // set case-insensitive
});
} else {
console.log('Fatal error: DATABASE_URL not set');
process.exit(1);
}
This is my part of my config:
"selenium": {
"start_process": true, // tells nightwatch to start/stop the selenium process
"server_path": "./selenium-server-standalone-3.8.1.jar",
"host": "127.0.0.1",
"port": 4444, // standard selenium port
"cli_args": {
"webdriver.chrome.driver": "./chromedriver_win32/chromedriver.exe"
}
},
test_settings" : {
"default" : {
"use_ssl": true,
"credentials": {
"username": "admin",
"key": "secret"
},
The test was running fine until I inserted use_ssl. Now I am getting this error:
Error retrieving a new session from the selenium server
Connection refused! Is selenium server started?
{ Error: write EPROTO 101057795:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:openssl\ssl\s23_clnt.c:797:
at exports._errnoException (util.js:1020:11)
at WriteWrap.afterWrite (net.js:800:14) code: 'EPROTO', errno: 'EPROTO', syscall: 'write' }
I am trying to test a page which requires windows authentication.