Liam_1998
Liam_1998

Reputation: 1477

tsx cannot find module 'xxx'

I am using yarn v2 to install the dependency package, and using yarn start command to start the project smoothly, but vscode always reminds me that I can't find any local modules.

enter image description here

And here is my tsconfig.json file:

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": false,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react"
  },
  "include": [
    "src"
  ]
}

Here is my folder structure:

enter image description here

Upvotes: 5

Views: 4031

Answers (2)

icc97
icc97

Reputation: 12793

What the default Nest.js installation nest new project-name does is uses yarn's nodeLinker by creating a .yarnrc.yml in the project root:

nodeLinker: node-modules

You can add this file to an existing yarn project, then run yarn and it will generate a node_modules directory that will make VSCode happy again.

Upvotes: 0

Viktor Vlasenko
Viktor Vlasenko

Reputation: 2512

You can use @yarnpkg/sdks package (which is a part of Yarn 2+)

You can enable PnP support in VS Code via:
yarn dlx @yarnpkg/sdks vscode
This will generate tssdk and modifies your .vscode/settings.json to add TypeScript compiler-wrapper inside tssdk as a Workspace TypeScript compiler. You should run VS Code, open any TypeScript file and in the bottom right side of the window click on TypeScript version. Select Use Workspace Version from dropdown menu to actually use Workspace Compiler, its version has the suffix -sdk.

You can also read Yarn 2+ docs regarding VSCode integration here:
https://yarnpkg.com/getting-started/editor-sdks#vscode

Upvotes: 9

Related Questions