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

Express.js Module Resolution Errors: Missing Internal Modules Not Utilized in Project #6227

Open
git-ishaan opened this issue Dec 17, 2024 · 3 comments

Comments

@git-ishaan
Copy link

git-ishaan commented Dec 17, 2024

Hello Express Community,

I've been encountering persistent issues with my Express.js application that I haven't been able to resolve despite numerous troubleshooting attempts. Below are the details of the problems and the steps I've taken so far.

Environment:

Node.js Version: v22.12.0
Express.js Version: latest
Operating System: Windows 11

Issues Encountered:

1: Error When Running node index.js:

node:internal/modules/cjs/loader:1252
  throw err;
  ^

Error: Cannot find module './middleware/query'
Require stack:
- ...\node_modules\express\lib\application.js
- ...\node_modules\express\lib\express.js
- ...\node_modules\express\index.js
- ...\index.js       
    at Function._resolveFilename (node:internal/modules/cjs/loader:1249:15) 
    ...
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '...\node_modules\express\lib\application.js',
    '...\node_modules\express\lib\express.js',
    '...\node_modules\express\index.js',
    '...\index.js'
  ]
}

Node.js v22.12.0

2: Error When Using Nodemon (npm start):

Error: Cannot find module '...\node_modules\nodemon\node_modules\brace-expansion\index.js'. Please verify that the package.json has a valid "main" entry
    at tryPackage (node:internal/modules/cjs/loader:494:19)
    ...
  code: 'MODULE_NOT_FOUND',
  path: '...\\brace-expansion\\package.json',
  requestPath: 'brace-expansion'
}

Node.js v22.12.0

Steps Taken to Resolve:

Reinstalled node_modules: Deleted the node_modules folder and ran npm install multiple times.

Reinstalled Node.js: Completely removed Node.js and installed the latest version.

Tried Alternative Package Managers: Switched between npm, Yarn, and pnpm without success.

Tested with Other Frameworks: Created simple boilerplate projects using Fastify and NestJS, which run without issues.

Used Different Boilerplates: Even with a fresh Express boilerplate, the same errors persist.

Additional Information:

The issue persists even with a fresh Express setup.

Other Node.js frameworks like Fastify and NestJS work flawlessly on the same environment.

Suspect it might be related to Express's internal modules or compatibility with the current Node.js version.
.

Request for Assistance:

Has anyone encountered similar issues with Express.js, particularly related to missing internal modules like ./middleware/query or brace-expansion? Any insights or suggestions on how to resolve these module resolution errors would be greatly appreciated.

Thank you in advance for your help!

@bjohansebas
Copy link
Member

bjohansebas commented Dec 17, 2024

Just to confirm, you're using Express with ES modules, in a .js file, and in the package.json you don't have the type set to module?

{ //package.json
  "type": "module"
}

note: As far as I know, in the latest version of Node.js 22.12, where CJ modules can be resolved in ES modules, there are issues with libraries that use monkey patching. This will be fixed in the next version of Node.js. I'm on my phone right now, so I can provide links for this later.

@axhuwastaken
Copy link

Could you share more context about your project structure and package.json?

@git-ishaan
Copy link
Author

git-ishaan commented Dec 18, 2024

Thank you for providing some insights into the issue.

To clarify, I am creating a new boilerplate HTTP server that essentially serves as a "Hello World" application. Here are the details of my setup and the steps I've taken:

  1. Using ES Modules:
  •   Configuration: Set "type": "module" in my package.json.
    
  •   Issue: Encountering the same MODULE_NOT_FOUND errors as mentioned earlier when running the application.\
    
  1. Using CommonJS (Default Express Behavior):
  • Configuration: Removed the "type": "module" from package.json to revert to CommonJS.
    
  • Issue: Still experiencing the same MODULE_NOT_FOUND errors when executing the server.
    
  1. Node.js Versions:
  •   Current Version: v22.12.0
    
  •   Older LTS Versions:(v18) Tested with previous Long-Term Support (LTS) versions of Node.js.
    
  •    Outcome: The errors persist across different Node.js versions, indicating that the issue isn't specific to the Node.js version being used.
    
  1. Tweaking File Extensions:
  • Approach: Attempted renaming files to .cjs and .mjs as appropriate to align with CommonJS and ES Module standards.
    
  • Outcome: The errors persisted despite these changes.
    

Steps Taken So Far:

  •     Reinstalled Dependencies: Deleted node_modules and package-lock.json, then ran npm install to ensure all packages are correctly installed.
    
  •     Verified Package Integrity: Checked the express package in node_modules to confirm that all necessary files, including ./middleware/query, are present.
    
  •     Simplified Codebase: Created a minimal index.js with just the basic Express setup to rule out any code-related issues.
    
  •     Despite these efforts, the application still fails to locate the required modules, both when using ES modules and CommonJS. 
    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants