Cannot find module 'joi' or its corresponding type declarations
- User
Cannot find module 'joi' or its corresponding type declarations
- Assistant
The error message:
codeCannot find module 'joi' or its corresponding type declarationsmeans that your TypeScript compiler (or Node.js) cannot find the
joipackage or its types. Here's how to fix this issue step-by-step:
1. Install
joipackageMake sure you have installed the
joipackage in your project:bashnpm install joi # or yarn add joi
2. Install TypeScript types for
joijoihas its type declarations included (since v16+), so you usually don't need additional types. However, if you are using an older version, try installing the types:bashnpm install --save-dev @types/hapi__joi # or yarn add -D @types/hapi__joiImportant: The
@typespackage is named differently becausejoioriginally was under hapi's namespace. However, this is usually unnecessary if you are using the latestjoi.
3. Check your import statement
From
joiv17+, the import should be:tsimport Joi from 'joi';If you are doing:
tsimport * as Joi from 'joi';or
tsconst Joi = require('joi');make sure your
tsconfig.jsonsettings support those, or change to the default import style.
4. Check your
tsconfig.jsonMake sure your
tsconfig.jsonhas:json{ "compilerOptions": { "moduleResolution": "node", "esModuleInterop": true, ... } }esModuleInteropenables default imports to work smoothly.moduleResolutionasnodeensures correct resolution of modules.
5. Delete and reinstall node_modules