1
"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"
0

3 Answers 3

2

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;
Sign up to request clarification or add additional context in comments.

2 Comments

yes I have given the same solution.. I realised you cannot export anything from inside if else statement
Would appreciate it if you marked this answer as the solution in that case. Thank you :)
0

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.

Comments

0

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;
}

2 Comments

i did and now getting this error export { pKeys }; SyntaxError: Unexpected token 'export'
which version of node are you using?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.