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

Naming collision when installing punycode via npm #79

Open
pdehaan opened this issue Mar 13, 2018 · 12 comments · May be fixed by #136
Open

Naming collision when installing punycode via npm #79

pdehaan opened this issue Mar 13, 2018 · 12 comments · May be fixed by #136

Comments

@pdehaan
Copy link

pdehaan commented Mar 13, 2018

I'm using Node 9.7.1, and have punycode@2.1.0 installed (via $ npm i punycode -S), but when I try and log out the punycode.version variable, I always seem to get "2.0.0", so I think my machine is still using the default/bundled/deprecated version of https://nodejs.org/api/punycode.html

How can you install this module from npm and use it in Node 8+ without it using the built-in version of the punycode module? It looks like I can use the npm version if I do something like require("./node_modules/punycode"), but that feels wrong.

$ node -e "console.log(require('punycode').version)" # 2.0.0
$ node -e "console.log(require('./node_modules/punycode').version)" # 2.1.0
$ node --version # v9.7.1

$ node -e "console.log(require.resolve('punycode'))" # punycode
$ node -e "console.log(require.resolve('./node_modules/punycode'))" # /Users/pdehaan/dev/tmp/node_modules/punycode/punycode.js
@KaiSchwarz-cnic
Copy link

KaiSchwarz-cnic commented Mar 14, 2018

Maybe package-lock.json or a npm-shrinkwarp.json file helps here.
Deeper read: https://docs.npmjs.com/files/package-locks
But really not sure if that helps..

@mathiasbynens
Copy link
Owner

IIRC there was a better way of preferring an npm-installed module over a Node-built-in module, although maybe I was dreaming. @zkat, do you know?

@zkat
Copy link

zkat commented Mar 15, 2018

This is literally impossible and unlikely to ever change. It is the first alternative in Node's module lookup algorithm, and so there's.... basically no actual way to do this besides referencing the module by path.

I would be tremendously surprised if this -ever- worked -- you might consider using a @scope for it, or simply publishing it as punycode.js (which is available! Get it while it's hot!).

@zkat
Copy link

zkat commented Mar 15, 2018

Of course, the other alternative is to make Node's built-in punycode module do a path-based lookup in a project's current dependencies, but it ain't pretty to do that, imo. I think it's important to continue enshrining node built-ins as special without getting into weird bypassy shenanigans like that? It's a case you can make with them, though, as I have no say in what their decision would be ;)

@mathiasbynens
Copy link
Owner

Thank you, @zkat!

@mathiasbynens
Copy link
Owner

I went with @zkat’s suggestion to publish a copy as punycode.js, so @pdehaan you can now use npm install punycode.js and require('punycode.js') as a workaround.

@vladikoff
Copy link

Thanks @mathiasbynens :)

@pdehaan
Copy link
Author

pdehaan commented Mar 16, 2018

Also, for my own future reference and general interest, I randomly spotted this gem in the eslint-plugin-node node/no-deprecated-api docs while closing some tabs:

⚠️ Note that userland modules don't hide core modules. For example, require("punycode") still imports the deprecated core module even if you executed npm install punycode. Use require("punycode/") to import userland modules rather than core modules.

$ node -e "console.log(require('punycode').version)" # 2.0.0
$ node -e "console.log(require('punycode/').version)" # 2.1.0

@vladikoff
Copy link

@pdehaan cool hacks!

@mathiasbynens
Copy link
Owner

Ooh, that may have been the workaround I was trying to remember! I like require('punycode/') much better than having a separate package.

@pdehaan
Copy link
Author

pdehaan commented Mar 20, 2018

Not that this is a democracy, but I like the new package name approach much better. It's a lot more obvious what's happening, and you wouldn't have to try explaining the odd looking trailing slash in the require statement.
It always feels weird to me that you can publish npm modules which have the same name as core Node modules (a la "fs", "path", "os", "crypto", etc). Seems super confusing, and the number of times I've seen people include "path" in their package.json is scary.

@michael-letcher
Copy link

michael-letcher commented May 22, 2018

@mathiasbynens Can punycode.js package be updated to 2.1.1 as well?

As I agree with @pdehaan as why require the use of a "hack" to be able to use the package.

skeggse added a commit to skeggse/isemail that referenced this issue Oct 21, 2018
We currently use the built-in (and deprecated) punycode module instead of the userland dependency we pull in. Per mathiasbynens/punycode.js#79 (comment), we can simply add a slash to force Node's resolution algorithm to ignore the core module.
whymarrh added a commit to whymarrh/punycode.js that referenced this issue Oct 21, 2020
Refs mathiasbynens#79

This change updates the usage instructions in the README to explain
the workaround required to use this module. As userland modules do not
hide core modules, a trailing slash is required to have Node.js use this
module over the built-in.
mathiasbynens pushed a commit that referenced this issue Oct 23, 2020
Refs #79

This change updates the usage instructions in the README to explain
the workaround required to use this module. As userland modules do not
hide core modules, a trailing slash is required to have Node.js use this
module over the built-in.
mei23 added a commit to mei23/misskey that referenced this issue Apr 3, 2021
@msimerson msimerson linked a pull request Mar 27, 2024 that will close this issue
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

Successfully merging a pull request may close this issue.

7 participants
@zkat @mathiasbynens @vladikoff @KaiSchwarz-cnic @pdehaan @michael-letcher and others