在server.ts中,导入PrismaClient,如下所示:
import { PrismaClient } from '@prisma/client';
export const prisma = new PrismaClient();
使用tsc构建并运行编译的代码时抛出错误:
yarn run build && yarn run start
import { PrismaClient } from '@prisma/client';
^^^^^^^^^^^^
SyntaxError: Named export 'PrismaClient' not found. The requested module '@prisma/client' 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 '@prisma/client';
const { PrismaClient } = pkg;
at ModuleJob._instantiate (internal/modules/esm/module_job.js:104:21)
at async ModuleJob.run (internal/modules/esm/module_job.js:149:5)
at async Loader.import (internal/modules/esm/loader.js:166:24)
at async Object.loadESM (internal/process/esm_loader.js:68:5)
error Command failed with exit code 1.
所以我做了建议的事情,把代码改为:
import Prisma from '@prisma/client';
const { PrismaClient } = Prisma;
export const prisma = new PrismaClient();
在用tsc编译并运行生成的代码之后,这段代码就可以工作了。但是现在用ts-node-dev运行typescript文件会抛出这个错误:
yarn run dev
TypeError: Cannot destructure property 'PrismaClient' of 'client_1.default' as it is undefined.
at Object.<anonymous> (C:\Users\gfs10\Projetos\rest-api\src\server.ts:11:9)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Module._compile (C:\Users\gfs10\Projetos\rest-api\node_modules\source-map-support\source-map-support.js:547:25)
at Module.m._compile (C:\Users\gfs10\AppData\Local\Temp\ts-node-dev-hook-9753341767331849.js:69:33)
at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at require.extensions.<computed> (C:\Users\gfs10\AppData\Local\Temp\ts-node-dev-hook-9753341767331849.js:71:20)
at Object.nodeDevHook [as .ts] (C:\Users\gfs10\Projetos\rest-api\node_modules\ts-node-dev\lib\hook.js:63:13)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
[ERROR] 19:03:38 TypeError: Cannot destructure property 'PrismaClient' of 'client_1.default' as it is undefined.
并将代码更改为:
import Prisma from '@prisma/client';
export const prisma = new Prisma.PrismaClient();
引发此错误:
yarn run dev
TypeError: Cannot read property 'PrismaClient' of undefined
at Object.<anonymous> (C:\Users\gfs10\Projetos\rest-api\src\server.ts:11:34)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Module._compile (C:\Users\gfs10\Projetos\rest-api\node_modules\source-map-support\source-map-support.js:547:25)
at Module.m._compile (C:\Users\gfs10\AppData\Local\Temp\ts-node-dev-hook-7015369495927739.js:69:33)
at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at require.extensions.<computed> (C:\Users\gfs10\AppData\Local\Temp\ts-node-dev-hook-7015369495927739.js:71:20)
at Object.nodeDevHook [as .ts] (C:\Users\gfs10\Projetos\rest-api\node_modules\ts-node-dev\lib\hook.js:63:13)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
[ERROR] 19:09:01 TypeError: Cannot read property 'PrismaClient' of undefined
怎么会呢?我怎么能让两者同时工作呢?
我的tsconfig.json
{
"compilerOptions": {
"target": "ES2020",
"moduleResolution": "node",
"outDir": "./build/",
"rootDir": "./src/",
"strict": true,
"alwaysStrict": true,
"noImplicitAny": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"strictNullChecks": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noFallthroughCasesInSwitch": true,
"esModuleInterop": true,
"declaration": true
}
}
我的包参:
{
"name": "rest-api",
"version": "1.0.0",
"description": "A REST API boilerplate.",
"main": "server.ts",
"author": "Gledyson Ferreira",
"license": "MIT",
"type": "module",
"scripts": {
"start": "node --experimental-specifier-resolution=node build/server.js",
"dev": "SET NODE_ENV=development&& ts-node-dev --clear src/server.ts",
"build": "tsc",
"lint": "eslint --ext .ts ."
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^4.18.0",
"@typescript-eslint/parser": "^4.18.0",
"eslint": "^7.22.0",
"prisma": "^2.19.0",
"ts-node-dev": "^1.1.6",
"typescript": "^4.2.3"
},
"dependencies": {
"@prisma/client": "^2.19.0",
"@types/bcrypt": "^3.0.0",
"@types/cors": "^2.8.10",
"@types/express": "^4.17.11",
"@types/jsonwebtoken": "^8.5.1",
"@types/morgan": "^1.9.2",
"@types/node": "^14.14.35",
"@types/passport": "^1.0.6",
"@types/passport-jwt": "^3.0.5",
"bcrypt": "^5.0.1",
"cors": "^2.8.5",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"morgan": "^1.10.0",
"passport": "^0.4.1",
"passport-jwt": "^4.0.0"
}
}
我的服务器.ts
import express from 'express';
import Prisma from '@prisma/client';
import config from './config';
import api from './api';
import middleware from './middlewares';
import morgan from 'morgan';
import cors from 'cors';
import passport from 'passport';
import setReqUser from './services/passport';
export const prisma = new Prisma.PrismaClient();
const app = express();
app.disable('x-powered-by');
app.use(express.json());
app.use(morgan('tiny'));
app.use(cors());
app.use(passport.initialize());
setReqUser(passport);
app.use('/api/v1', api.authRoute);
app.use('/api/v1/users', api.userRoute);
app.use(middleware.unknownEndpoint);
app.use(middleware.errorHandler);
app.listen(config.port, () => {
console.log(`
################################################
Server running on port ${config.port} in ${config.env} mode.
################################################
`);
});
3条答案
按热度按时间nhhxz33t1#
Prisma还不支持ES6模块。我建议按照this请求,同时使用CommonJS,即从您的
package.json
中删除"type": "module"
,并将tsconfig.json
中的target
设置为ES2018
。gpnt7bae2#
如您所知,此问题已在here上解决。
同时,您可以对模拟的prisma客户端使用依赖项注入,并移动解构线,以继续进行测试
返回到类或函数在if中使用它的位置,即:
我知道这不是很理想,但希望这能起到作用。
要模拟PrismaClient,您可以使用jest-mock-extended模拟它,如下所示
gc0ot86w3#
如果你刚刚克隆了这个项目,那么很可能你还没有生成prisma客户端。我已经运行了
npx prisma generate
,它似乎修复了这个问题。