vercel / serve

Static file serving and directory listing
https://npmjs.com/package/serve
MIT License
9.24k stars 684 forks source link

Node.js 21: "[DEP0040] DeprecationWarning: The `punycode` module is deprecated." #789

Closed MikeMcC399 closed 2 months ago

MikeMcC399 commented 8 months ago

Description

Running serve under Node.js 21.x version causes the following deprecation notice to be displayed:

(node:24848) [DEP0040] DeprecationWarning: The punycode module is deprecated. Please use a userland alternative instead. (Use node --trace-deprecation ... to show where the warning was created)

The message is also shown in response to serve -v.

Node.js v21.0.0 (Current) was released on Oct 17, 2023.

Steps to reproduce

npm install serve -g
export NODE_OPTIONS='--trace-deprecation'
serve -v

shows

added 89 packages in 8s

24 packages are looking for funding
  run `npm fund` for details
(node:4213) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
    at node:punycode:3:9
    at BuiltinModule.compileForInternalLoader (node:internal/bootstrap/realm:392:7)
    at BuiltinModule.compileForPublicLoader (node:internal/bootstrap/realm:328:10)
    at loadBuiltinModule (node:internal/modules/helpers:101:7)
    at Module._load (node:internal/modules/cjs/loader:1001:17)
    at Module.require (node:internal/modules/cjs/loader:1235:19)
    at require (node:internal/modules/helpers:176:18)
    at Object.<anonymous> (/home/mike/n/lib/node_modules/serve/node_modules/fast-url-parser/src/urlparser.js:401:16)
    at Module._compile (node:internal/modules/cjs/loader:1376:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
14.2.1

https://nodejs.org/dist/latest-v21.x/docs/api/deprecations.html#DEP0040 lists the deprecation and shows that a runtime deprecation was added to Node.js v21.0.0 (see https://github.com/nodejs/node/pull/47202).

Library version

First reported on version: 14.2.1 Still reproducible with version: 14.2.2

Node version

v21.0.0 v21.7.3

Related issues

MikeMcC399 commented 7 months ago

The serve dependency fast-url-parser can be patched as a temporary measure. Starting with the following:

mkdir serve-test
cd serve-test
npm init -y
npm install serve
export NODE_OPTIONS='--trace-deprecation'
npx serve -v

Workaround

Use npm module patch-package

npm install patch-package

add script to package.json

"postinstall": "patch-package"

Edit node_modules\fast-url-parser\src\urlparser.js Line 401

Change

var punycode = require("punycode");

to

var punycode = require("punycode/");

then execute

npx patch-package fast-url-parser

To verify, execute again:

npx serve -v

There should be no deprecation message, even when running under Node.js 21.x.

For package managers other than npm, see README of patch-package.

tik9 commented 6 months ago

Should I execute the code on top mkdir serve-test.. and then execute the workaround?

MikeMcC399 commented 5 months ago

@tik9

Should I execute the code on top mkdir serve-test.. and then execute the workaround?

You would replace serve-test with the name of your existing repository. Sorry if that was not clear. I just used the name serve-test to demonstrate the principal of patching. You wouldn't normally be creating a new directory if you already have a repository that you are going to patch.

MikeMcC399 commented 2 months ago

Node.js 21 is now in maintenance and reaches end-of-life on June 1, 2024.