punycode is deprecated in npm - what should I replace it with?

91.3k views Asked by At

I'm using the npm module punycode in my Angular project. VsCode tells me it's deprecated and https://nodejs.org/api/punycode.html#punycode_punycode confirms:

The version of the punycode module bundled in Node.js is being deprecated. In a future major version of Node.js this module will be removed. Users currently depending on the punycode module should switch to using the userland-provided Punycode.js module instead.

The suggestion is to switch to the 'userland-provided' module. What is that?

There is a link to https://github.com/mathiasbynens/punycode.js and I tried including that in my package.json instad of 'punycode' and I get the same error.

8

There are 8 answers

4
LEMON On

First:

npm install punycode --save

Second:

Then in node_modules go to the directory tr46 > index.js

// Replace this:
const punycode = require('punycode');
// With this:
const punycode = require('punycode/');
0
Sanchyan Chakraborty On

Refer to this GitHub issue

In my case, despite not using punycode directly in my codebase, Nodemailer relies on it. To address this, I implemented a custom script, as detailed in the linked GitHub issue.

If your situation differs, you may need to identify which library is utilizing punycode and apply a similar workaround until an official fix is released. Feel free to share a test case publicly, and I'd be happy to assist you further.

1
Akash On

It is deprecated on node version 21.0.0. Use version node version 18.

nvm use 18
2
Wesley Silva On

Recently I faced this same issue.

I highly recommend you use the LTS(long term support) version of the node.

You can validate the version here: https://nodejs.org/en

when I'm writing this answer the node LTS version is 20.10.0

so then in your command line you should perform:

$nvm install 20.10.0
$nvm use 20.10.0

This solves the problem, because makes you use the most tested and approved version.

If I use the version with latest features (actually 21.5.0) I will face the problem reported here. There is an issue open in github to solve it by the way.

You can see more details here: https://github.com/yarnpkg/yarn/issues/9005

1
BkackBob On

From my experience, this solution with ('punycode/') - does not work. It doesn't seem to have any problems with any LTS version, but on anything 21.^ and above - unfortunately not a single hack I've seen - does NOT work (so far). I tried the final LTS version (v20.11.0) this morning and it works.

0
sharon On

I also got the message:

(node:14220) [DEP0040] DeprecationWarning: The punycode module is deprecated. Please use a userland alternative instead.

I solved it by running npm update ajv.

AJV stands for Another JSON Schema Validator.

0
Allan Bazinet On

Frequently, usage of the punycode module was simply for conversion of a domain name to ASCII, e.g., when MIME-encoding an address.

Yes, the module does a lot more, but commonly, you're just using the toASCII() function, handing it a domain name. For that, this deprecation, confusion over the magic trailing slash, further confusion when using ESM imports, all ends up being a bit complicated.

If that's the case, then the domainToASCII() function in the node:url package might be a simpler way to get your desired result.

1
Илья Куприянов On

I'm used Ubuntu and I have similar error. Clearing cache don't solved my problem. After remove nodejs and installation via nvm, problem has been solved

source: https://tecadmin.net/how-to-install-nvm-on-ubuntu-22-04/