vercel / ncc

Compile a Node.js project into a single file. Supports TypeScript, binary addons, dynamic requires.
https://npmjs.com/@vercel/ncc
MIT License
8.99k stars 285 forks source link

Why i got multiple js file, and Cannot find module './indexes' ? #1189

Open seepine opened 1 month ago

seepine commented 1 month ago

I ncc a fastify project with typeorm, run ncc build app.ts -o dist and like this

$ npm run ncc

> fastify-template-ts@1.0.0 ncc
> ncc build app.ts -o dist

ncc: Version 0.38.1
ncc: Compiling file index.js into CJS
ncc: Using typescript@5.4.5 (local user-provided)
   0kB  dist/file.js
   1kB  dist/worker-pipeline.js
   1kB  dist/worker.js
   5kB  dist/worker1.js
7024kB  dist/index.js
7031kB  [5705ms] - ncc 0.38.1
image

And try run node index.js

$ node index.js

node:internal/event_target:1088
  process.nextTick(() => { throw err; });
                           ^
Error: Cannot find module './indexes'
Require stack:
- /Users/seepine/workspace/test-transfer/dist/worker1.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15)
    at Module._load (node:internal/modules/cjs/loader:985:27)
    at Module.require (node:internal/modules/cjs/loader:1235:19)
    at require (node:internal/modules/helpers:176:18)
    at Object.<anonymous> (/Users/seepine/workspace/test-transfer/dist/worker1.js:5:37)
    at Module._compile (node:internal/modules/cjs/loader:1376:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
Emitted 'error' event on Worker instance at:
    at [kOnErrorMessage] (node:internal/worker:326:10)
    at [kOnMessage] (node:internal/worker:337:37)
    at MessagePort.<anonymous> (node:internal/worker:232:57)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:814:20)
    at exports.emitMessage (node:internal/per_context/messageport:23:28) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/Users/seepine/workspace/test-transfer/dist/worker1.js' ]
}

Node.js v20.10.0
styfle commented 1 month ago

Its hard to know without the source code. Usually this is caused by a dynamic require.

You can check in /Users/seepine/workspace/test-transfer/dist/worker1.js for something like require(str) and change it to require('./indexes') perhaps.

seepine commented 1 month ago

ncc build app.ts -o dist

Hi, try index.ts

import fastify from "fastify";
const server = fastify({});

server.listen({
  host: "0.0.0.0",
  port: 3000
});
pnpm i fastify
pnpm i @vercel/ncc -D

Add scripts to package.json

"ncc": "ncc build index.ts -o dist-ncc"

And run

pnpm ncc