egg typescript模板下使用 egg-sequelize 下就报错。
发布于 6 年前 作者 a304885433 5977 次浏览 来自 问答

import { Application } from 'egg'
//import 'egg-sequelize'

export default (app: Application) => {
    // @ts-ignore
    const { STRING, UUID, UUIDV4, INTEGER, DECIMAL } = app.Sequelize;

    // @ts-ignore
    const model = app.model.define('bak', {
        id: {
            type: UUID,
            defauleValue: UUIDV4,
            primaryKey: true,
        },
    }, {
            comment: '备份表',
        });

    return model;
};

其实本质上只想加载 sequelize 后,就可以正常使用 app.model 和 app.sequelize 了, 但是不知道为什么一直报错。
错误现象:

sequelize deprecated String based operators are now deprecated. Please use Symbol based operators for better security, read more at http://docs.sequelizejs.com/manual/tutorial/querying.html#operators node_modules/sequelize/lib/sequelize.js:242:13
Error: [egg-core] load file: /showcase/app/model/bak.ts, error: Cannot find module 'egg-sequelize'
11 回复

你没安装依赖吧

@atian25 肯定是安装了的呀,我只要屏蔽 import 'egg-sequelize’就好了,然后在相关方法,指定 ts-ingore ,屏蔽掉 ts编译错误就好了

用的就是showcase的模板,然后安装了相关插件。

给个 repo 看看

https://gitee.com/a304885433/egg-issue.git

传上来了,主要看 app/model/bak.ts

@whxaxes 只要打开这个注释,就会报错。//import ‘egg-sequelize’

@a304885433 你在 tsconfig.json 中加上这个配置 "allowSyntheticDefaultImports": true 应该就可以了,这是一个类型声明的 bug,已经有人提了 PR 了 https://github.com/eggjs/egg-sequelize/pull/55 ,但是还没 merged

image.png

修改了仍然是报错了,尝试过重启vscode。。。中间有一次不知道怎么好了,但是重新打开后,这个错误还在报。 预计那个PR什么时候合并,我可以等它合并好了,在更新试试。

sequelize deprecated String based operators are now deprecated. Please use Symbol based operators for better security, read more at http://docs.sequelizejs.com/manual/tutorial/querying.html#operators node_modules/sequelize/lib/sequelize.js:242:13
Error: [egg-core] load file: /egg-issue/app/model/bak.ts, error: Cannot find module 'egg-sequelize'

@a304885433 找到原因了。因为 egg-sequelize 不属于常规的 npm 模块,没有 main 入口,所以 import 被转成 js 的时候会变成 require,然后就会去加载该模块的 main 入口,导致报错,所以不能这么引入。你将 import 'egg-sequelize' 这段代码加到 typings/index.d.ts 下就可以解决了。

还有一种解决方案:其实这个是不需要你手动加 import 的,egg-ts-helper 会自动生成 import plugin.js 中配置的那些插件的 d.ts,但是由于刚好里面有个 bug 导致没生成出来,我刚才已经修复了,你把 egg-ts-helper 升级到 1.9.1 ,删掉 import 'egg-sequelize' 这一行也可以解决这个问题

@whxaxes 谢谢大神。采用了升级egg-ts-helper1.9.1版本处理这个问题了。 验证ok,不需要import插件库了。可以继续验证ts升级老项目了。。。

回到顶部