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
66.9k stars 7.55k forks source link

Nest app is not starting with npm run start (:dev) #2135

Closed x2pdenjo closed 5 years ago

x2pdenjo commented 5 years ago

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Sometimes, when I run e.g. npm run start:dev then Nest.js is not starting.

[nodemon] 1.18.6
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: C:\Users\kvn\workspace\kargo\kargo-client-be\src/**/*
[nodemon] starting `ts-node -r tsconfig-paths/register src/main.ts`

That's all what is coming for more than 10 minutes. I think it has something to do with performance and the scale of the project. My projects has around 30 modules.

I found a solution: I open my app.module.ts and comment almost every of my module, to make the app more lightweight. Then it runs again, and I remove every module step by step from comment block.

Module({
  providers,
  imports: [
    ConfigModule,
    MailModule,
    // ContactModule,
    // AuthModule,
    // CompanyModule,
    // AccountModule,
    // ProductModule,
  ],
  controllers: [AppController],
})
export class AppModule {}

Expected behavior

It should just start.

Minimal reproduction of the problem with instructions

I guess you can reproduce it with any bigger Nest.js project and a not so good PC/Notebook.

What is the motivation / use case for changing the behavior?

Well, people with less good PC/Notebooks can't work on my project.

Environment


Nest version: 5.4.0


For Tooling issues:
- Node version: 10.14.1  
- Platform:  Win 10 64x Home, 16 GB RAM, i7 6 Generation

Others:

zinzinday commented 5 years ago

review package.json nodemon.json and reinstall global nodemon

jmcdo29 commented 5 years ago

One option is to remove nodemon and ts-node and switch it out for tsc (regular typescript compiler) or tsc-watch (typescript compiler package in watch mode). The problem with ts-node is that it will run out of memory and have problems keeping your app running, hence why you should opt to run your app with node instead of ts-node. ts-node is a great dev tool, but I've had my issues with it over time and eventually opted for tsc-watch instead. It all comes down to dev preference though.

See my response here for a little bit more in depth of an answer about tsc-watch.

kamilmysliwiec commented 5 years ago

We already moved typescript-starter to nodemon + regular tsc. Let's track schematics issue here https://github.com/nestjs/schematics/issues/172

karocksjoelee commented 5 years ago

I am facing same issue , after created project using nest-cli . After I "npm run start:dev" , my terminal just show : Starting compilation in watch mode ... just it , no other [nodemon] or [Nest] related message .

ludusrusso commented 5 years ago

Same here!

thachp commented 5 years ago

I am facing same issue , after created project using nest-cli . After I "npm run start:dev" , my terminal just show : Starting compilation in watch mode ... just it , no other [nodemon] or [Nest] related message .

Any update here? I'm experiencing a similar problem.

thachp commented 5 years ago

Disregard my last comment. I removed the noEmit flag from tsconfig.json.. it fixed my problem.

itskemo commented 5 years ago

Simply make the following changes to config files in order to get npm run start:dev working again:

package.json

"start:dev": "concurrently --handle-input \"wait-on dist/main.js && nodemon\" \"tsc -w -p tsconfig.build.json\" ","start:dev": "concurrently --handle-input \"wait-on dist/src/main.js && nodemon\" \"tsc -w -p tsconfig.build.json\" ",

nodemon.json

"exec": "node dist/main""exec": "node dist/src/main"

image

arvinsingla commented 5 years ago

All of a sudden this recently happened in my nest codebase. The approach from @id-kemo worked, my concern is why did this all of a sudden change from compiling to dist/main.js to dist/src/main.js? Was there an underlying dependency change that triggered it?

goodoldneon commented 5 years ago

I had the same problem. It occurred when my project root had 2 folders with .ts files. For example, if I created the file foo/a.ts then I ended up with dist/src and dist/foo. If I deleted the dist and foo folders and reran npm run start:dev then everything was fine.

afiaudofia commented 5 years ago

Simply make the following changes to config files in order to get npm run start:dev working again:

package.json

"start:dev": "concurrently --handle-input \"wait-on dist/main.js && nodemon\" \"tsc -w -p tsconfig.build.json\" ","start:dev": "concurrently --handle-input \"wait-on dist/src/main.js && nodemon\" \"tsc -w -p tsconfig.build.json\" ",

nodemon.json

"exec": "node dist/main""exec": "node dist/src/main"

image

This works for me. Thanks @id-kemo

kamilmysliwiec commented 5 years ago

For better developer experience, I have just updated both typescript-starter and schematics (nest new). From now (moving forward), we'll use tsc-watch + just node for start:dev and start:debug - no concurrently and wait-on dependencies. Hence, we should get more descriptive errors (not jus empty console) :)

If you want to migrate your existing apps, follow this commit: https://github.com/nestjs/typescript-starter/commit/7f2dde2d6c5335306ec559195fb0e634edd5c314

and remove concurrently and wait-on from devDependencies

bmoe24x commented 5 years ago

For better developer experience, I have just updated both typescript-starter and schematics (nest new). From now (moving forward), we'll use tsc-watch + just node for start:dev and start:debug - no concurrently and wait-on dependencies. Hence, we should get more descriptive errors (not jus empty console) :)

If you want to migrate your existing apps, follow this commit: nestjs/typescript-starter@7f2dde2

and remove concurrently and wait-on from devDependencies

Hi Kamil,

My team and I are running into issues with this. We had a working project and are doing a rewrite and we used the Nest new project command, installed TypeORM, etc etc. and now when we use "npm run start" with "entities": [ "src/*/.entity{.ts,.js}"] our app works but with "npm run start:dev" it breaks. On the other hand when using "entities": [ "dist/*/.entity{.ts,.js}"] then start:dev works while start does not. We had a similar issue with our old project but our fix for that is not working either. Any help would be appreciated

filipjnc commented 5 years ago

@kamilmysliwiec I've created a new project a few days ago and have the same setup as after your commit. When I run npm run start:dev I get the following error:

internal/modules/cjs/loader.js:584
    throw err;
    ^
Error: Cannot find module 'C:\Users\Filip\Documents\Projects\hr-app\server\dist\main.js'

After checking, main.js is not in dist but rather in dist/src.

Do you plan on updating the package.json script?

kamilmysliwiec commented 5 years ago

@filipjnc it means that you have some other directories (not only src) that contain TS files. You should update your paths then.

@bmoe24x what about this:

"entities": [
   "src//*.entity.ts",
   "dist//*.entity.js",
]
jmcdo29 commented 5 years ago

@bmoe24x why not use

"entities": [
   join(__dirname, "/**/*.entity.ts")
]

So that it works with both dist and src without explicit dependencies?

kamilmysliwiec commented 5 years ago

@jmcdo29 because some people are using ormconfig.json to load configuration

jmcdo29 commented 5 years ago

@kamilmysliwiec ah, good point. I guess it could be made into a js file, but otherwise your solution is better :smiley:

filipjnc commented 5 years ago

@filipjnc it means that you have some other directories (not only src) that contain TS files. You should update your paths then.

Genius. Didn't know ts builds the project in relation to that. I moved that knex config file into src, got rid of the old dist and then it worked. Thanks!

bmoe24x commented 5 years ago

@filipjnc it means that you have some other directories (not only src) that contain TS files. You should update your paths then.

@bmoe24x what about this:

"entities": [
   "src//*.entity.ts",
   "dist//*.entity.js",
]

@kamilmysliwiec no luck with this in an ormconfig.json file. Works for 'npm run start' but not 'npm run start:dev' where we get the message "[TypeOrmModule] Unable to connect to the database. Retrying (1)"

kamilmysliwiec commented 5 years ago

Works for 'npm run start' but not 'npm run start:dev' where we get the message "[TypeOrmModule] Unable to connect to the database. Retrying (1)"

It's not related to "entities" property.

bmoe24x commented 5 years ago

Works for 'npm run start' but not 'npm run start:dev' where we get the message "[TypeOrmModule] Unable to connect to the database. Retrying (1)"

It's not related to "entities" property.

@kamilmysliwiec any idea what it is related to then? Changing that property is the only thing we have found to effect it so kind of struggling. It is annoying since our previous project worked with both 'npm run start' and 'npm run start:dev'

eker247 commented 5 years ago

Has anyone fix that? I have the same issue.

"Unable to connect to the database. Retrying (1)"

Tested on scripts: "start:dev": "tsc-watch -p tsconfig.build.json --onSuccess \"node dist/main.js\"", and older: "start:dev": "concurrently --handle-input \"wait-on dist/main.js && nodemon\" \"tsc -w -p tsconfig.build.json\" ",

ebadia commented 5 years ago

That's what I observed in my typeorm config:

I could not make it work for both "starts" with only one configuration

bmoe24x commented 5 years ago

That's what I observed in my typeorm config:

  • if i write ["src/*/.entity.ts"] in entities then npm run start works, but not npm run start:dev (Unable to connect to the database. SyntaxError: Unexpected token { ... )
  • if i write ["dist/*/.entity.js"] in entities then npm run start:dev works, but not npm run start (RepositoryNotFoundError)
  • if i write ["src//*.entity.ts", "dist/*/.entity.js"] in entities then npm run start works, but not npm run start:dev** (Unable to connect to the database. SyntaxError: Unexpected token { ... )

I could not make it work for both "starts" with only one configuration

@kamilmysliwiec could we reopen this or could you point us to somewhere that more closely relates to this issue? It seems that it is not an isolated issue

TautvydasDerzinskas commented 4 years ago

Guys try set your entities value like this:

entities: [path.join(__dirname, '../') + '/*/.entity{.ts,.js}'],

You may play with folder levels in path.join(__dirname, '../')

wjhurley commented 4 years ago

@TautvydasDerzinskas how is that going to fix the issue when it's the fact that TypeORM can't differentiate between file types when ran in dev mode or not? Did you see @ebadia 's post above?

That's what I observed in my typeorm config:

if i write ["src/*/.entity.ts"] in entities then npm run start works, but not npm run start:dev (Unable to connect to the database. SyntaxError: Unexpected token { ... )

if i write ["dist/*/.entity.js"] in entities then npm run start:dev works, but not npm run start (RepositoryNotFoundError)

if i write ["src//.entity.ts", "dist//.entity.js"] in entities then npm run start works, but not npm run start:dev (Unable to connect to the database. SyntaxError: Unexpected token { ... )

I could not make it work for both "starts" with only one configuration

TautvydasDerzinskas commented 4 years ago

@TautvydasDerzinskas how is that going to fix the issue when it's the fact that TypeORM can't differentiate between file types when ran in dev mode or not? Did you see @ebadia 's post above?

That's what I observed in my typeorm config: if i write ["src/*/.entity.ts"] in entities then npm run start works, but not npm run start:dev (Unable to connect to the database. SyntaxError: Unexpected token { ... ) if i write ["dist/*/.entity.js"] in entities then npm run start:dev works, but not npm run start (RepositoryNotFoundError) if i write ["src//.entity.ts", "dist//.entity.js"] in entities then npm run start works, but not npm run start:dev (Unable to connect to the database. SyntaxError: Unexpected token { ... ) I could not make it work for both "starts" with only one configuration

First of all you need to define both .entity.js and .entity.ts as in different start modes different kinds of files are being consumed.

Errors and issues mentioned by people (such as @ebadia ) in this discussion usually happen because entity files can't be resolved. Even though error messages can simply state "Unable to connect to the database...." which often is misleading.

I dealt with exact symptoms on one project myself, and using absolute paths resolved it!

entities: [path.join(__dirname, '../') + '/**/*.entity{.ts,.js}'],

Waiting for someone who is actually dealing with these problems to try it out.

wjhurley commented 4 years ago

I had the same issue and your solution did not work. yarn start:dev tried loading both js and ts files and threw an error on the first ts file. The solution for me was to set the file extension based on an environment variable.

Iamhere commented 4 years ago

npm run start and npm run start:dev work with:

    entities: [ path.join(__dirname, '../') + '**/*.entity.js' ]

nestjs verion 6.9.0

lock[bot] commented 4 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.