defunctzombie / node-url

node.js core url module as a module
MIT License
375 stars 96 forks source link

NPM@3 causes unwanted module overwrite #22

Closed JoshuaWise closed 8 years ago

JoshuaWise commented 8 years ago

Before NPM version 3.0.0, I could use modules that require()'d this module, and then go ahead and use the Node core url module all I wanted. But now in NPM@3, the entire dependency tree is installed as flat as possible in the node_modules folder. This means that if I use a module that uses this module, I can no longer use the native url module anymore, because this one overwrites it. I did not put this module in my package.json as a dependency, an yet I am forced to use it. This should NOT be possible. If it's important to provide mirrors of the Node core modules in NPM, they should be under a different name. This wouldn't cause any problems, but it would solve this one.

JoshuaWise commented 8 years ago

Closed. This bug was being caused by a custom-hacked version of NPM at my office. Sorry.

rhengles commented 2 years ago

I'm having this problem today because of pnpm (6.23.1, but I don't think it's limited to this version).

My project uses pnpm and cypress. Cypress imports this "url" npm module but then Vite is replacing another package's import of the builtin url module with this npm module, and then my code breaks, with this error specifically:

Uncaught SyntaxError: The requested module '/node_modules/.pnpm/url@0.11.0/node_modules/url/url.js?v=0c7e7bad' does not provide an export named 'default'
rhengles commented 2 years ago

I added url to my vite config's optimizeDeps.exclude while trying to resolve the problem that when building for production, Vite complained that it could not bundle url for the browser.

Now I removed that, and both vite dev and vite build seem to be working now!

Edit: A few more details here -> https://discord.com/channels/804011606160703521/814182556068085760/915588702489681941

Edit 2: I should note that I had to write a custom (albeit tiny) vite plugin:

```js ({ name: 'remove-server-modules-from-serve', apply: 'serve', resolveId(id) { if ( id === `url` ) { return `\0remove` } }, load(id) { if ( id === `\0remove` ) { return `export default {}` } }, }) ```
ljharb commented 2 years ago

Sounds like a vite bug.