When TSC suddenly errors with cannot find module
TLDRโ
- If you have this error, double-check if your
rootDir
is consistent. - I got this error from TSC, auto-flattening the folder structure.
On my TypeScript Node Server, I suddenly got the following error on the tsc
command for production settings.
internal/modules/cjs/loader.js:{number}
throw err;
Error: Cannot find module '{project}/dist'
at ... {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
Then I stashed my work and started traveling back in time with git checkout HASH
. Comes out, the error started when I added MongoDB Models at src/models
.
It seemed strange since it had nothing to do with adding new modules or dependencies. Reinstalling node_modules
did not do the job for me (Relevant Stack Overflow Question here). Please take a look at my folder structure.
.
โโโ LICENSE
โโโ README.md
โโโ dist
โโโ package-lock.json
โโโ package.json
โโโ src
โ โโโ models (Newly added. Started to cause an error.)
โ โ โโโ user.ts (Newly added. Started to cause an error.)
โ โโโ server
โ โโโ config
โ โ โโโ config.ts
โ โ โโโ dev.env
โ โ โโโ dev.env.sample
โ โ โโโ prod.env
โ โ โโโ prod.env.sample
โ โโโ index.ts
โโโ tsconfig.json
Long story short, it was the problem in my tsconfig
. I have previously declared the following statement on my tsconfig
.
{
...
"include": ["src/**/*"]
}
However, since there was only a /server
folder before creating the model, it seems that TSC has automatically set the root directory to src/server
. Therefore the dist
output seemed like the following.
dist
โโโ config
โ โโโ config.js
โ โโโ prod.env
โโโ index.js
But after models/user.ts
was added, src
contained both models
and server
directories, recognizing the root directory as src
. So it now became:
dist
โโโ models
โ โโโ user.js
โโโ server
โโโ config
โ โโโ config.js
โ โโโ prod.env
โโโ index.js
Notice the directory structure has changed. My entire npm commands
were based as if src/server
was a root directory (as if the index was at dist/index.js
), so that began to cause the error. Therefore I updated the npm commands
. Note that I changed dist
s to dist/server
s.
rm -rf dist
&& tsc
- && cp ./src/server/config/prod.env ./dist/config/prod.env
&& export NODE_ENV=prod
- && node dist
rm -rf dist
&& tsc
+ && cp ./src/server/config/prod.env ./dist/server/config/prod.env
&& export NODE_ENV=prod
+ && node dist/server
To prevent TSC from guessing the root directory, you can add the following line on your tsconfig.json
.
{
"compilerOptions": {
...
"rootDir": "src",
}
}
This line will retain the absolute folder structure from src
.