I want to use:
require.context('../images/', true, /\.(png|ico|svg|jpg|gif)$/)
but I get the following error:
Property context does not exist on type NodeRequire
I want to use:
require.context('../images/', true, /\.(png|ico|svg|jpg|gif)$/)
but I get the following error:
Property context does not exist on type NodeRequire
And in tsconfig.json add it in the types property example:
{
"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
}
Besides the above solutions, you can also add an interface for this function manually just like this:
//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;
}
}