Typescript 找不到模块';模块名称''/path/to/module name.js';隐式具有';任何';类型

Typescript 找不到模块';模块名称''/path/to/module name.js';隐式具有';任何';类型,typescript,node-modules,Typescript,Node Modules,我读过打字脚本的工作原理 我有以下存储库:。 编译后的目录结构如下: ├── dist │   ├── annotations.d.ts │   ├── annotations.js │   ├── index.d.ts │   ├── index.js │   ├── injector.d.ts │   ├── injector.js │   ├── profiler.d.ts │   ├── profiler.js │   ├── providers.d.ts │   ├── provide

我读过打字脚本的工作原理

我有以下存储库:。 编译后的目录结构如下:

├── dist
│   ├── annotations.d.ts
│   ├── annotations.js
│   ├── index.d.ts
│   ├── index.js
│   ├── injector.d.ts
│   ├── injector.js
│   ├── profiler.d.ts
│   ├── profiler.js
│   ├── providers.d.ts
│   ├── providers.js
│   ├── util.d.ts
│   └── util.js
├── LICENSE
├── package.json
├── README.md
├── src
│   ├── annotations.ts
│   ├── index.ts
│   ├── injector.ts
│   ├── profiler.ts
│   ├── providers.ts
│   └── util.ts
└── tsconfig.json
在我的package.json中,我写了
“main”:“dist/index.js”

在Node.js中,一切正常,但TypeScript:

import {Injector} from '@ts-stack/di';
找不到模块“@ts stack/di”的声明文件/path/to/node_modules/@ts stack/di/dist/index.js'隐式具有“any”类型

然而,如果我按如下方式导入,那么一切都会正常工作:

import {Injector} from '/path/to/node_modules/@ts-stack/di/dist/index.js';

我做错了什么?

当你看了两天,发现它是这样的感觉:只要从
“main”中删除
.js
package.json
中的“dist/index.js”
,一切正常

“main”:“dist/index”,
UPD:如果您有自己的npm软件包,则此答案是相对的,如果没有,请参阅

如果上面的答案没有解决,请导入您的模块,尝试在
package.json
中添加
typings

“main”:“dist/index”,
“打字”:“距离/索引”,

当然,这里是文件夹
dist
——它是存储模块文件的地方。

这里是另外两个解决方案

当模块不属于您时-尝试从
@types
安装类型:

npm install -D @types/module-name
如果出现上述安装错误-请尝试将
import
语句更改为
require

// import * as yourModuleName from 'module-name';
const yourModuleName = require('module-name');

如果导入的第三方模块
'foo'
在库本身或
@types/foo
包(从存储库生成)中不提供任何键入,则可以通过在扩展名为
.d.ts
的文件中声明模块来消除此错误。TypeScript查找
.d.ts
文件的位置与查找普通
.ts
文件的位置相同:如
tsconfig.json
中“文件”、“包含”和“排除”下所述

  {
    "compilerOptions": {
        "strictNullChecks": true,
        "moduleResolution": "node",
        "jsx": "react",
        "noUnusedParameters": true,
        "noUnusedLocals": true,
        "allowSyntheticDefaultImports":true,
        "target": "es5",
        "module": "ES2015",
        "declaration": true,
        "outDir": "./lib",
        "noImplicitAny": true,
        "importHelpers": true
      },
      "include": [
        "src/**/*",
        "index.d.ts",   // declaration file path
      ],
      "compileOnSave": false
    }
//foo.d.ts
声明模块'foo';
然后,当您导入
foo
时,它将被键入为
any


或者,如果您想自己打字,也可以这样做:

//foo.d.ts
声明模块'foo'{
导出函数getRandomNumber():number
} 
这样就可以正确编译:

从'foo'导入{getRandomNumber};
常量x=getRandomNumber();//x被推断为数字
您不必为模块提供完整的输入,只需为实际使用的位提供足够的输入(并且需要正确的输入),因此,如果您使用的API数量相当少,那么就特别容易做到


另一方面,如果您不关心外部库的键入,并且希望将所有没有键入的库作为
any
导入,则可以将其添加到扩展名为
.d.ts
的文件中:

声明模块'*';

这样做的好处(也有坏处)是,您可以完全导入任何内容,TS将进行编译。

TypeScript基本上是实现规则并向代码中添加类型,以使代码更加清晰和准确,因为Javascript中缺少约束。TypeScript要求您描述数据,以便编译器可以检查代码并发现错误。编译器将告诉您是否使用了不匹配的类型、是否超出了范围或是否尝试返回其他类型。 因此,当您将外部库和模块与TypeScript一起使用时,它们需要包含描述代码中类型的文件。这些文件称为类型声明文件,扩展名为
d.ts
。npm模块的大多数声明类型都已经编写好了,您可以使用
npm install@types/module\u name
(其中module\u name是要包含其类型的模块的名称)

但是,有些模块没有其类型定义,为了消除错误并使用“模块名称”中的
import*as module\u name导入模块,请在项目根目录中创建一个文件夹
typings
,在内部使用您的模块名称创建一个新文件夹,并在该文件夹中创建一个
module\u name.d.ts
文件并写入
声明模块“module\u name”
。在此之后,只需转到您的
tsconfig.json
文件,并在
编译器选项中添加
“typeroot”:[“../../typings”、“../../node_modules/@types”]
(具有文件夹的正确相对路径),让TypeScript知道在哪里可以找到库和模块的类型定义,并添加一个新属性
“排除”:[“../../node_modules”、“../../typings”]
到文件。
  {
    "compilerOptions": {
        "strictNullChecks": true,
        "moduleResolution": "node",
        "jsx": "react",
        "noUnusedParameters": true,
        "noUnusedLocals": true,
        "allowSyntheticDefaultImports":true,
        "target": "es5",
        "module": "ES2015",
        "declaration": true,
        "outDir": "./lib",
        "noImplicitAny": true,
        "importHelpers": true
      },
      "include": [
        "src/**/*",
        "index.d.ts",   // declaration file path
      ],
      "compileOnSave": false
    }
下面是您的tsconfig.json文件的示例:

{
    "compilerOptions": {
        "module": "commonjs",
        "noImplicitAny": true,
        "sourceMap": true,
        "outDir": "../dst/",
        "target": "ESNEXT",
        "typeRoots": [
            "../../typings",
            "../../node_modules/@types"
        ]
    },
    "lib": [
            "es2016"
    ],
    "exclude": [
        "../../node_modules",
        "../../typings"
    ]
}
  {
    "compilerOptions": {
        "strictNullChecks": true,
        "moduleResolution": "node",
        "jsx": "react",
        "noUnusedParameters": true,
        "noUnusedLocals": true,
        "allowSyntheticDefaultImports":true,
        "target": "es5",
        "module": "ES2015",
        "declaration": true,
        "outDir": "./lib",
        "noImplicitAny": true,
        "importHelpers": true
      },
      "include": [
        "src/**/*",
        "index.d.ts",   // declaration file path
      ],
      "compileOnSave": false
    }

通过这样做,错误将消失,您将能够坚持最新的ES6和TypeScript规则。

我也得到了这一点,让我困惑了一段时间,即使模块和类型已经安装并多次重新加载IDE


在我的案例中,解决这个问题的方法是终止终端进程,删除
节点\u模块,清除节点包管理器缓存,重新安装
,然后重新加载编辑器。

我使用带有用typescript编写的react应用程序的节点模块时遇到了同样的问题。该模块是使用成功安装的de>npm i——保存我的模块
。 它是用javascript编写的,并导出一个
客户机

与:

编译失败,错误为:

Could not find a declaration file for module 'my-module'. 
'[...]/node_modules/my-module/lib/index.js' implicitly has an 'any' type.
  Try `npm install @types/my-module` if it exists or add a new declaration (.d.ts) file containing `declare module 'my-module';`
Namespace '"my-module"' has no exported member 'Client'.
@types/my module
不存在,因此我在导入
my module
的文件旁边添加了一个带有建议行的
my module.d.ts
文件。然后我得到了错误:

Could not find a declaration file for module 'my-module'. 
'[...]/node_modules/my-module/lib/index.js' implicitly has an 'any' type.
  Try `npm install @types/my-module` if it exists or add a new declaration (.d.ts) file containing `declare module 'my-module';`
Namespace '"my-module"' has no exported member 'Client'.
如果我在js应用程序中使用客户端,它实际上已导出并正常工作。此外,前面的消息告诉我编译器正在查找正确的文件(
/node\u module)
declare module 'v-tooltip';
declare module 'parse5';
declare module 'emoji-mart-vue-fast';
var _ = require('your_module_name');
// example.d.ts
declare module 'foo';
// example.d.ts
declare module 'foo'{
    // example
    export function getName(): string
}
const x = require('foo') // or import x from 'foo'
x.getName() // intellisense can read this
import { SomeModuleType } from '3rd-party-module';
type MyType: import('3rd-party-module').SomeModuleType;
// tsconfig.json
{
  "compilerOptions": {
  ...
  "include": [
    "src", 
  ],
  "exclude": [
    "node_modules", 
  ]
}
declare module 'react-mobile-datepicker' {
  class DatePicker extends React.Component<DatePickerProps, any> {}

  interface DatePickerProps {
    isPopup?: boolean;
    theme?: string;
    dateConfig?: DatePickerConfig;
  }

  export interface DatePickerConfig {
    prop1: number;
    pro2: string;
  }
  export default DatePicker;
}
import DatePicker, { DatePickerConfig, DatePickerConfigDate } from 'react-mobile-datepicker';
{
  "compilerOptions": {
    //...other properties
    "typeRoots": [
      "src/typings",
      "node_modules/@types"
    ]
  }}