vercel / vercel/ncc

Cannot find module

Open
#649 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug conditional require
Dominant language
JavaScript
Stars
9.8k
Forks
326
PR merge metrics
No merged PRs in 30d

Description

Hi,

I hit a problem after migrating from ncc 0.23.0 to 0.27.0. I'm using node 14.15.2.

To reproduce:

  1. Create an app.js file with the following content:
const fs = require('fs');
let settingsPath = './settings.json';
if (fs.existsSync(settingsPath)) {
    let settings = require(settingsPath);
    console.log(settings);
}
  1. Create a settings.json file with content: {}.
  2. Execute: ncc build app.js -o .
  3. Run the packaged application using `node index.js. This will result in the following error:
Error: Cannot find module './settings.json'
    at webpackEmptyContext (C:\projects\auroramm\sample123\index.js:21:10)
    at Object.498 (C:\projects\auroramm\sample123\index.js:11:44)
    at __nccwpck_require__ (C:\projects\auroramm\sample123\index.js:61:42)
    at C:\projects\auroramm\sample123\index.js:83:18
    at Object.<anonymous> (C:\projects\auroramm\sample123\index.js:84:12)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) {
  code: 'MODULE_NOT_FOUND'
}

Note that extracting the fs.existsSync() call in a separate variable produces proper result.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with app.js, settings.json, Node 14.15.2, and ncc 0.23.0 versus 0.27.0 using the documented ncc build command. Inspect the generated index.js around the webpackEmptyContext failure and compare it with the case where fs.existsSync is extracted; done means the packaged application can load the existing settings.json without MODULE_NOT_FOUND.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
build-system
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.