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.

Kaan Yalti
  • 113
  • 2
  • 9
  • Possible duplicate of [Node error: SyntaxError: Unexpected token import](https://stackoverflow.com/questions/37634198/node-error-syntaxerror-unexpected-token-import) – Dez Oct 11 '18 at 15:09

1 Answers1

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
Eugene Tsakh
  • 2,777
  • 2
  • 14
  • 27
  • I am traspiling my code, the whole application aside from the sequelize scripts is written in es6. – Kaan Yalti Oct 11 '18 at 15:09
  • My npm script is this, "babel-node --presets env ./app.js --nodeEnv dev" this transpiles the code and starts the application – Kaan Yalti Oct 11 '18 at 15:10
  • @KaanYalti then, for some reason, this module is not transpiled. Anyway I would not recommend using ES6 modules in node js for now. – Eugene Tsakh Oct 11 '18 at 15:11
  • @KaanYalti you are saying that the problem occurs when you try to execute migrations. How are you executing them? – Eugene Tsakh Oct 11 '18 at 15:12
  • Through a npm script that basically executes this "sequelize db:migrate:undo:all". – Kaan Yalti Oct 11 '18 at 15:16
  • @KaanYalti seems like here sequelize is importing your config file without transpiling. – Eugene Tsakh Oct 11 '18 at 15:22
  • I think that is the problem; however, I don't know how to fix it. I am currently looking into modifying the npm scipt I mentioned above to call babel-node before executing the migrations, but so far no success – Kaan Yalti Oct 11 '18 at 15:25
  • @KaanYalti you can use require in your `config.js` (it should work as expected with ES6 modules as well) or you could precompile your project and point sequelize to your dist folder – Eugene Tsakh Oct 11 '18 at 16:40
  • 1
    Thanks a lot, I already switched to common js in my config file. That resolved the issue – Kaan Yalti Oct 11 '18 at 16:55
  • hi @KaanYalti can you how you to config your js for import and export witth Sequalize ?? i always failed , – koplweou Feb 21 '19 at 11:49
  • @koplweou We actually ended up converting the whole application to typescript before I was able to fix this issue. But What you can do is run the sequelize scripts through babel and then execute the migrations and seeds; so transpile them, and then run them – Kaan Yalti Feb 22 '19 at 16:23