0

在 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"
  }
}

我的服务器.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.

    ################################################
  `);
});
4

2 回答 2

1

Prisma 还不支持 ES6 模块。我建议遵循这个请求,同时使用 CommonJS,即"type": "module"从你的移除并将inpackage.json设置为.targettsconfig.jsonES2018

于 2021-03-22T06:31:47.667 回答
0

如您所知,这里正在处理此问题。

同时,您可以做的是继续进行测试,将依赖注入与模拟的 prisma 客户端一起使用并移动解构线

const { PrismaClient } = pkg;

到您的类或函数在 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>();
于 2021-05-05T23:41:40.827 回答