Using couchbase database, I get this error in config/db.js:
var cluster = new _couchbase2.default.Cluster(_env2.default.database.cluster);
TypeError: Cannot read property 'cluster' of undefined
config/env/development.js:
const config = {
database: {
cluster: '...',
password: '',
bucket: '...'
}
}
export default config;
config/env/index.js:
const env = process.env.NODE_ENV || 'development';
const config = require(`./${env}.js`);
export default config;
config/db.js:
import config from './env';
const cluster = new couchbase.Cluster(config.database.cluster);
Try importing couchbase into your file: config/db.js
import config from './env';
import couchbase from 'couchbase'
const cluster = new couchbase.Cluster(config.database.cluster);
Related
basically, i am trying to make my app choose which database it's going to use. i'm having trouble, because i keep getting a "TypeError: Cannot read properties of undefined (reading 'max')". i've spent about three hours on this (very new to web stack stuff, so don't laugh at me please) and could really use some assistance. thanks in advance.
db.config.js.
it doesn't work with pool active either, so don't mind that.
require('dotenv').config();
module.exports = {
dev: {
HOST: "localhost",
USER: "postgres",
PASSWORD: "redacted",
DB: "testtest",
//dialect: "mysql"
dialect: "postgres"
}, // pool was here, removed it to make the whole thing smaller
production:{
// empty until i get actual production values from my hosting service
}
}
index.js
const env = process.env.NODE_ENV || 'dev';
const config = require("../config/db.config.js");
const Sequelize = require("sequelize");
const sequelize = new Sequelize(
config.DB,
config.USER,
config.PASSWORD,
{
host: config.HOST,
dialect: config.dialect,
operatorsAliases: false,
pool: {
// omitted to make example smaller
}
}
);
const db = {};
db.Sequelize = Sequelize;
db.sequelize = sequelize;
// ...models relationships defined here
module.exports = db;
to my understanding,
const env = process.env.NODE_ENV || 'dev';
means that if a value isn't present within my .env file (it isn't), it will default to 'dev' settings, and connect to my local database.
const config = require("../config/db.config.js");
this is here so the file can access those values.
i tried making an entirely separate file for environment configuration, but it gave me a sequelize error (that i can't pull up right now). this is what i had before:
const dbEngine = process.env.DB_ENVIRONMENT || 'dev';
const config = require('./db.config')[dbEngine];
module.exports = require('sequelize')(config);
Hello friends.
I need to continue my Nuxt JS work with SSL. However, after installation, I am getting the following error. I know the problem is because Node JS doesn't recognize the word "IMPORT". But I don't know how to solve the problem. Because I use Components as IMPORT all over the project. What is your suggestion?
Thank you very much in advance. 👋
package.json
"dev": "node server.js",
"nuxt": "^2.15.7",
"express": "^4.17.1"
ERROR IMAGE
error
SyntaxError: Cannot use import statement outside a module at compileFunction (<anonymous>)
nuxt.config.js
import axiosModule from './modules/axiosModule'
import momentModule from './modules/momentModule'
export default {
server: {
host: '0.0.0.0',
port: 3000,
},
......
server.js
const { Nuxt, Builder } = require('nuxt')
const expressServer = require('express')()
const thisHttp = require('http')
const thisHttps = require('https')
const thisFs = require('fs-extra')
const isProd = (process.env.NODE_ENV === 'production')
const isPort = 3000
let thisServer
if (isProd) {
const pKey = thisFs.readFileSync('./key.pem')
const pCert = thisFs.readFileSync('./cert.pem')
const httpsOptions = { key: pKey, cert: pCert }
thisServer = thisHttps.createServer(httpsOptions, expressServer)
} else {
thisServer = thisHttp.createServer(expressServer)
}
const nuxtConfig = require('./nuxt.config')
nuxtConfig.dev = !isProd
const nuxtServer = new Nuxt(nuxtConfig)
expressServer.use(nuxtServer.render)
const listen = () => { thisServer.listen(isPort, 'localhost') }
if (nuxtConfig.dev) {
new Builder(nuxtServer).build().then(listen()).catch(error => { console.log(error); process.exit(1) })
} else {
listen()
}
I fixed the situation manually. I did REQUIRE instead of IMPORT in Nuxt Config and used module.exports instead of Export Default. Even though I'm currently logging in via HTTPS, it's crossed out by Google Chrome.
I add fs to my nextjs project and received the following error:
Module not found: Can't resolve 'fs' in '/Users/neven/Development/next-js/node_modules/dotenv/lib'
I found that to resolve this issue, I should add config.node = { fs: 'empty' } to my next.config.js file.
The problem is that when I add that config param, dotenv plugin stops working, that is env variables are not loaded on client side.
This is my next.config.js file, which works without any issues.
const withCSS = require('#zeit/next-css')
const dotenv = require('dotenv')
const path = require('path')
const Dotenv = require('dotenv-webpack')
dotenv.config()
module.exports = withCSS({
webpack: config => {
config.plugins = config.plugins || []
config.plugins = [
...config.plugins,
// Read the .env file
new Dotenv({
path: path.join(__dirname, '.env'),
systemvars: true,
}),
]
return config
},
})
And then when I add fs: 'empty', it looks like this:
const withCSS = require('#zeit/next-css')
const dotenv = require('dotenv')
const path = require('path')
const Dotenv = require('dotenv-webpack')
dotenv.config()
module.exports = withCSS({
webpack: config => {
config.plugins = config.plugins || []
config.node = {
fs: 'empty'
}
config.plugins = [
...config.plugins,
// Read the .env file
new Dotenv({
path: path.join(__dirname, '.env'),
systemvars: true,
}),
]
return config
},
})
Do you have any suggestions on how I could work this thing out?
Let me know in case additional details are needed.
I found out what the issue was; dotenv plugin is working correctly, but I was trying to get the variables on client side, and that is not possible in this way.
The solution to use env variables on client side is to add env: { EXAMPLE: 'helloWorld' } to next.config.js file.
const withCSS = require('#zeit/next-css')
const dotenv = require('dotenv')
const path = require('path')
const Dotenv = require('dotenv-webpack')
dotenv.config()
module.exports = withCSS({
env: { EXAMPLE: 'helloWorld' },
webpack: config => {
config.plugins = config.plugins || []
config.node = {
fs: 'empty'
}
config.plugins = [
...config.plugins,
// Read the .env file
new Dotenv({
path: path.join(__dirname, '.env'),
systemvars: true,
}),
]
return config
},
})
The issue here is that your client-side can't access the environment variables.
Started NextJS 9.4, you can use .env* files to add your environment variables.
For your client-side to get access access to the environment variables, you just need to prefix them with NEXT_PUBLIC_
NEXT_PUBLIC_YOUR_KEY="keykeykey"
These can be accessible with:
process.env.NEXT_PUBLIC_YOUR_KEY
I need to run third party application in node.js environment but Sequelize throws 'Dialect needs to be explicitly supplied as of v4.0.0'
I've found similar topic here Dialect needs to be explicitly supplied as of v4.0.0 but 'export NODE_ENV=development' doesn't work and I can not find Sequelize config file.
How can I fix this error?
Here is code:
const Sequelize = require('sequelize');
const scheme = require('./scheme');
const Op = Sequelize.Op;
const sequelize = new Sequelize(null, null, {
dialect: 'sqlite',
storage: 'db.sqlite3',
operatorsAliases: { $and: Op.and },
logging: false
});
scheme(sequelize);
sequelize.sync();
module.exports.sequelize = sequelize;
module.exports.models = sequelize.models;
You simply supply the dialect when you initialize sequelize;
const sequelize = new Sequelize('database', 'username', 'password', {
host: 'localhost',
dialect: // pick one of 'mysql','sqlite','postgres','mssql',
});
Node cannot find your environment to load in the config file.
You can easily fix by running this
export NODE_ENV=development; npx sequelize db:migrate
This should export to NODE_ENV the environment needed to run it.
this worked for me:
'use strict';
const path = require('path');
const Sequelize = require('sequelize');
const db = {};
const DB = 'users';
const USER = 'user';
const PASSWORD = 'password';
const HOST = 'host';
const DIALECT = 'postgres';
const PORT = 5432;
const CONNECTION = new Sequelize(
DB,
USER,
PASSWORD,
{
host: HOST,
dialect: DIALECT,
port: PORT,
}
)
module.exports.CONNECTION = CONNECTION;
I think you already have solved the problem. But I faced the same issue.
My issue had happened due to the name change of the config file that is auto generated by the sequelize-cli. So what I eventually did was, I created a .sequelizerc file in the project root folder and include following content.
const path = require('path');
module.exports = {
'config': path.resolve('config', 'database.json'),
'models-path': path.resolve('db', 'models'),
'seeders-path': path.resolve('db', 'seeders'),
'migrations-path': path.resolve('db', 'migrations')
};
Make sure to change the arguments according to your folder structure of the project.
A comprehensive explanation has on the sequelize documentation page.
https://sequelize.org/master/manual/migrations.html
I want to create a simple config file with one object, for example:
const config = {
serverIp: "localhost",
serverPort: 3000
}
which I can use in both Angular (Angular-cli with typescript):
import { config } from '../pathToConfigFile';
and node.js server (javascript):
const config = require("./config");
If I do this (config has .js extension):
export const config = {
ipOrDomainName: "localhost",
serverPort: 3000
}
if(module && module.exports){
module.exports = config;
}
I get this error on server
SyntaxError: Unexpected token export
And in typescript I get this error
Module '../../../../../config.js' was resolved to
'C:/Users/(...)/config.js', but '--allowJs'
is not set.
How can I get this module to be working in both Angular and node?