typescript 打字稿找不到模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39326821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:49:16  来源:igfitidea点击:

Typescript Cannot find module

typescript

提问by HMR

Filter.ts, mustache.js and redux.3.5.2.js are in the same directory (/Scripts). The Filter.ts has the following code:

Filter.ts、mustache.js 和 redux.3.5.2.js 位于同一目录(/Scripts)中。Filter.ts 有以下代码:

///<reference path="./typings/mustache.d.ts" />
import Mustache = require("mustache");
import Redux = require("redux.3.5.2");

In Visual studio code it shows an error on redux cannot find module. When I compile it it says:

在 Visual Studio 代码中,它在 redux 上显示错误找不到模块。当我编译它时,它说:

Filter.ts(13,24): error TS2307: Cannot find module 'redux.3.5.2'.

Filter.ts(13,24):错误 TS2307:找不到模块“redux.3.5.2”。

So how is it able to find mustache but not redux?

那么它如何能够找到 mustache 而不是 redux 呢?

I did not add a typing file yet but how would that affect importing the file? It is not yet used anywhere in code? Also removing the mustache typing does not result in typescript not finding the file.

我还没有添加打字文件,但这会如何影响导入文件?它尚未在代码中的任何地方使用?此外,删除胡子打字不会导致打字稿找不到文件。

$ ls *.js
requirejs.2.1.22.js  Filter.ts              mustache.js          redux.3.5.2.js

[Update]

[更新]

Did an update of typescript:

是否更新了打字稿:

npm install -g typescript

tsc.cmd now tells me I have Version 1.8.10

tsc.cmd 现在告诉我我有 1.8.10 版

My task.json looks like:

我的 task.json 看起来像:

{
    "version": "0.1.0",
    "command": "tsc.cmd",
    "isShellCommand": true,
    "showOutput": "silent",
    "problemMatcher": "$tsc"
}

and tsconfig.json looks like:

和 tsconfig.json 看起来像:

{
    "compilerOptions": {
        "target": "es5",
        "watch": true,
        "module": "amd",
        "removeComments": true,
        "sourceMap": false,
        "outDir": "."
    }
}

Now Visual studio code complains it cannot find any of my required modules and tsc.cmd complains it still cannot find module 'redux.3.5.2'. Luckily after a couple of restarts of Visual studio code those errors went away but it is still not able to find redux. I'm not sure if it cannot find the file or something in the file is causing this problem because the error does not specify what the exact problem is.

现在 Visual Studio 代码抱怨它找不到我需要的任何模块,而 tsc.cmd 抱怨它仍然找不到模块“redux.3.5.2”。幸运的是,在几次重新启动 Visual Studio 代码后,这些错误消失了,但仍然无法找到 redux。我不确定它是否找不到文件或文件中的某些内容导致了此问题,因为该错误未指定确切的问题是什么。

回答by basarat

So how is it able to find mustache but not redux

那么它如何能够找到小胡子而不是 redux

I can see in your code you have ///<reference path="./typings/mustache.d.ts" />. So you do have typings for mustache and hence it is found. I don't see any typings for redux in your sample, hence not found.

我可以在你的代码中看到你有///<reference path="./typings/mustache.d.ts" />。所以你确实有胡子的类型,因此它被找到了。我在您的示例中没有看到任何 redux 类型,因此未找到。

回答by HMR

The JavaScript file does not matter, put a definition file (for example: module-name.d.ts) in the root.

JavaScript 文件无所谓,module-name.d.ts在根目录中放一个定义文件(例如:)。

Now your ts file can contain the following:

现在您的 ts 文件可以包含以下内容:

///<reference path="module-name.d.ts" />
import redux = require("module-name");

In require.config you can do the following:

在 require.config 中,您可以执行以下操作:

require.config = {
  baseUrl:settings.appRoot,
  urlArgs: "version=" + settings.version,
  paths: {
    "module-name" :   settings.appRoot + "/Scripts/SomePlace/some-module",
...

If you put the type definition in one directory (for example typings) then the ts file would look like this:

如果您将类型定义放在一个目录中(例如 Typings),那么 ts 文件将如下所示:

///<reference path="../typings/module-name.d.ts" />
import redux = require("typings/module-name");

If another ts file requires this module but the ts file is in another directory (let's see one deeper) then you can do the following:

如果另一个 ts 文件需要此模块,但 ts 文件位于另一个目录中(让我们深入了解),那么您可以执行以下操作:

///<reference path="../../typings/module-name.d.ts" />
import redux = require("typings/module-name");

Your require.config will look like:

您的 require.config 将如下所示:

require.config = {
  baseUrl:settings.appRoot,
  urlArgs: "version=" + settings.version,
  paths: {
    "typings/module-name" :   settings.appRoot + "/Scripts/SomePlace/some-module",
...