help icon indicating copy to clipboard operation
help copied to clipboard

Error: Cannot find module 'is-odd'

Open vfilatov opened this issue 1 year ago • 17 comments

Version

v20.12.1

Platform

Linux dev 5.10.0-28-amd64 nodejs/node#1 SMP Debian 5.10.209-2 (2024-01-31) x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

The original issue https://github.com/sveltejs/kit/issues/11912

git clone https://github.com/gtm-nayan/node-socket-import-error.git
cd node-socket-import-error/
rm package-lock.json pnpm-lock.yaml
pnpm i
node index.mjs

How often does it reproduce? Is there a required condition?

No response

What is the expected behavior? Why is that the expected behavior?

If package installed with npm i it does not throw the error below

What do you see instead?

node:internal/modules/cjs/loader:1048
  const err = new Error(message);
              ^

Error: Cannot find module 'is-odd'
Require stack:
- /tmp/node-socket-import-error/node_modules/is-even/index.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1048:15)
    at Module._load (node:internal/modules/cjs/loader:901:27)
    at Module.require (node:internal/modules/cjs/loader:1115:19)
    at require (node:internal/modules/helpers:130:18)
    at Object.<anonymous> (/tmp/node-socket-import-error/node_modules/is-even/index.js:10:13)
    at Module._compile (node:internal/modules/cjs/loader:1233:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1287:10)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Module._load (node:internal/modules/cjs/loader:938:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:165:29) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/tmp/node-socket-import-error/node_modules/is-even/index.js' ]
}

Node.js v20.5.1

Additional information

No response

vfilatov avatar Apr 15 '24 15:04 vfilatov

Hi! This repo is issues with the NodeJS core, and this looks like an issue with a package installation.

But, to answer your question, modules are usually installed with npm install <module> (or similar, depending on your package manager). Installed packages typically appear in ./node_modules/<module>, so if that folder doesn't exist, than the package is not installed.

If your experiencing an issue with pnpm, please file an issue in their repository.

Thank you!

avivkeller avatar Apr 15 '24 17:04 avivkeller

Thanks!

@VoltrexKeyva, I believe this is a wrong-repo issue. Could you label it when you get a chance? Thank you!

Pinging you as you edited the question (and are a member of the triage team)

avivkeller avatar Apr 15 '24 19:04 avivkeller

Hi! This repo is issues with the NodeJS core, and this looks like an issue with a package installation.

But, to answer your question, modules are usually installed with npm install <module> (or similar, depending on your package manager). Installed packages typically appear in ./node_modules/<module>, so if that folder doesn't exist, than the package is not installed.

If your experiencing an issue with pnpm, please file an issue in their repository.

Thank you!

They insist that is a nodejs bug. https://github.com/pnpm/pnpm/issues/7931#issuecomment-2093152936

vfilatov avatar May 03 '24 14:05 vfilatov

Both forms of code run fine, so the dependencies are installed correctly

// index.js
// node ./index.js

const isEven = require('is-even')
console.log(isEven(1))
// index.mjs
// node ./index.mjs
import * as isEven from 'is-even'
console.log(isEven.default(1))

Esbuild can also bundle correctly

 esbuild ./index.mjs --bundle --platform=node

ahaoboy avatar May 05 '24 06:05 ahaoboy

I found an interesting thing. This line of code will affect the execution results. After commenting it out, it will only report an error the first time it is run. After that, it can run correctly every time. After removing the comment, it will report an error every time it is run.

import fs from 'fs';
import { createServer } from 'net';
const mySocketFile = '/tmp/mysocketfile';

if (fs.statSync(mySocketFile, { throwIfNoEntry: false }))
  fs.unlinkSync(mySocketFile);

// magic line
createServer((socket) => { }).listen(mySocketFile);

setTimeout(async () => {
  const isEven = await import("is-even")
  console.log("isEven: ", isEven.default(1))
});
 node --version
v22.1.0

ahaoboy avatar May 05 '24 08:05 ahaoboy

@vfilatov Can you test using node 18? On my machine node18 doesn't have this problem

ahaoboy avatar May 05 '24 09:05 ahaoboy

@vfilatov Can you test using node 18? On my machine node18 doesn't have this problem

Yes, this particular example working on node18. But this example is a simplified code from the original issue, which NOT working on node18 either.

https://github.com/sveltejs/kit/issues/11912

vfilatov avatar May 08 '24 12:05 vfilatov

I found an interesting thing. This line of code will affect the execution results. After commenting it out, it will only report an error the first time it is run. After that, it can run correctly every time. After removing the comment, it will report an error every time it is run.

import fs from 'fs';
import { createServer } from 'net';
const mySocketFile = '/tmp/mysocketfile';

if (fs.statSync(mySocketFile, { throwIfNoEntry: false }))
  fs.unlinkSync(mySocketFile);

// magic line
createServer((socket) => { }).listen(mySocketFile);

setTimeout(async () => {
  const isEven = await import("is-even")
  console.log("isEven: ", isEven.default(1))
});
 node --version
v22.1.0

Yes, that is the point! Initially I though the problem is on the SvelteKit side, but this simplified code excludes all SvelteKit relations. It is either pnpm or node. As you found the (simplified) code working under node18 that makes me suspect the code might be on node core...

vfilatov avatar May 08 '24 12:05 vfilatov

Hi! If you delete the package-lock and pnpm-lock files and run npm i, then test the code in the latest version (v20.1.3 for LTS), will this issue still occur?

avivkeller avatar May 08 '24 23:05 avivkeller

Hi! If you delete the package-lock and pnpm-lock files and run npm i, then test the code in the latest version (v20.1.3 for LTS), will this issue still occur?

Yes, it is working when I use npm I mean there is no issues when using npm

vfilatov avatar May 08 '24 23:05 vfilatov

So this probably isn't an issue with npm. What happens when you follow the same instructions but use pnpm instead?

avivkeller avatar May 08 '24 23:05 avivkeller

So this probably isn't an issue with npm. What happens when you follow the same instructions but use pnpm instead?

when I use pnpm and node 20, I get the error above. when I use pnpm and node 18, I do not get the error above. when I use npm and any node, I do not get the error above.

vfilatov avatar May 08 '24 23:05 vfilatov

So, despite what the pnpm team may have said, it really doesn't seem like an issue with Node.

avivkeller avatar May 08 '24 23:05 avivkeller

So, despite what the pnpm team may have said, it really doesn't seem like an issue with Node.

It might an issue on both sides... As something changed between node18 & node20... Would be great if somebody could simply debug the code and find that "breaking point". May guess that bug could be fixed by either team.

vfilatov avatar May 08 '24 23:05 vfilatov

Well, the change from Node.js v18 to Node.js v20 was breaking, as with major release lines, so breakages are expected, maybe the pnpm team knows?

avivkeller avatar May 08 '24 23:05 avivkeller

Well, the change from Node.js v18 to Node.js v20 was breaking, as with major release lines, so breakages are expected, maybe the pnpm team knows?

Someone more familiar with both libraries would need to investigate this in depth, and as a short-term solution, maybe try running it with bun for now?

ahaoboy avatar May 12 '24 02:05 ahaoboy

Marking as NPM ad this regards it vs pnpm

avivkeller avatar May 12 '24 02:05 avivkeller

As this is a still not reproducible via NPM, I'm gonna go ahead and close this issue. I know this isn't the answer you wanted, but It doesn't seem actionable. If you continue to experience errors, please open a new issue here or at npm/cli.

avivkeller avatar Jul 15 '24 22:07 avivkeller

Here is the problem I see. The issue involves nodejs & pnpm. Each one of the core developers blame the other side. Would it be possible for you guys to talk to each other and find a solution?

Thanks

On Mon, Jul 15, 2024 at 6:20 PM Aviv Keller @.***> wrote:

As this is a still not reproducible via NPM, I'm gonna go ahead and close this issue. I know this isn't the answer you wanted, but It doesn't seem actionable. If you continue to experience errors, please open a new issue here or at npm/cli.

— Reply to this email directly, view it on GitHub https://github.com/nodejs/help/issues/4372#issuecomment-2229533768, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJMTYRO4R6VN5IG4OPTXFTZMRDJZAVCNFSM6AAAAABGIVIOYSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMRZGUZTGNZWHA . You are receiving this because you were mentioned.Message ID: @.***>

vfilatov avatar Jul 15 '24 22:07 vfilatov

Each one of the core developers blame the other side.

Just want to point out a few things:

  1. I'm not a Node.js core collaborator. While I'm a member of the organization, my role is as issue triage and support

  2. I wouldn't say I'm "blaming" the PNPM team, I just don't think this is an issue with Node.js, as (AFAICT) it's not reproducible.

CC @nodejs/npm

avivkeller avatar Jul 15 '24 23:07 avivkeller

Pnpm team insists this issue appears only in the nodejs v20, not in v18 (that is true). Apparently (I don't aggree with that) issue on the node side.

The real issue is that nobody is willing to determine the breaking point.

Unfortunately my js experience doesn't let me dive that deep.

Thanks

On Mon, Jul 15, 2024, 19:33 Aviv Keller @.***> wrote:

Each one of the core developers blame the other side.

Just want to point out a few things:

I'm not a Node.js core collaborator. While I'm a member of the organization, my role is as issue triage and support 2.

I wouldn't say I'm "blaming" the PNPM team, I just don't think this is an issue with Node.js, as (AFAICT) it's not reproducible.

CC @nodejs/npm https://github.com/orgs/nodejs/teams/npm

— Reply to this email directly, view it on GitHub https://github.com/nodejs/help/issues/4372#issuecomment-2229617972, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJMTYVOWYHC6VL2NKPKGRDZMRL3JAVCNFSM6AAAAABGIVIOYSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMRZGYYTOOJXGI . You are receiving this because you were mentioned.Message ID: @.***>

vfilatov avatar Jul 15 '24 23:07 vfilatov

I've CCd the NPM team to take a look, but since this issue your describing appears to only affect PNPM (at least in the way you have described), I think that tracking it should be done in the PNPM repo. I'm not saying it's their "fault", I just think it's more actionable in that repo rather than this one.

avivkeller avatar Jul 15 '24 23:07 avivkeller

I am not familiar with the underlying mechanisms of node and pnpm, but I found some very strange nodejs behaviors when reproducing this problem.

 node --version
v22.4.1

const fs = require('fs')
const { createServer } = require('net')
const mySocketFile = '/tmp/mysocketfile';

if (fs.statSync(mySocketFile, { throwIfNoEntry: false }))
  fs.unlinkSync(mySocketFile);

// magic line
createServer((socket) => { }).listen(mySocketFile);

setTimeout(async () => {
  try {
    console.log('resolve: ', require.resolve('is-even'))
    require('is-even')
  } catch (e) {
    console.log("err:", e)
  }
});

Run the above code multiple times. If an error occurs, the output should be a file path similar to the following:

resolve:  /root/code/npm-bug/node_modules/is-even/index.js

Comment out the magic line and run it multiple times. It should only report an error the first time. If there is no error, it should output a path similar to the following

resolve:  /root/code/npm-bug/node_modules/.pnpm/[email protected]/node_modules/is-even/index.js

This seems to indicate that the createServer function causes the behavior of nodejs resolve to change. I don't know what the specific mechanism is. If anyone wants to delve into this issue, I hope it will help.

ahaoboy avatar Jul 16 '24 01:07 ahaoboy

Thanks for the info, @ahaoboy! This is certainly strange. My gut instinct would be to check how symlinks are handled, but I really don't know much about this part of core. I've CC'd relevant teams.

CC @nodejs/net - createServer weird cases CC @nodejs/loaders - Knows the most about require

avivkeller avatar Jul 16 '24 01:07 avivkeller

Sorry for the earlier close, @vfilatov. It's unclear how/why this issue occurs, but I've reopened the issue for tracking.

avivkeller avatar Jul 16 '24 02:07 avivkeller

Please pay attention, this happens only on IPC, while TCP (the code below) working fine.

import fs from 'fs';
import { createServer } from 'net';
const mySocketFile = '/tmp/mysocketfile';

if (fs.statSync(mySocketFile, { throwIfNoEntry: false }))
  fs.unlinkSync(mySocketFile);

// magic line
//createServer((socket) => { }).listen(mySocketFile);
createServer((socket) => { }).listen(8080);

setTimeout(async () => {
  const isEven = await import("is-even")
  console.log("isEven: ", isEven.default(1))
});

vfilatov avatar Jul 16 '24 15:07 vfilatov

Hey everyone, I found the culprit: https://github.com/nodejs/node/issues/51142.

I'm tempted to close this in favor of that, as they appear to be the same issue. LMK if you disagree.

(Duplicate of https://github.com/nodejs/node/issues/51142)

avivkeller avatar Jul 23 '24 00:07 avivkeller