类型NodereQuire上不存在属性上下文



我想使用: require.context('../images/', true, /.(png|ico|svg|jpg|gif)$/)

但是我会收到以下错误:

类型noderequire上不存在属性上下文

只需安装 webpack-env

的打字
npm i @types/webpack-env -D

和在tsconfig.json中将其添加到类型属性中示例:

 {
  "compilerOptions": {
    "outDir": "./dist/",
    "declaration": true,
    "declarationDir": "./dist/",
    "sourceMap": true,
    "noImplicitAny": false,
    "module": "commonjs",
    "target": "es6",
    "allowJs": false,
    "types": [
      "node",
      "webpack-env" // here
    ]
  },
  "files": [
    "src/index.ts"
  ],
  "compileOnSave": false
}

除了上述解决方案之外,您还可以手动添加此功能的接口:

//index.d.ts
declare global {
    interface NodeRequire {
        /** A special feature supported by webpack's compiler that allows you to get all matching modules starting from some base directory.  */
        context: (
            directory: string,
            useSubdirectories: boolean,
            regExp: RegExp
        ) => any;
    }
}

相关内容

  • 没有找到相关文章

最新更新