Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webpack-env.d.ts does not compile with node.d.ts #8018

Closed
punmechanic opened this issue Feb 9, 2016 · 6 comments
Closed

webpack-env.d.ts does not compile with node.d.ts #8018

punmechanic opened this issue Feb 9, 2016 · 6 comments

Comments

@punmechanic
Copy link

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?

@use-strict
Copy link
Contributor

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.

@foklepoint
Copy link

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'.

@punmechanic
Copy link
Author

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.

@foklepoint
Copy link

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
sausha@microsoft.com / (925)-878-5722
Yammer, Microsoft
http://www.foklepoint.com

On Aug 14, 2016, at 7:08 AM, Dan notifications@github.com 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.

@punmechanic
Copy link
Author

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

@vyorkin
Copy link

vyorkin commented Dec 4, 2016

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) {
...

😢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants