简体   繁体   English

无法将es6导入和导出与sequ​​elize一起使用

[英]Cannot use es6 import and export with sequelize

I am using convictjs to manage the configuration files for my nodejs application. 我正在使用convictjs来管理我的nodejs应用程序的配置文件。 The file that has the configuration schema is called "config.js" and it is in the "config" folder. 具有配置架构的文件称为“ config.js”,位于“ config”文件夹中。

I am using es6 import and export statements 我正在使用es6导入和导出语句

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. 这些配置文件包含数据库凭证和其他必要的数据库信息,我在“ index.js”中使用了这些序列模型。

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. 注意:在开发过程的早期,我们尝试将es6与sequ​​elize一起使用时遇到了其他问题,因此我们选择将es5用于模型,迁移和种子。

Currently whenever I try to execute migrations, seuqlize throws an error as shown below 目前,每当我尝试执行迁移时,seuqlize都会引发错误,如下所示

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. Babel设置正确,除了数据库迁移外,我可以移植和运行该应用程序而没有任何问题。 Any help to resolve this issue is much appreciated. 非常感谢您解决此问题的任何帮助。

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. 据我所知,Node.js目前还不支持这种语法,因此您应该在执行之前使用require或使用babel转换代码。

UPD: seems like it's possible to enable ES6 modules in Node.js starting from v10.12.0 https://nodejs.org/api/esm.html UPD:似乎可以从v10.12.0 https://nodejs.org/api/esm.html开始在Node.js中启用ES6模块

node --experimental-modules my-app.mjs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM