I'm running into errors when I try to run the build artifact of a NestJS app that was created within an Nx workspace. The error only occurs when I run the resulting artifact anywhere outside of the repository where no node_modules folder exists.
Steps to reproduce:
- Clone this repo https://github.com/baumgarb/proxy-example
- Run
npm install
to install all packages - Run
ng build backend
- Go into the dist folder in
dist/apps/backend
- Run
node main.js
in that folder, you should see the backend starting up successfully - Now copy main.js to a different location outside of the cloned repository (e.g.
/tmp
orc:\temp
) - Run
node main.js
again in the new location and you'll run into the following error:
internal/modules/cjs/loader.js:775
throw err;
^
Error: Cannot find module 'tslib'
Require stack:
- /home/bernhard/main.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:772:15)
at Function.Module._lo[ad (internal/modules/cjs/loader.js:677:27)
at Module.require (internal/modules/cjs/loader.js:830:19)
at require (internal/modules/cjs/helpers.js:68:18)
...[omitted for brevity] {
code: 'MODULE_NOT_FOUND',
}
You can also try to run the build artifact in a Docker container, it will lead to the same error.
Here's also the comparison between my local machine and WSL:
Can anyone tell me what the issue is and how to fix it? Thanks a lot in advance!