找不到模块的声明文件。 '/path/to/module-name.js'隐式具有'any'类型

时间:2018-12-01 23:54:37

标签: javascript reactjs gatsby

我正在阅读Gatsby.js教程,在第二篇教程中,您必须导入一些字体。当我尝试导入theme lawton时,我在import lawtonTheme from "typography-theme-lawton";下看到此错误

我首先做了npm install --save typography-theme-lawton

  Could not find a declaration file for module 'typography-theme-lawton'. '/Users/react/tutorial-part-two/node_modules/typography-theme-lawton/dist/index.js' implicitly has an 'any' type.
  Try `npm install @types/typography-theme-lawton` if it exists or add a new declaration (.d.ts) file containing `declare module 'typography-theme-lawton';`

这是我的typography.js文件:

import Typography from "typography";
//import bootstrapTheme from " typography-theme-bootstrap";
import lawtonTheme from "typography-theme-lawton";

const typography = new Typography(lawtonTheme);

//const typography = new Typography({ baseFontSize: "18px" });
//const typography = new Typography(bootstrapTheme);

export default typography;

这是我的gatsby-config.js文件:

  module.exports = {
  plugins: [
    {
      resolve: `gatsby-plugin-typography`,
      options: {
        pathToConfigModule: `src/utils/typography.js`
      }
    }
  ]
};

还在gatsby-config文件中,单词module是带下划线的,此注释显示: [ts] File is a CommonJS module; it may be converted to an ES6 module. [80001]

我也尝试过npm install @types/typography-theme-lawton,但它给了我错误

1 个答案:

答案 0 :(得分:1)

根据您的问题,我想您正在使用打字稿,并且该项目找不到ts模块。 您需要安装ts文件。尝试以下操作:

npm install —-save @types/.....

或者,您可以在项目中的某个位置添加d.ts文件,然后在文件中添加以上内容。

declare module typography-theme-lawton

[ts]文件是CommonJS模块;可以将其转换为ES6模块。 [80001]

我假设您使用的是VSCode。请将其添加到设置中以启用它:

"javascript.suggestionActions.enabled": false

PS:这只是一个建议,因此您可以忽略此点。