2

I am using convictjs to manage the configuration files for my nodejs application. The file that has the configuration schema is called "config.js" and it is in the "config" folder.

I am using es6 import and export statements

import convict from "convict";
import dotenv from "dotenv";
import configDev from "./config.dev";
import configProd from "./config.prod";
import configStage from "./config.stage";
.
.
.
export default config.getProperties();

These configuration files contain database credentials and other necessary database information, which I use in the "index.js" for the sequelize models.

Note: Early on in the development process we tried using es6 with sequelize ran into other problems so we opted to using es5 for models, migrations and seeds.

Currently whenever I try to execute migrations, seuqlize throws an error as shown below

ERROR: Error reading "config\config.js". Error: SyntaxError: Unexpected token 
import

Babel is setup properly, I can transpile and run the application without any issues except for database migrations. Any help to resolve this issue is much appreciated.

1

1 Answer 1

3

As far as I know Node.js does not support this syntax yet so you should either use require or transpile your code with babel before the execution.

UPD: seems like it's possible to enable ES6 modules in Node.js starting from v10.12.0 https://nodejs.org/api/esm.html

node --experimental-modules my-app.mjs
Sign up to request clarification or add additional context in comments.

11 Comments

I am traspiling my code, the whole application aside from the sequelize scripts is written in es6.
My npm script is this, "babel-node --presets env ./app.js --nodeEnv dev" this transpiles the code and starts the application
@KaanYalti then, for some reason, this module is not transpiled. Anyway I would not recommend using ES6 modules in node js for now.
@KaanYalti you are saying that the problem occurs when you try to execute migrations. How are you executing them?
Thanks a lot, I already switched to common js in my config file. That resolved the issue
|

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.