Node.js 如何为所有节点模块自动安装@types
我不熟悉打字脚本和NodeJs。每当我在Node.js 如何为所有节点模块自动安装@types,node.js,angular,typescript,npm,Node.js,Angular,Typescript,Npm,我不熟悉打字脚本和NodeJs。每当我在package.json中提到一个节点模块,并且在导入节点模块时,我总是会遇到以下错误 Could not find a declaration file for module 'ini'. 'e:/typescript-2020-1/parser1/node_modules/ini/ini.js' implicitly has an 'any' type. Try `npm install @types/ini` if it exists or ad
package.json
中提到一个节点模块,并且在导入节点模块时,我总是会遇到以下错误
Could not find a declaration file for module 'ini'. 'e:/typescript-2020-1/parser1/node_modules/ini/ini.js' implicitly has an 'any' type.
Try `npm install @types/ini` if it exists or add a new declaration (.d.ts) file containing `declare module 'ini';`
Could not find a declaration file for module 'json-query'. 'e:/typescript-2020-1/parser1/node_modules/json-query/index.js' implicitly has an 'any' type.
Try `npm install @types/json-query` if it exists or add a new declaration (.d.ts) file containing `declare module 'json-query';`
为了解决这个问题,我总是在package.json
中搜索并安装以下内容,如下所示
"dependencies": {
"ini": "^1.3.5",
"@types/ini": "^1.3.30",
"json-query": "^2.2.2",
"@types/json-query": "^2.2.0"
"@types/node": "^13.9.0"
}
如何避免包含所有类型的节点模块特定类型。
是否可以在节点模块内自动添加@types/
,而不直接添加到package.json
?
我还试图在tsconfig.json
中添加以下内容,但它不起作用
"typeRoots": [
"node_modules/@types"
],
我的问题是“是否总是需要为每个节点模块添加@类型
?
请帮我解决这个问题。如果npm软件包包含类型,您将在安装时免费获得它们 如果没有,那么您可以在npm的@types命名空间下找到它们 有一个很有用的工具叫做,你可以通过
npx
安装或运行它,当你使用它来安装任何npm软件包时,它会尝试为你的软件包安装@types
i、 e
npx typac ini-i
afaik,如果模块没有类型声明(直接包含或作为依赖项),则必须添加相应的@types
模块,就像您在package.json
中那样,作为依赖项。默认情况下,@types
下的所有模块(一旦安装)是可见的,tsconfig.json中的types
和typeroot
用于更改此默认可见性。Doc:@Tobías,很少有模块没有键入,有些模块有。作为开发人员,我知道要安装哪个节点模块,但如何实现这一点在使用typescript的情况下,将为特定的节点添加键入节点模块。如果模块在package.json
中声明了types
元素,它就有类型声明。如果没有,我会尝试从@types repo安装它们,比如npm install@types/module name
。但是有些模块根本没有类型定义。作者和其他人都没有创建它们。在这种情况下,如果我想要或者必须使用我自己为模块做了一个类型定义(.d.ts
文件)。我不知道是否有办法自动生成类型。