nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript πŸš€
https://nestjs.com
MIT License
67.78k stars 7.64k forks source link

Nest doesnt want to compile dist correctly #9298

Closed lohevol closed 2 years ago

lohevol commented 2 years ago

Is there an existing issue for this?

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?

Other

No response

micalevisk commented 2 years ago

your repo didn't reproduce the issue

I got this after running npm run start:dev

src/app.module.ts:5:24 - error TS2307: Cannot find module '../../config/default' or its corresponding type declarations.

import { config } from '../../config/default';

Also,

image

what if you change that sourceRoot to "src" (which is the default when bootstraping a new app)

EDIT

You might need to adjust your tsconfig.json to fulfill your needs. nest build will use tsc (typescript compiler) to build the project.

lohevol commented 2 years ago

your repo didn't reproduce the issue

I got this after running npm run start:dev

src/app.module.ts:5:24 - error TS2307: Cannot find module '../../config/default' or its corresponding type declarations.

import { config } from '../../config/default';

Also,

image

what if you change that sourceRoot to "src" (which is the default when bootstraping a new app)

EDIT

You might need to adjust your tsconfig.json to fulfill your needs. nest build will use tsc (typescript compiler) to build the project.

I said that if the error is not reproduced, error occurred with bundle, then all files are still generated nested, and nest cannot access main, as well as new files, so nest did not react to the fact that I rewrote the code and issued the same thing, even if I could erase one file under a clean one, dist did not change in the end.

And at the expense of changing the path in nest-cli.json - it didn’t work, the server folder is also generated, it’s not clear how it gets there at all, I tried to immediately change the path in tsconfig.json but also to no avail

edit Can you screen what you got in dist when reproduced repository, please?

micalevisk commented 2 years ago

rm -rf dist && npm run build

dist/
β”œβ”€β”€ app.controller.d.ts
β”œβ”€β”€ app.controller.js
β”œβ”€β”€ app.controller.js.map
β”œβ”€β”€ app.module.d.ts
β”œβ”€β”€ app.module.js
β”œβ”€β”€ app.module.js.map
β”œβ”€β”€ auth
β”‚Β Β  β”œβ”€β”€ auth.controller.d.ts
β”‚Β Β  β”œβ”€β”€ auth.controller.js
β”‚Β Β  β”œβ”€β”€ auth.controller.js.map
β”‚Β Β  β”œβ”€β”€ auth.module.d.ts
β”‚Β Β  β”œβ”€β”€ auth.module.js
β”‚Β Β  β”œβ”€β”€ auth.module.js.map
β”‚Β Β  β”œβ”€β”€ auth.service.d.ts
β”‚Β Β  β”œβ”€β”€ auth.service.js
β”‚Β Β  β”œβ”€β”€ auth.service.js.map
β”‚Β Β  └── dto
β”‚Β Β      β”œβ”€β”€ create-user.dto.d.ts
β”‚Β Β      β”œβ”€β”€ create-user.dto.js
β”‚Β Β      └── create-user.dto.js.map
β”œβ”€β”€ main.d.ts
β”œβ”€β”€ main.js
β”œβ”€β”€ main.js.map
β”œβ”€β”€ schemas
β”‚Β Β  β”œβ”€β”€ user.schema.d.ts
β”‚Β Β  β”œβ”€β”€ user.schema.js
β”‚Β Β  └── user.schema.js.map
└── tsconfig.build.tsbuildinfo

3 directories, 25 files
lohevol commented 2 years ago

rm -rf dist && npm run build

dist/
β”œβ”€β”€ app.controller.d.ts
β”œβ”€β”€ app.controller.js
β”œβ”€β”€ app.controller.js.map
β”œβ”€β”€ app.module.d.ts
β”œβ”€β”€ app.module.js
β”œβ”€β”€ app.module.js.map
β”œβ”€β”€ auth
β”‚   β”œβ”€β”€ auth.controller.d.ts
β”‚   β”œβ”€β”€ auth.controller.js
β”‚   β”œβ”€β”€ auth.controller.js.map
β”‚   β”œβ”€β”€ auth.module.d.ts
β”‚   β”œβ”€β”€ auth.module.js
β”‚   β”œβ”€β”€ auth.module.js.map
β”‚   β”œβ”€β”€ auth.service.d.ts
β”‚   β”œβ”€β”€ auth.service.js
β”‚   β”œβ”€β”€ auth.service.js.map
β”‚   └── dto
β”‚       β”œβ”€β”€ create-user.dto.d.ts
β”‚       β”œβ”€β”€ create-user.dto.js
β”‚       └── create-user.dto.js.map
β”œβ”€β”€ main.d.ts
β”œβ”€β”€ main.js
β”œβ”€β”€ main.js.map
β”œβ”€β”€ schemas
β”‚   β”œβ”€β”€ user.schema.d.ts
β”‚   β”œβ”€β”€ user.schema.js
β”‚   └── user.schema.js.map
└── tsconfig.build.tsbuildinfo

3 directories, 25 files

nestjs-issue

micalevisk commented 2 years ago

can you show your tsconfig.json? Could be hard to tell what's going on without a reproduction

lohevol commented 2 years ago

can you show your tsconfig.json? Could be hard to tell what's going on without a reproduction

tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./src", <-
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false
  }
}

nest-cli.json:

{
  "collection": "@nestjs/schematics",
  "sourceRoot": "./src" <-
}
jmcdo29 commented 2 years ago

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.

micalevisk commented 2 years ago

@lohevol read this: https://www.typescriptlang.org/tsconfig/rootDir.html and this: https://www.typescriptlang.org/tsconfig#baseUrl

lohevol commented 2 years ago

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