最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot find TypeScript Modules - Stack Overflow

programmeradmin2浏览0评论

In a very large Mixed TS/JS project, using VSCode, I get the following errror.

Cannot find module 'shared/common/enums/httpMethod'.ts(2307)

The app builds successfully, and our pre-commit hook, which runs eslint and tests isn't failing.

This happens in all .ts files that I import .ts files. I found a couple of answers on the matter, but none helped.

What I did so far includes:

  1. Change the workspace version of TypeScript in VSCode from the bottom Status Bar in the Version.
  2. Link th evscode.d.ts file from the node_modules with this command ln -s /home/work/mymodule/node_modules/vscode.d.ts /usr/share/code/resources/app/out/vs/vscode.d.ts
  3. NPM Install, even remove the node_modules and restarted the VSCode

Here is the .tsconfig file, that we use. Occasionally on the team, other members get this error, but after npm install and restarting VSCode they are just fine.

{
  "compilerOptions": {
    "baseUrl": "./src",
    "jsx": "react",
    // Target latest version of ECMAScript.
    "target": "esnext",
    // Search under node_modules for non-relative imports.
    "moduleResolution": "node",
    // Process & infer types from .js files.
    "allowJs": true,
    // Don't emit; allow Babel to transform files.
    "noEmit": true,
    // Enable strictest settings like strictNullChecks & noImplicitAny.
    "strict": true,
    // Disallow features that require cross-file information for emit.
    "isolatedModules": true,
    // Import non-ES modules as default imports.
    "esModuleInterop": true,
    "skipLibCheck": true
  },
  // Import custom typings
  "include": ["src/shared/**/*", "./typings"]
}

This is not a problem with .tsconfig. I researched it, and we have the correct settings. Just putting it out there, for more info on the matter. If someone has the same problem as me, please post the answer or a link to it. Thanks!!

In a very large Mixed TS/JS project, using VSCode, I get the following errror.

Cannot find module 'shared/common/enums/httpMethod'.ts(2307)

The app builds successfully, and our pre-commit hook, which runs eslint and tests isn't failing.

This happens in all .ts files that I import .ts files. I found a couple of answers on the matter, but none helped.

What I did so far includes:

  1. Change the workspace version of TypeScript in VSCode from the bottom Status Bar in the Version.
  2. Link th evscode.d.ts file from the node_modules with this command ln -s /home/work/mymodule/node_modules/vscode.d.ts /usr/share/code/resources/app/out/vs/vscode.d.ts
  3. NPM Install, even remove the node_modules and restarted the VSCode

Here is the .tsconfig file, that we use. Occasionally on the team, other members get this error, but after npm install and restarting VSCode they are just fine.

{
  "compilerOptions": {
    "baseUrl": "./src",
    "jsx": "react",
    // Target latest version of ECMAScript.
    "target": "esnext",
    // Search under node_modules for non-relative imports.
    "moduleResolution": "node",
    // Process & infer types from .js files.
    "allowJs": true,
    // Don't emit; allow Babel to transform files.
    "noEmit": true,
    // Enable strictest settings like strictNullChecks & noImplicitAny.
    "strict": true,
    // Disallow features that require cross-file information for emit.
    "isolatedModules": true,
    // Import non-ES modules as default imports.
    "esModuleInterop": true,
    "skipLibCheck": true
  },
  // Import custom typings
  "include": ["src/shared/**/*", "./typings"]
}

This is not a problem with .tsconfig. I researched it, and we have the correct settings. Just putting it out there, for more info on the matter. If someone has the same problem as me, please post the answer or a link to it. Thanks!!

Share Improve this question edited Mar 25, 2019 at 18:08 Christian Vincenzo Traina 10.4k4 gold badges45 silver badges78 bronze badges asked Mar 25, 2019 at 18:01 user10104341user10104341 1
  • In my case, adding "moduleResolution": "node" in the author's configuration fixed the issue. – dance2die Commented Oct 4, 2022 at 14:25
Add a comment  | 

2 Answers 2

Reset to default 11

I found an answer to my question. In the tsconfig.json. The problem was that in the include section. I was having problems in the src/admin/...

So, I modified the file accordingly to include that. After that I don't have any problems:

  "include": ["src/shared/**/*", "src/admin/**/* ", ./typings"]

So, the general answer I guess is to include every module, that you use aside from the general src one.

I had this problem with a module I had created myself locally and had installed using npm i file://<PATH> which creates a symlink to the local module.

The project built fine but VSCode's typescript language server could not seem to find the module.

Closing and reopening the folder did not resolve the problem.

I resolved it by going to an import declaration for the module and engaging autocomplete on the module name with ctrl+space to populate a list of modules starting with my actual module name. The module name popped up in the resulting list of suggestions (well, it was a list of one item) and the errors vanished immediately. OK. Whatever. Thanks Microsoft. So this is something else you can try.

发布评论

评论列表(0)

  1. 暂无评论