Bug 2404330

Summary: module search path is broken
Product: [Fedora] Fedora Reporter: Martin Pitt <mpitt>
Component: nodejs22Assignee: Jan Staněk <jstanek>
Status: CLOSED DUPLICATE QA Contact:
Severity: high Docs Contact:
Priority: unspecified    
Version: 42CC: aradchen, jstanek, sgallagh, tjuhasz
Target Milestone: ---   
Target Release: ---   
Hardware: Unspecified   
OS: Linux   
URL: https://src.fedoraproject.org/rpms/golang-github-evanw-esbuild/pull-request/13
Whiteboard:
Fixed In Version: Doc Type: ---
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2025-10-17 10:51:48 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On: 2404685    
Bug Blocks:    

Description Martin Pitt 2025-10-16 04:47:23 UTC
Fedora's nodejs-* packages ship their modules in a versioned node directory:

❱❱❱ rpm -ql nodejs-colors
/usr/lib/node_modules_22/colors
/usr/lib/node_modules_22/colors/lib
/usr/lib/node_modules_22/colors/lib/colors.js
[...]

❱❱❱ rpm -ql nodejs-underscore 
/usr/lib/node_modules_22/underscore
/usr/lib/node_modules_22/underscore/package.json
/usr/lib/node_modules_22/underscore/underscore-esm-min.js
[...]

To be honest I don't think that's very sensible, as in general, node versions are backwards compatible -- so the packaged modules will not be available for the non-default version like nodejs-24.

However, this directory does not actually exit in the search path:

❱❱❱ node -e "console.log(require.resolve.paths('colors'))"
[
  '/var/home/martin/tmp/golang-github-evanw-esbuild/node_modules',
  '/var/home/martin/tmp/node_modules',
  '/var/home/martin/node_modules',
  '/var/home/node_modules',
  '/var/node_modules',
  '/node_modules',
  '/var/home/martin/.node_modules',
  '/var/home/martin/.node_libraries',
  '/usr/lib/node'
]

The only package directory is /usr/lib/node/.

Reproducible: Always

Steps to Reproduce:
dnf install nodejs-colors
node -e 'require("colors")'

Actual Results:
Error: Cannot find module 'colors'
Require stack:
- /var/home/martin/tmp/golang-github-evanw-esbuild/[eval]
    at Module._resolveFilename (node:internal/modules/cjs/loader:1383:15)
    at defaultResolveImpl (node:internal/modules/cjs/loader:1025:19)
    at resolveForCJSWithHooks (node:internal/modules/cjs/loader:1030:22)
    at Module._load (node:internal/modules/cjs/loader:1192:37)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:237:24)
    at Module.require (node:internal/modules/cjs/loader:1463:12)
    at require (node:internal/modules/helpers:147:16)
    at [eval]:1:1
    at runScriptInThisContext (node:internal/vm:209:10) {
  code: 'MODULE_NOT_FOUND',


Expected Results:
Loading module works.

Additional Information:
I think the preferred way to fix that is to change %{nodejs_sitelib} to /usr/lib/node/, so that nodejs-* packages ship their modules there and they work across multiple versions.

If for some reason you really do want node-* packages to install into the versioned path, then the `node` command needs to be configured to actually look into /usr/lib/node_modules_22/ .

Comment 1 Martin Pitt 2025-10-16 05:40:17 UTC
> I think the preferred way to fix that is to change %{nodejs_sitelib} to /usr/lib/node/, so that nodejs-* packages ship their modules there and they work across multiple versions.

The mechanism for that is that a module's package.json can declare which versions it is compatible with:

  "engines": {
    "node": ">=18"
  },

Comment 2 Martin Pitt 2025-10-16 05:50:03 UTC
The situation is even worse in Rawhide. Check in `podman run -it --rm fedora:rawhide`.

`dnf install nodejs-colors` installs nodejs24 as a dependency (NOT -22). Then Running `node` fails due to "no such command". 

# rpm -q --requires nodejs-colors
nodejs(engine) >= 0.1.90

So apparently dnf selects the "wrong" version here, supposedly as it installs the latest one. This is showing that /usr/bin/node should really be an /etc/alternatives/node symlink, but we already discussed that some months ago and you rejected the idea. But ok, that's also not what this bug is about.

`node-24 -e 'require("colors")'` still fails with MODULE_NOT_FOUND as above, as nodejs-colors.rpm still installs into /usr/lib/node_modules_22/.

Just like `node`, `node-24` is also configured to look into /usr/lib/node:

# node-24 -e "console.log(require.resolve.paths('colors'))"
[
  '/node_modules',
  '/root/.node_modules',
  '/root/.node_libraries',
  '/usr/lib/node'
]

So all of this corroborates that %{nodejs_sitelib} *really* wants to be /usr/lib/node.

Comment 3 Jan Staněk 2025-10-16 10:15:37 UTC
We live in interesting times for nodejs packaging in Fedora, so the current state is at the same time unfortunate, broken, legacy, and by desing. Let me elaborate on the various parts of the problem.

The /usr/lib/node_modules and /usr/lib/node_modules_XY: For as far as I can tell, the packages in Fedora always lived in /usr/lib/node_modules; with the demodularization of nodejs, the /usr/lib/node_modules_XY directories were introduced, to allow multiple nodejs streams to live in parallel. Our current goal is to use /usr/lib/node_modules for packages that have no version requirements for the nodejs binary, and the versioned /usr/lib/node_modules_XY for those that for any reason need or want to use the specific version. Admittedly, the macros provided did not catch up with the latest changes yet; I'll open another bug to address that.

The /usr/lib/node and "global" packages: From what I've been told by upstream, these are sort of "legacy", and upstream would really like if people stop using them. Also, they apparently only work if you use the `require` function (CommonJS?) to pull dependencies; if you try to use the "import" machinery (ECMAScript modules?), /usr/lib/node should not be searched. Given that the current approach in Fedora is to only package "applications" as rpms which would bundle their dependencies anyway, we decided (for at least the time being) to *not* support global package installations at all.

If you need a workaround, the NODE_PATH environment variable might be able to help you add the right directory(-ies) into the require search path. See https://nodejs.org/docs/v22.20.0/api/cli.html#node_pathpath. Again, this does not work for the import command, only for the require function.

Also, the package in your examples (nodejs-color) seems to be an outlier, and from what I have written above, may be considered "legacy". That being said, I do not want to unilaterally reject your use case. Feel free to reach out with more specifics on what you are trying to do and we'll see if we can help.

---

With the above explanation, I unfortunately would consider this a WONTFIX. I'm leaving this open for a while if you or anyone would like to continue the discussion, but if nothing happens, I'll be closing it in a week or two.

Comment 4 Martin Pitt 2025-10-16 10:47:44 UTC
I'm writing this in the context of https://src.fedoraproject.org/rpms/golang-github-evanw-esbuild/pull-request/13 . Essentially I'm trying to get a working esbuild in Fedora, so that we can actually build the JS bundles of anaconda, cockpit and the like in Fedora (see https://pagure.io/packaging-committee/issue/1496)

For that we essentially want/need `node -e 'require("esbuild")` to work. Yes, I realize that this is sort of evil and legacy, and `import` would be preferred. But NODE_PATH only seems to apply to commonjs/require old-style modules, not ESM/import ones:

  NODE_PATH=/usr/lib/node_modules_22 node --input-type=module -e 'import colors from "colors";'

  → code: 'ERR_MODULE_NOT_FOUND'

  NODE_PATH=/usr/lib/node_modules_22 node -e 'require("colors")

  → that works.

Using NODE_PATH is an acceptable workaround if there is a programmatic way to detect it. Project build scripts certainly can't hardcode that path -- it's Fedora specific and will change over time. Is there some `node` invocation that spits out that path?

Note that I'm not attached to nodejs-colors, it was just one of the packaged node modules that I happened to look at, and its packaging was fairly straightforward. It basically only uses %{nodejs_sitelib}

> we decided (for at least the time being) to *not* support global package installations at all.

What does that mean? Installing nodejs-foo is a global install. You mean you want to remove all packaged node extensions from Fedora? If that's the direction, then 
https://src.fedoraproject.org/rpms/golang-github-evanw-esbuild/pull-request/13 would be opposite to that, and hence a wontfix as well, and hence we have to forever violate Fedora packaging policies as it'd just not be possible to build esbuild bundles during Fedora package rpms. (shipping bundled esbuild in node_modules/ is a no-no, as it involves architecture specific compiled go program)

Thanks for any insight!

Comment 5 Martin Pitt 2025-10-16 11:33:38 UTC
Do you know how that would look like in the modern world?

  $ node --input-type=module -e "import colors from '/usr/lib/node_modules_22/colors'"
  Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '/usr/lib/node_modules_22/colors' is not supported resolving ES modules imported from //[eval1]

That is assuming that the actual path can be probed from the system via the `node` binary -- it of course can't be hardcoded into build systems that way. But even then that doesn't seem to work that way?

Thanks!

Comment 6 Jan Staněk 2025-10-16 15:00:32 UTC
> What does that mean? Installing nodejs-foo is a global install. You mean you want to remove all packaged node extensions from Fedora?

Not remove explicitly, but state that they are not expected to work out of the box.

> hence we have to forever violate Fedora packaging policies

Actually no; NodeJS packages have a permanent exception on bundling dependencies, so it is expected that everything except the final "application" will be bundled in the "application" node_modules subdir.

> Do you know how that would look like in the modern world?

No, but IIRC the import machinery does not do any global directories; you *have to* have the dependencies in the local node_modules directory. The suggested upstream remedy is "to use symlinks" (https://nodejs.org/api/esm.html#no-node_path).

---

Your use case is actually one of those that is not neatly covered by the NodeJS packaging guidelines, so we will have to sort-of figure it as we go along and turn that into guidelines later.

I would suggest to do the following:

1. Install the javascript files from the esbuild into %{nodejs_sitelib}/esbuild. That is the canonical path for any "distribution" modules, and we treat is as such.
2. In any software you would need `Requires: nodejs-esbuild", do `rm -rf ./node_modules/esbuild && ln -s %{nodejs_sitelib}/esbuild ./node_modules/esbuild`. That should make both `require("esbuild")` and `import esbuild` work. (This should be probably provided as a script/macro in nodejs-packaging in the future).

Let me know if that helps! And sorry for the difficulties; nodejs is apparently very specific piece of software, and this is but a small window into the packaging pains we are currently trying to cut our way through.

Comment 7 Martin Pitt 2025-10-16 15:23:25 UTC
> Actually no; NodeJS packages have a permanent exception on bundling dependencies, so it is expected that everything except the final "application" will be bundled in the "application" node_modules subdir.

Again, that does not work for esbuild due to the shipped arch specific Go binary. And we can't use the packaged Fedora go binary and get the JS lib from npmjs.com. This forever runs into version incompatibilities, and there is even an upstream check in the binary program search that explicitly prevents that:

  var isValidBinaryPath = (x) => !!x && x !== "/usr/bin/esbuild";

> 1. Install the javascript files from the esbuild into %{nodejs_sitelib}/esbuild.

OK.

> The suggested upstream remedy is "to use symlinks" (https://nodejs.org/api/esm.html#no-node_path).

That's just sick... It requires hacking in the original upstream sources, which ought to be read-only. I think I'll stay with the requires() path for now..

But either way, this still hinges on the question:

> That is assuming that the actual path can be probed from the system via the `node` binary -- it of course can't be hardcoded into build systems that way.

I.e. how can I get the value of %{nodejs_sitelib} at runtime? This isn't an rpm build environment.

Thanks!

Comment 8 Jan Staněk 2025-10-17 09:41:25 UTC
> It requires hacking in the original upstream sources, which ought to be read-only.

Not really. Anything in node_modules is presumably installed from the npm registry by the npm tool (or equivalent). The symlink "hack" I proposed is just replacing the one installed by npm with the one from rpm. As such, I would even suggest doing this if you are staying with the requires() path – it is probably the one least likely to break in the future.

> How can I get the value of %{nodejs_sitelib} at runtime?

AFAIK you cannot, at least at the moment. If there was not a bug ATM, you could assume it will be set to /usr/lib/nodejs and use that. We'll try to take a look at it soon, but no promises, as the list of packaging woes is long.

Again, from upstream PoV, this approach is discouraged, and works basically only for backwards compatibility. <rant>Their values and approaches to packaging are unfortunately at times almost orthogonal to the way Fedora (and other linux distros) think packaging should be done, so we are stuck with trying to find some sort of least-hacky-compromise that would work and did not break either approach too much. So do whatever feels least ugly, but be prepared to rethink the solution if it breaks.</rant>

Comment 9 Martin Pitt 2025-10-17 10:51:48 UTC
Thanks Jan -- I feel you. I realize I'm swimming against upstream's "everything and their dog should be in `npm install`" stream.

> Anything in node_modules is presumably installed from the npm registry by the npm tool (or equivalent).

For upstream, -ish (we install it into/from git, as `npm install` just sucks - but that's malleable). For Fedora/Debian downstream, no -- that has to come from a sources tarball, we cannot run `npm i` during package build (and OMG I also really don't want to).

So I guess I'll search for a workaround to edit/restore, or copy node_modules/ or something.

> it will be set to /usr/lib/nodejs

#2404685 points to /usr/lib/node_modules , but either way -- a version agnostic path would be sufficient.

That still leaves the inability to `import` from /usr/... , but that's for me to figure out.

Thanks for the discussion! So I suppose #2404685 is the best I can hope for, so I'll close as duplicate.

*** This bug has been marked as a duplicate of bug 2404685 ***

Comment 10 Jan Staněk 2025-10-20 09:45:48 UTC
> For Fedora/Debian downstream, no -- that has to come from a sources tarball, we cannot run `npm i` during package build

Take a look at https://src.fedoraproject.org/rpms/nodejs-packaging/blob/rawhide/f/nodejs-packaging-bundler script, which is the current recommended approach for packaging npm modules into rpm. I assumed you already know about that, but I see that may not be the case. It runs `npm install` for you locally when you do a package update, then makes another tarball(s) from the resulting node_modules. It is expected that this will become another Source: in the spec file, and be installed along the actual sources of the thing you want to package.

For example on when some of the sources are edited, take a look at https://src.fedoraproject.org/rpms/nodejs-undici/, which could be similar to what you are trying to do. This package contains pre-built WASM (binary) blobs in the upstream tarball; the rpm package is setup so that the pre-built bits are stripped from the tarball on it's upload to the lookaside cache, and then later rebuild in koji. Maybe you will glean something useful from it for your use case.

> it will be set to /usr/lib/nodejs

oops, a typo… yeah, /usr/lib/node_modules should be the right version-agnostic path.

Comment 11 Martin Pitt 2025-10-27 05:56:53 UTC
(Sorry for late answer, was on PTO last week)

In https://github.com/cockpit-project/cockpit-files/pull/1206 I am putting everything together, and it works on Debian/Ubuntu already. For Fedora I am still waiting for https://src.fedoraproject.org/rpms/golang-github-evanw-esbuild/pull-request/13 but it's looking good.

Thanks Jan for the nodejs-packaging-bundler pointer! Indeed generating/checking licenses and copyright is still on the TODO list, I need it for Debian as well. But the general approach is the same, we provide a reduced/verified node_modules/ tarball as part of the upstream source which distributions can consume.