我知道这个问题在这里已经问了很多,但是通常的解决方法是添加
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "current"
}
}
]
]
}
到您的.babelrc,但是我已经这样做了,但仍然出现错误。
我的错误的完整堆栈跟踪为:
C:\...\node_modules\sequelize\lib\sequelize.js:417
this.importCache[importPath] = defineCall(this, DataTypes);
^
TypeError: Class constructor User cannot be invoked without 'new'
at Sequelize.import (C:\...\node_modules\sequelize\lib\sequelize.js:417:38)
at Object.<anonymous> (../../src/models/index.ts:22:9)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Module._compile (C:\...\node_modules\pirates\lib\index.js:99:24)
at Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Object.newLoader [as .js] (C:\...\node_modules\pirates\lib\index.js:104:7)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (../src/index.ts:9:1)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Module._compile (C:\...\node_modules\pirates\lib\index.js:99:24)
at Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Object.newLoader [as .js] (C:\...\node_modules\pirates\lib\index.js:104:7)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at Object.<anonymous> (C:\...\node_modules\@babel\node\lib\_babel-node.js:234:23)
at Module._compile (internal/modules/cjs/loader.js:701:30)
我的依赖项:
"devDependencies": {
"@babel/core": "^7.4.3",
"@babel/node": "^7.2.2",
"@babel/preset-env": "^7.4.3",
"@babel/register": "^7.4.0",
"@types/bcrypt": "^3.0.0",
"@types/bluebird": "^3.5.26",
"@types/cors": "^2.8.5",
"@types/dotenv": "^6.1.1",
"@types/express": "^4.16.1",
"@types/graphql": "^14.2.0",
"@types/graphql-resolvers": "^0.2.0",
"@types/ioredis": "^4.0.10",
"@types/jsonwebtoken": "^8.3.2",
"@types/node": "^11.13.7",
"@types/nodemailer": "^4.6.7",
"@types/pg": "^7.4.14",
"@types/validator": "^10.11.0",
"nodemon": "^1.18.11",
"typescript": "^3.4.5"
},
"dependencies": {
"apollo-server": "^2.4.8",
"apollo-server-express": "^2.4.8",
"bcrypt": "^3.0.6",
"cors": "^2.8.5",
"dotenv": "^7.0.0",
"express": "^4.16.4",
"graphql": "^14.2.1",
"graphql-redis-subscriptions": "^2.1.0",
"graphql-resolvers": "^0.3.2",
"ioredis": "^4.9.0",
"jsonwebtoken": "^8.5.1",
"nodemailer": "^6.1.0",
"pg": "^7.9.0",
"sequelize": "^5.5.0"
}
使用Node版本10.15.3(这是当前的LTS版本。由于它破坏了bcrypt软件包,因此我无法升级到当前版本12)。
我的打字稿配置:
{
"compilerOptions": {
"allowJs": true,
"allowSyntheticDefaultImports": true,
"jsx": "preserve",
"lib": ["dom", "es2017"],
"module": "esnext",
"moduleResolution": "node",
"noUnusedLocals": true,
"noUnusedParameters": true,
"preserveConstEnums": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"target": "esnext",
"outDir": "out"
}
}
以下是导致问题的代码行:
const models: IF_Models = {
User: sequelize.import("./user"),
Team: sequelize.import("./team")
};
export default models;
../ user通过以下方式导入:
class User extends Model {
public test!: string;
public userId!: number;
public orgId!: number;
public role!: string;
public name!: string;
public email!: string;
public password!: string | null;
public readonly createdAt!: Date;
public readonly updatedAt!: Date;
public static async findByLogin(email: string): Promise<User | null> {
return this.findOne({ where: { email } });
}
public generatePasswordHash!: () => Promise<string>;
public validatePassword!: (password: string) => Promise<boolean>;
}
User.init( ... )
User.prototype.generatePasswordHash( ... )
User.prototype.validatePassword( ... )
export default User
./ team是类似的格式。
如果我更改为"module": "commonjs"
并在没有babel的情况下运行该应用程序,则会收到相同的错误。
最初使我相信这是babel错误,但是我认为sequelize的import方法一定是错误,因为即使不使用babel,该错误仍然会发生。
关于我还能尝试什么的任何建议?