Reputation: 223
"node": "14.16.0",
"npm": "6.14.11"
I have 3 js files,
dev.js
const keys = {
googleClientID: 'creds',
googleClientSecret: 'creds',
mongoURI: 'creds',
cookieKey: 'creds'
}
export { keys };
prod.js
const pKeys = {
googleClientID: process.env.GOOGLE_CLIENT_ID,
googleClientSecret: process.env.GOOGLE_CLIENT_SECRET,
mongoURI: process.env.MONGO_URI,
cookieKey: process.env.COOKIE_KEY
}
export { pkeys };
key.js
if(process.env.NODE_ENV === 'production'){
export { pKeys } from './prod.js'
} else{
export { keys } from './dev.js';
}
when I do this i am getting this error:
file:///home/vaibhav/Documents/email-app/email-server/config/key.js:2
export { pKeys } from './prod.js'
^^^^^^
SyntaxError: Unexpected token 'export'
where am I going wrong? because according to this doc by MDN if you go down to examples and 'export from' I did the same thing. Do let me know if I need to provide any other information, because i am not very strong at javascrpt. Also inside package.json i have set:
"type": "module"
Upvotes: 1
Views: 3160
Reputation: 1073
You cannot use export inside if statements in node.js, but the following code should do what you are trying to achieve:
import { keys as devKeys } from './dev.js';
import { pKeys as prodKeys } from './prod.js';
export const keys = process.env.NODE_ENV === 'production' ? null : devKeys;
export const pKeys = process.env.NODE_ENV === 'production' ? prodKeys : null;
Upvotes: 2
Reputation: 508
from my understanding, you are re-exporting the keys from key.js.
try this in key.js:
import { pKeys as importedPKeys } from './prod.js'; // I think you had a typo for pKeys in prod.js export
import { keys as importedKeys } from './dev.js';
export let keys;
export let pKeys;
if(process.env.NODE_ENV === 'production'){
pKeys = importedPKeys;
} else{
keys = importedKeys;
}
Upvotes: 0
Reputation: 223
okay so according to this thread what I have learnt is that you cannot export inside if else statement MAY BE, I AM NOT SURE. But the solution to my problem is there and it is done in a slightly different way.
Upvotes: 0