我正试图在NodeJS上运行sequelize。我正在用打字本编写代码,我正在使用的软件版本如下:
NodeJS-14.16.0续写- 6.5.1打字本- 4.2.3
类型记录的构建是正确的,但是当对编译的index.js文件运行节点命令时,我会得到以下错误:
import { DataTypes, Model, Sequelize, TableHints } from 'sequelize';
^^^^^
SyntaxError: Named export 'Model' not found. The requested module 'sequelize' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from 'sequelize';
const { DataTypes, Model, Sequelize, TableHints } = pkg;代码是一个简单的index.ts
import { DataTypes, Model, Optional, Sequelize, TableHints } from 'sequelize';
let server = "server";
let database = "db";
let user_name = "user";
let password = "password";
let app_name = "api";
let _mssql_service = new Sequelize(database, user_name, password, {
dialect: "mssql",
isolationLevel: TableHints.READUNCOMMITTED,
host: server,
dialectOptions: {
options: {
appName: app_name,
},
},
});
// These are all the attributes in the User model
export interface TrackingAttributes {
id: number;
column1: string;
...
}
interface TrackingCreationAttributes
extends Optional<TrackingAttributes, "id"> {}
export class TrackingModel
extends Model<TrackingAttributes, TrackingCreationAttributes>
implements TrackingAttributes {
public id!: number; // Note that the `null assertion` `!` is required in strict mode.
public column1!: string;
...
}
async function test() {
try {
TrackingModel.init(
{
id: {
type: DataTypes.INTEGER.UNSIGNED,
autoIncrement: true,
primaryKey: true,
},
column1: {
type: DataTypes.STRING(10),
allowNull: true
},
},
{
tableName: "table1",
schema: "schema1",
sequelize: _mssql_service, // passing the `sequelize` instance is required
}
);
let val = await TrackingModel.findOne({ where: { id: 24 } });
console.log("Connection has been established successfully.");
console.log(val);
} catch (error) {
console.error("Unable to connect to the database:", error);
} finally {
_mssql_service.close();
}
}
test();我已经安装并包含在package.json中的下列类型:
“@类型/节点”:"^14.14.35“、”@类型/续订“:"^4.28.9”、“@类型/验证器”:"^13.1.3“
我有下面的tsconfig.json
{
"compilerOptions": {
"target": "es2020",
"module": "esnext",
"esModuleInterop": true,
"moduleResolution": "node",
"strict": true,
"declaration": true,
"outDir": "bin",
"types": ["node"],
"typeRoots": ["../node_modules/@types"],
"sourceMap": true
},
"include": ["**/*.ts"],
"exclude": ["tests", "dist", "node_modules", "lib", "bin"]
}我有下面的package.json
{
"name": "console-application",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"sequelize": "^6.5.1"
},
"devDependencies": {
"@types/node": "^14.14.35",
"@types/validator": "^13.1.3"
}
}我很困惑为什么我会得到这个错误,但任何帮助都将不胜感激。
发布于 2021-03-17 21:21:39
通常,@types/*包与它们提供类型的包的主要版本相匹配。在这种情况下,您有sequelize,v6,但是@types/sequelize,v4,所以它们不匹配。
我看了一下后缀回购,发现它们自v5以来就提供了自己的类型,所以您应该卸载@types/sequelize,因为它们是不需要的,也是引发问题的原因。
请看这里我找到的信息- https://sequelize.org/master/manual/typescript.html
编辑:在OP更新他们的帖子后,关于tsconfig.json/package.json问题的更多信息,包括它。
首先,您需要在依赖项列表中包括typescript。您可以简单地运行npm i typescript (如果需要,也可以使用特定的版本)。
然后,tsconfig.json的问题是,您已经将module编译器选项设置为esnext,但Node不支持这一点(Node确实通过--experimental-modules标志对此进行了实验支持,但我暂时忽略了这一点)。您应该将其设置为commonjs。
工作tsconfig.json
{
"compilerOptions": {
"target": "es2020",
"module": "commonjs",
"esModuleInterop": true,
"moduleResolution": "node",
"strict": true,
"declaration": true,
"outDir": "bin",
"types": ["node"],
"typeRoots": ["../node_modules/@types"],
"sourceMap": true
},
"include": ["**/*.ts"],
"exclude": ["tests", "dist", "node_modules", "lib", "bin"]
}这样,我就能够编译和运行代码了,尽管我现在得到了一个单独的错误,即Error: Please install tedious package manually和sequelize --但这只是一个依赖问题。
https://stackoverflow.com/questions/66666042
复制相似问题