The property "provide" does not exist in the type "NodeRequire"

I am trying to break the webpack 2 code.

According to this document: https://webpack.js.org/guides/code-splitting-require/

The following code should include some.css in a new snippet called "something"

require.ensure([], function(require) { require('some.css'); }, 'something'); 

but when I run it, I get this error:

 ERROR in ./src/index.ts (4,9): error TS2339: Property 'ensure' does not exist on type 'NodeRequire'. 

Any idea on how to fix it? Thanks

+5
source share
2 answers

As I solved this, I created my own interface - WebpackRequire - which extends NodeRequire with ensure 1 .

 interface WebpackRequire extends NodeRequire { ensure( dependencies: string[], callback: (require: WebpackRequire) => void, errorCallback?: (error: Error) => void, chunkName?: string ): void; }; 

If you have only one instance of require.ensure , you can enter it in WebpackRequire using (require as WebpackRequire).ensure , but since I used it several times in the module, I created a local require at the top of the module, cast type as WebpackRequire , eg:

 const require: WebpackRequire = (window as any).require; 

1 I got ensure types from Documents on the Internet

+1
source

I needed a javascript document which then required. Not a pretty solution, but it really worked

0
source

Source: https://habr.com/ru/post/1263123/


All Articles