typescript 找不到模块“module-name”的声明文件。'/path/to/module-name.js' 隐式有一个 'any' 类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41292559/
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
Could not find a declaration file for module 'module-name'. '/path/to/module-name.js' implicitly has an 'any' type
提问by ktretyak
I read how TypeScript module resolutionworks.
我阅读了 TypeScript模块解析的工作原理。
I have the following repository: @ts-stack/di. After compiling the directory structure is as follows:
我有以下存储库:@ts-stack/di。编译后的目录结构如下:
├── 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
In my package.json I wrote "main": "dist/index.js"
.
在我的 package.json 中,我写了"main": "dist/index.js"
.
In Node.js everything works fine, but TypeScript:
在 Node.js 中一切正常,但 TypeScript:
import {Injector} from '@ts-stack/di';
Could not find a declaration file for module '@ts-stack/di'. '/path/to/node_modules/@ts-stack/di/dist/index.js' implicitly has an 'any' type.
找不到模块“@ts-stack/di”的声明文件。'/path/to/node_modules/@ts-stack/di/dist/index.js' 隐式具有 'any' 类型。
And yet, if I import as follows, then everything works:
然而,如果我按如下方式导入,那么一切正常:
import {Injector} from '/path/to/node_modules/@ts-stack/di/dist/index.js';
What am I doing wrong?
我究竟做错了什么?
采纳答案by ktretyak
That feeling when you are looking out for two days and find it like this: just remove .js
from "main": "dist/index.js"
in package.json
and everything works fine!
那种感觉,当你正在寻找了两天,发现它是这样的:只是删除.js
从"main": "dist/index.js"
在package.json
,一切工作正常!
"main": "dist/index",
UPD: this answer relative if you have your own npm package, if not - see my answer below.
UPD:如果你有自己的 npm 包,这个答案是相对的,如果没有 - 请参阅下面的答案。
And if above answer not resolved import your module, try just add typings
in package.json
:
如果以上回答没有解决您的进口模块,尝试只需添加typings
在package.json
:
"main": "dist/index",
"typings": "dist/index",
Of course, here folder dist
- it's where stores your module's files.
当然,这里的文件夹dist
- 它是存储模块文件的地方。
回答by ktretyak
Here are two other solutions
这是另外两个解决方案
When a module is not yours - try to install types from @types
:
当模块不属于您时 - 尝试从@types
以下位置安装类型:
npm install -D @types/module-name
If the above install errors - try changing import
statements to require
:
如果上述安装错误 - 尝试将import
语句更改为require
:
// import * as yourModuleName from 'module-name';
const yourModuleName = require('module-name');
回答by Retsam
If you're importing a third-party module 'foo'
that doesn't provide any typings, either in the library itself, or in the @types/foo
package (generated from the DefinitelyTypedrepository), then you can make this error go away by declaring the module in a file with a .d.ts
extension. Typescript looks for .d.ts files in the same places that it will look for normal .ts files: as specified under "files", "include", and "exclude" in the tsconfig.json.
如果您要导入'foo'
不提供任何类型的第三方模块,无论是在库本身中,还是在@types/foo
包中(从绝对类型存储库生成),那么您可以通过在带.d.ts
扩展名的文件。Typescript 在与查找普通 .ts 文件相同的位置查找 .d.ts 文件:如 tsconfig.json 中“files”、“include”和“exclude”下指定的那样。
// foo.d.ts
declare module 'foo';
Then when you import foo
it'll just be typed as any
.
然后当你导入时,foo
它只会被输入为any
.
Alternatively, if you want to roll your own typings, you can do that, too:
或者,如果您想滚动自己的类型,也可以这样做:
// foo.d.ts
declare module 'foo' {
export function getRandomNumber(): number
}
Then this will compile correctly:
然后这将正确编译:
import { getRandomNumber } from 'foo';
const x = getRandomNumber(); // x is inferred as number
You don't have to provide full typings for the module, just enough for the bits that you're actually using (and want proper typings for), so it's particularly easy to do if you're using a fairly small amount of API.
您不必为模块提供完整的类型,只需要为您实际使用的位(并需要适当的类型)提供足够的类型,因此如果您使用相当少量的 API,这特别容易做到。
On the other hand, if you don't care about the typings of external libraries and want all libraries without typings to be imported as any
, you can add this to a file with a .d.ts
extension:
另一方面,如果您不关心外部库的类型并希望将所有没有类型的库导入为any
,则可以将其添加到带有.d.ts
扩展名的文件中:
declare module '*';
The benefit (and downside) of this is that you can import absolutely anything and TS will compile.
这样做的好处(和坏处)是你可以导入任何东西并且 TS 会编译。
回答by Liran H
If you need a quick fix, simply add this before your import:
如果您需要快速修复,只需在导入前添加:
// @ts-ignore
回答by Marko Rochevski
TypeScript is basically implementing rules and adding types to your code to make it more clear and more accurate due to the lack of constraints in Javascript. TypeScript requires you to describe your data, so that the compiler can check your code and find errors. The compiler will let you know if you are using mismatched types, if you are out of your scope or you try to return a different type.
So, when you are using external libraries and modules with TypeScript, they need to contain files that describe the types in that code. Those files are called type declaration fileswith an extension d.ts
. Most of the declaration types for npm modules are already written and you can include them using npm install @types/module_name
(where module_name is the name of the module whose types you wanna include).
由于 Javascript 中缺少约束,TypeScript 基本上是在实现规则并将类型添加到您的代码中以使其更加清晰和准确。TypeScript 要求您描述您的数据,以便编译器可以检查您的代码并发现错误。编译器会告诉您是否使用了不匹配的类型,是否超出了您的范围或您尝试返回不同的类型。因此,当您在 TypeScript 中使用外部库和模块时,它们需要包含描述该代码中类型的文件。这些文件称为类型声明文件,扩展名为d.ts
. 大多数 npm 模块的声明类型都已经编写好了,您可以使用它们来包含它们npm install @types/module_name
(其中 module_name 是您想要包含其类型的模块的名称)。
However, there are modules that don't have their type definitions and in order to make the error go away and import the module using import * as module_name from 'module-name'
, create a folder typings
in the root of your project, inside create a new folder with your module name and in that folder create a module_name.d.ts
file and write declare module 'module_name'
. After this just go to your tsconfig.json
file and add "typeRoots": [ "../../typings", "../../node_modules/@types"]
in the compilerOptions
(with the proper relative path to your folders) to let TypeScript know where it can find the types definitions of your libraries and modules and add a new property "exclude": ["../../node_modules", "../../typings"]
to the file.
Here is an example of how your tsconfig.json file should look like:
但是,有些模块没有它们的类型定义,为了使错误消失并使用导入模块import * as module_name from 'module-name'
,typings
在项目的根目录中创建一个文件夹,在里面创建一个具有模块名称的新文件夹,在该文件夹中文件夹创建一个module_name.d.ts
文件并写入declare module 'module_name'
. 在此之后就到你的tsconfig.json
文件,并添加"typeRoots": [ "../../typings", "../../node_modules/@types"]
在compilerOptions
(用正确的相对路径到您的文件夹),让打字稿知道在哪里可以找到你的库和模块的类型定义和新的属性添加"exclude": ["../../node_modules", "../../typings"]
到文件中。以下是 tsconfig.json 文件的示例:
{
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": true,
"sourceMap": true,
"outDir": "../dst/",
"target": "ESNEXT",
"typeRoots": [
"../../typings",
"../../node_modules/@types"
]
},
"lib": [
"es2016"
],
"exclude": [
"../../node_modules",
"../../typings"
]
}
By doing this, the error will go away and you will be able to stick to the latest ES6 and TypeScript rules.
通过这样做,错误将消失,您将能够坚持最新的 ES6 和 TypeScript 规则。
回答by Martin Lockett
For anyone else reading this, try renaming your .js file to .ts
对于阅读本文的其他人,请尝试将您的 .js 文件重命名为 .ts
Edit:
You can also add "allowJs": true
to your tsconfig file.
编辑:您还可以添加"allowJs": true
到您的 tsconfig 文件。
回答by Lumaskcete
This way works for me:
这种方式对我有用:
1.在index.d.ts等声明文件中添加自己的声明(可能在项目根目录下)declare module 'Injector';
2. 将你的 index.d.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
}
-- edit: needed quotation marks around module name
-- 编辑:需要在模块名称周围加引号
回答by Kanthavel
I had the same issue using a node module with a react application written in typescript. The module was successfully installed using npm i --save my-module
.
It is written in javascript and exports a Client
class.
我使用带有用打字稿编写的反应应用程序的节点模块时遇到了同样的问题。该模块已成功安装使用npm i --save my-module
. 它是用 javascript 编写的并导出一个Client
类。
With:
和:
import * as MyModule from 'my-module';
let client: MyModule.Client = new MyModule.Client();
Compilation fails with the error:
编译失败并显示错误:
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';`
@types/my-module
does not exist, so I added a my-module.d.ts
file next to the one where my-module
is imported, with the suggested line. I then got the error:
@types/my-module
不存在,所以我my-module.d.ts
在my-module
导入的文件旁边添加了一个文件,并带有建议的行。然后我得到了错误:
Namespace '"my-module"' has no exported member 'Client'.
The client is actually exported and works normally if I use it in a js app. Also, the previous message tells me that the compiler is looking in the right file (/node_modules/my-module/lib/index.js
is defined in my-module/package.json
"main"
element).
如果我在 js 应用程序中使用它,客户端实际上已导出并正常工作。此外,上一条消息告诉我编译器正在查找正确的文件(/node_modules/my-module/lib/index.js
在my-module/package.json
"main"
element 中定义)。
I solved the issue by telling the compiler I do not care about implicit any
, that is, I set to false
the following line of the tsconfig.json
file:
我通过告诉编译器我不关心隐式解决了这个问题any
,也就是说,我设置为文件false
的以下行tsconfig.json
:
"noImplicitAny": false,
回答by Abdul Aziz Al Basyir
simple to fix you re is :
修复您的简单方法是:
// example.d.ts
declare module 'foo';
if you want to declarate interface of object (Recommend for big project) you can use :
如果你想声明对象的接口(推荐用于大项目),你可以使用:
// example.d.ts
declare module 'foo'{
// example
export function getName(): string
}
How to use that? simple..
怎么用那个?简单的..
const x = require('foo') // or import x from 'foo'
x.getName() // intellisense can read this
回答by Luke Garrigan
Unfortunately it's out of our hands whether the package writer bothers with a declaration file. What I tend to do is have a file such index.d.ts
that'll contain all the missing declaration files from various packages:
不幸的是,我们无法控制包编写者是否会为声明文件烦恼。我倾向于做的是有一个文件,index.d.ts
其中将包含来自各种包的所有丢失的声明文件:
Index.ts:
索引.ts:
declare module 'v-tooltip';
declare module 'parse5';
declare module 'emoji-mart-vue-fast';