Error importing PrismaClient in code compiled from typescript [SyntaxError: Named export 'PrismaClient' not found]
Error importing PrismaClient in code compiled from typescript [SyntaxError: Named export 'PrismaClient' not found]
在 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 的打字稿文件抛出这个错误:
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
}
}
我的package.json:
{
"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"
}
}
我的server.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.
################################################
`);
});
Prisma 还不支持 ES6 模块。我建议遵循 this 请求并同时使用 CommonJS,即从 package.json
中删除 "type": "module"
并将 tsconfig.json
中的 target
设置为 ES2018
.
如您所知,此问题正在 here 中处理。
与此同时,你可以做些什么来继续你的测试是使用模拟 prisma 客户端的依赖注入并移动解构线
const { PrismaClient } = pkg;
到您的 class 或函数在 if 中使用它的位置,即:
class MyClass {
prisma: Prisma.PrismaClient
def constructor(props) {
if (!props?.prisma) {
const { PrismaClient } = Prisma
this.prisma = new PrismaClient({
log: ['error']
})
} else {
this.prisma = props.prisma
}
}
}
我知道它并不理想,但希望它能解决问题。
要模拟 PrismaClient,您可以像这样使用 jest-mock-extended 来模拟它
const mockPrisma = mockDeep<OriginalPrismaClient>();
在 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 的打字稿文件抛出这个错误:
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
}
}
我的package.json:
{
"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"
}
}
我的server.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.
################################################
`);
});
Prisma 还不支持 ES6 模块。我建议遵循 this 请求并同时使用 CommonJS,即从 package.json
中删除 "type": "module"
并将 tsconfig.json
中的 target
设置为 ES2018
.
如您所知,此问题正在 here 中处理。
与此同时,你可以做些什么来继续你的测试是使用模拟 prisma 客户端的依赖注入并移动解构线
const { PrismaClient } = pkg;
到您的 class 或函数在 if 中使用它的位置,即:
class MyClass {
prisma: Prisma.PrismaClient
def constructor(props) {
if (!props?.prisma) {
const { PrismaClient } = Prisma
this.prisma = new PrismaClient({
log: ['error']
})
} else {
this.prisma = props.prisma
}
}
}
我知道它并不理想,但希望它能解决问题。
要模拟 PrismaClient,您可以像这样使用 jest-mock-extended 来模拟它
const mockPrisma = mockDeep<OriginalPrismaClient>();