Nest doesnt want to compile dist correctly

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

Nest does not compile the dist folder correctly. I first encountered this problem when nest stopped updating my code, after a few hours I opened the dist folder:

dist
  config
  server/src <- here are app module and auth module
  tsconfig.build.tsbuildinfo

when I run npm run start or npm run start:dev I get an error:

Error: Cannot find module 'C:\Users\server\dist\main'
    at Function.Module._resolveFilename(node: internal/modules/cjs/loader:933:15)
    in Function.Module._load(node:internal/modules/cjs/loader:778:27)
    at Function.executeUserEntryPoint [as runMain](node:internal/modules/run_main:81:12)
    on node: internal/main/run_main_module:17:47

and it’s true, main is in the dist/server/src folder which has been compiled into three files main.d.ts, main.js, main.js.map but I don’t understand why the root folder of the server is duplicated and files without the root folder are not compiled (for example, just dist/main)

before that all i saw was that nest didn’t want to update my files, then i removed dist, ran npm run build to update my code but no success, nest created server folder (why?), and after that when i ran start i had the same error

Minimum reproduction code

https://github.com/lohevol/nestjs-issue/

Steps to reproduce

  1. npm run start:dev
  2. try to update anything
  3. see what nothing doesnt update or
  4. npm run start:dev
  5. delete dist (what i did when code stopped to update)
  6. npm run build (i thought it can help to update)
  7. npm run start:dev
  8. See error

Expected behavior

what nest compile files without server folder, just dist/main, dist/app.module, etc.

Package

Other package

no

NestJS version

8.0.0

Packages versions

{
  "name": "server",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@nestjs/common": "^8.0.0",
    "@nestjs/core": "^8.0.0",
    "@nestjs/mongoose": "^9.0.2",
    "@nestjs/platform-express": "^8.0.0",
    "bcryptjs": "^2.4.3",
    "class-validator": "^0.13.2",
    "mongoose": "^6.2.4",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^7.2.0"
  },
  "devDependencies": {
    "@nestjs/cli": "^8.0.0",
    "@nestjs/schematics": "^8.0.0",
    "@nestjs/testing": "^8.0.0",
    "@types/express": "^4.17.13",
    "@types/jest": "27.4.1",
    "@types/node": "^16.0.0",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^5.0.0",
    "@typescript-eslint/parser": "^5.0.0",
    "eslint": "^8.0.1",
    "jest": "^27.2.5",
    "source-map-support": "^0.5.20",
    "supertest": "^6.1.3",
    "ts-jest": "^27.0.3",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "^3.10.1",
    "typescript": "^4.3.5"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

Node.js version

16.13.2

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
lohevolcommented, Mar 6, 2022

Most likely this isn’t nest’s doing, but how Typescript’s compiler works. It will try to keep your original directory structure in the dist of it’s not a single directory (i.e. there’s a directory with Typescript, that is not ignored, outside of the top level directory [usually src]). Best is just calling the Typescript compiler and trying to call dist/main. If that’s failing, you’ve probably got a file outside of src that is being compiled.

yes, it’s true, I forgot to add the config (which is out of src folder) folder to the repository, from which I import the configuration file… after deleting the import, everything worked, but how can I use the config now?

edit now I moved config folder into server, and now ts is working and compiling all correctly thank you

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nest JS build does does not generate the dist folder
Once I update the typescript version into 3.7.2 in package.json file, dist folder generated.previously it was 3.4.1 "typescript": "3.7.2",.
Read more >
Configuration | NestJS - A progressive Node.js framework
A good approach for using this technique in Nest is to create a ConfigModule that exposes a ConfigService which loads the appropriate .env...
Read more >
Nest Deployment on App Service Linux -
npm run start:prod; node dist/main. Nest App. Azure DevOps. You can use Azure Pipelines to build your Nest application. You ...
Read more >
Deploy NestJs App into Production | by Liangjun Jiang - Medium
The frontend (it really doesn't count) is written with Vue.js, ... time you run npm run build, the .cer.pem is not copied over...
Read more >
Deploying a NestJS app to Cloud Run with Github Actions
In order to run `npm run build` we need access to the Nest CLI which is ... dist # Start the server using...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found