Search code examples
typescript

Cannot find module 'ts-transformer-keys'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?


I want to use ts-transformer-keys in my typescript project "typescript": "^4.5.5". So I added this dependency to the typescript package.json:

"ts-transformer-keys": "^0.4.3",

and added this to tsconfig.json like this:

"plugins": [
      { "transform": "ts-transformer-keys/transformer" }
] 

but when I import the ts-transformer-keys in the current project:

import { keys } from 'ts-transformer-keys';

shows error:

Cannot find module 'ts-transformer-keys'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?ts(2792)

Why did this happen? What should I do to fix this problem? I have already run the yarn command. The node version is 16.13.2.


Solution

  • this error message tell how to fix this problem, the typescript default module resolution is classic, this would not search from node_modules. That why tell you did not found the module that in the node_modules folder. more info you can read from here: https://www.typescriptlang.org/docs/handbook/module-resolution.html, so fix your problem, try add "moduleResolution": "node" in your "tsconfig.json" (or "jsconfig.json") file:

    "compilerOptions": {
        //...
        "moduleResolution": "node",
    }