Definitelytyped: webpack-env.d.ts does not compile with node.d.ts

Created on 9 Feb 2016  ·  6Comments  ·  Source: DefinitelyTyped/DefinitelyTyped

Before you ask: Yes, I searched - and found #6334 - although this does not address my issue.

I'm writing a web application that uses Webpack. As a result, I do need access to webpack-env.d.ts in the front end, but I'm also interested in using the url module from Node (for url.parse). If I require webpack-env.d.ts and node.d.ts in the same references file, predictably it breaks because Node re-defines require. IOW, I need access to node.d.ts's url module and webpack's require definition.

I can get around this by using const url: any = require('url'), though this isn't ideal and I'd like to have typings for this module. Another option would be to split node.d.ts into its constituent modules and have the master node.d.ts require each individual module, though I'm not sure if this would break anything from the many people who rely on this TypeScript declaration file - and I don't particularly want to hack it together locally.

Any suggestions?

Most helpful comment

but what if @types/node is a dependency of another dependency (e.g. fs-extra, jsdom, shelljs, tape)?
also ended up removing webpack-env and

if ((module as any).hot) {
...

😢

All 6 comments

Well, technically you are still not in node environment and it's not correct to include the entire node.d.ts file. You are using an npm package that acts the same as node's "url", but in browser environment. What you can do is extract those typings from node.d.ts and have a separate url.d.ts, like you already suggested. Just splitting node.d.ts into smaller modules may not suit every need, because the signatures may be different due to environment differences (Buffers vs Arrays) or implementations. I would suggest an independent typing for this "url" package.

I sort of ran into the same issue where I use include typings/index.ts in my tsconfig.json to include all typings. When adding both webpack-env.d.ts and node, the typescript compiler obviously complains. Is there a way around this?

test_index.ts(1,28): error TS2339: Property 'context' does not exist on type 'NodeRequire'.

The way I "solved" it was to create a separate typings file for the node-specific modules I wanted to use. Bit of a hack, unfortunately.

I ended up removing webpack-env altogether as I only needed one typing in my entire project for that. Hopefully it stays that way. I tried your solution but I really don't like polluting my files with references as much as possible. Thanks for the reply

Saurabh Sharma / Quality Engineering
[email protected] / (925)-878-5722
Yammer, Microsoft
http://www.foklepoint.com

On Aug 14, 2016, at 7:08 AM, Dan [email protected] wrote:

The way I "solved" it was to create a separate typings file for the node-specific modules I wanted to use. Bit of a hack, unfortunately.


You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.

Sure, neither do I :( But I needed that module, sadly. @foklepoint

but what if @types/node is a dependency of another dependency (e.g. fs-extra, jsdom, shelljs, tape)?
also ended up removing webpack-env and

if ((module as any).hot) {
...

😢

Was this page helpful?
0 / 5 - 0 ratings