NodeJS 如何解决./types打包的问题,错误:包子路径“./types”未由“exports”定义

w41d8nur  于 12个月前  发布在  Node.js
关注(0)|答案(1)|浏览(209)

我有一个错误:
错误:与序列化包连接的“exports”未定义包子路径“./types”。
(错误:D:\Projects\pets\realtime-chat\backend\node_modules\sequelize\package.json中的“exports”未定义包子路径“./types”)。
尝试通过将所有npm和node版本更新到最新版本来解决这个问题,但是没有成功。我已经为我的应用程序设置了socket.io服务器,并使用typescript创建了User模型。
当我尝试用User.create({\attributes\})创建一个新的User时,它会抛出上面提到的Error。

server.ts

import express from "express"
import { createServer } from "http"

import { Server } from "socket.io";

import apiRouter from "./routes/api.route";

import { seq } from "./models/db"

const app = express();

app.use("/api", apiRouter);
const httpServer = createServer(app);
const io = new Server(httpServer);

io.on("connection", (socket) => {
  console.log(socket.id);
});

httpServer.listen(process.env.PORT || 5000, () => {
  console.log(seq.config);
  seq.authenticate();
  
  console.log("Server is started")
});

字符串

User.model.ts

import { DataTypes, Model, Optional } from "sequelize/types";
import { seq } from "./db";

interface UserAttributes { 
  username: string,
  password: string,
  email: string
}

type UserCreationAttributes = Optional<UserAttributes, "email">;

class User extends Model<UserAttributes, UserCreationAttributes> {
  declare username: string;
  declare password: string;
  declare email: string
}

User.init({
  username: {
    type: DataTypes.STRING,
  },
  password: {
    type: DataTypes.STRING
  },
  email: {
    type: DataTypes.STRING
  }
},
{
  sequelize: seq,
  tableName: "users"
});

export default User;

我的API.route.ts

import express from "express"

import User from "../models/User";

const router = express.Router();

router.get("/users", async () => {
  await User.create({
    password: "name",
    username: "name"
  })
});

export default router;

zxlwwiss

zxlwwiss1#

只需直接从sequelize导入所有这些类型:

import { DataTypes, Model, Optional } from "sequelize";

字符串

相关问题