5
votes

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:

  1. Clone this repo https://github.com/baumgarb/proxy-example
  2. Run npm install to install all packages
  3. Run ng build backend
  4. Go into the dist folder in dist/apps/backend
  5. Run node main.js in that folder, you should see the backend starting up successfully
  6. Now copy main.js to a different location outside of the cloned repository (e.g. /tmp or c:\temp)
  7. 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: enter image description here

Can anyone tell me what the issue is and how to fix it? Thanks a lot in advance!

1
Any Update on a fix for this?Kevin192291

1 Answers

-1
votes

Your NestJs app has a tslib module dependency (at least, and even several others).

So you should deploy also the package.json with dependencies needed. For that, you can make an exact copy of the main development package.json from nx workspace, or use a specific one with only dependencies for deployed target. (to avoid your production hosting environment to load a lot of useless libraries).

And don't forget to call npm install before launching your NestJs app.