使用带有 typescript 的 next.js 导入节点模块失败

问题描述 投票:0回答:1

我正在尝试使用一个包,brypt,我已经使用 npm install bcrypt 安装在项目中。

我可以在 node_modules 文件夹中看到该包,但是当尝试在我的代码中导入时,我收到错误: “找不到模块‘bcryptjs’或其相应的类型声明。

我尝试修改 tsconfig.json 中的 moduleResolution 值,因为我认为这可能是我的问题,但仍然是一个问题。任何人都可以看到/指导我,关于我在这里做错了什么吗?

这是我的 tsconfig.json 文件的内容:

{
  "compilerOptions": {
    "target": "ESNext",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "bundler",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "plugins": [
      {
        "name": "next"
      }
    ],
    "paths": {
      "@/*": [
        "./*"
      ]
    }
  },
  "include": [
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx",
    ".next/types/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}

这就是我尝试使用方法的地方:

import { IUserAuthenticatedResponse } from "@/types/userAccountTypes";
import axios from "axios";
import bcrypt from 'bcryptjs';

export const dynamic = 'force-dynamic' // defaults to auto
export async function POST(request: Request) {
    const body = await request.json();
    const { emailAddress, password } = body;

    async function authenticateUser(cancel = false): Promise<IUserAuthenticatedResponse[]> {
        const salt = bcrypt.genSaltSync(10)
        const response = await api.request({
            url: process.env.apiBaseUrl + 'authenticateUserAccount/' + emailAddress + "/" + password,
            method: "GET",
            // retrieving the signal value by using the property name
            signal: cancel ? cancelApiObject[this.get.name].handleRequestCancellation().signal : undefined,
        })
        return response.data
    }
}

感谢您提供的任何帮助。

typescript next.js npm-install typescript-module-resolution
1个回答
0
投票

不知道为什么,但似乎我必须使用以下方式安装:

npm i --save-dev @types/bcrypt
© www.soinside.com 2019 - 2024. All rights reserved.