4

Hot reloading is not working. The API is not being updated after changes in the code are saved. Here is the code:

https://codesandbox.io/s/practical-snowflake-c4j6fh

When bulding (docker-compose up -V --build) I get the following messages on terminal:

2022-12-16 09:29:53 redis                | 1:C 16 Dec 2022 12:29:53.411 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo</br>2022-12-16 09:29:53 redis                | 1:C 16 Dec 2022 12:29:53.411 # Redis version=7.0.6, bits=64, commit=00000000, modified=0, pid=1, just started</br>2022-12-16 09:29:53 redis                | 1:C 16 Dec 2022 12:29:53.411 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf</br>2022-12-16 09:29:53 redis                | 1:M 16 Dec 2022 12:29:53.411 * monotonic clock: POSIX clock_gettime</br>2022-12-16 09:29:53 redis                | 1:M 16 Dec 2022 12:29:53.411 * Running mode=standalone, port=6379.</br>2022-12-16 09:29:53 redis                | 1:M 16 Dec 2022 12:29:53.411 # Server initialized</br>2022-12-16 09:29:53 redis                | 1:M 16 Dec 2022 12:29:53.411 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can can also cause failures without low memory condition, see https://github.com/</br>jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.</br>2022-12-16 09:29:53 redis                | 1:M 16 Dec 2022 12:29:53.412 * Ready to accept connections</br>2022-12-16 09:29:53 postgres             | The files belonging to this database system will be owned by user "postgres".</br>2022-12-16 09:29:53 postgres             | This user must also own the server process.</br>2022-12-16 09:29:53 postgres             | </br>2022-12-16 09:29:53 postgres             | The database cluster will be initialized with locale "en_US.utf8".</br>2022-12-16 09:29:53 postgres             | The default database encoding has accordingly been set to "UTF8".</br>2022-12-16 09:29:53 postgres             | The default text search configuration will be set to "english".</br>2022-12-16 09:29:53 postgres             | </br>2022-12-16 09:29:53 postgres             | Data page checksums are disabled.</br>2022-12-16 09:29:53 postgres             | </br>2022-12-16 09:29:53 postgres             | fixing permissions on existing directory /var/lib/postgresql/data ... ok</br>2022-12-16 09:29:53 postgres             | creating subdirectories ... ok</br>2022-12-16 09:29:53 postgres             | selecting dynamic shared memory implementation ... posix</br>2022-12-16 09:29:53 postgres             | selecting default max_connections ... 100</br>2022-12-16 09:29:53 postgres             | selecting default shared_buffers ... 128MB</br>2022-12-16 09:29:53 postgres             | selecting default time zone ... Etc/UTC</br>2022-12-16 09:29:53 postgres             | creating configuration files ... ok</br>2022-12-16 09:29:53 postgres             | running bootstrap script ... ok</br>2022-12-16 09:29:54 postgres             | performing post-bootstrap initialization ... ok</br>2022-12-16 09:29:54 postgres             | initdb: warning: enabling "trust" authentication for local connections</br>2022-12-16 09:29:54 postgres             | You can change this by editing pg_hba.conf or using the option -A, or</br>2022-12-16 09:29:54 postgres             | --auth-local and --auth-host, the next time you run initdb.</br>2022-12-16 09:29:54 postgres             | syncing data to disk ... ok</br>2022-12-16 09:29:54 postgres             | </br>2022-12-16 09:29:54 postgres             | </br>2022-12-16 09:29:54 postgres             | Success. You can now start the database server using:</br>2022-12-16 09:29:54 postgres             | </br>2022-12-16 09:29:54 postgres             |     pg_ctl -D /var/lib/postgresql/data -l logfile start</br>2022-12-16 09:29:54 postgres             | </br>2022-12-16 09:29:54 postgres             | waiting for server to start....2022-12-16 12:29:54.305 UTC [48] LOG:  starting PostgreSQL 12.13 (Debian 12.13-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit</br>2022-12-16 09:29:54 postgres             | 2022-12-16 12:29:54.311 UTC [48] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"</br>2022-12-16 09:29:54 store-backend-api-1  | </br>2022-12-16 09:29:54 store-backend-api-1  | > store-backend@0.0.1 start:dev</br>2022-12-16 09:29:54 store-backend-api-1  | > nest start --watch</br>2022-12-16 09:29:54 store-backend-api-1  | </br>2022-12-16 09:29:54 postgres             | 2022-12-16 12:29:54.338 UTC [49] LOG:  database system was shut down at 2022-12-16 12:29:54 UTC</br>2022-12-16 09:29:54 postgres             | 2022-12-16 12:29:54.345 UTC [48] LOG:  database system is ready to accept connections</br>2022-12-16 09:29:54 postgres             |  done</br>2022-12-16 09:29:54 postgres             | server started</br>2022-12-16 09:29:54 postgres             | </br>2022-12-16 09:29:54 postgres             | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*</br>2022-12-16 09:29:54 postgres             | </br>2022-12-16 09:29:54 postgres             | 2022-12-16 12:29:54.434 UTC [48] LOG:  received fast shutdown request</br>2022-12-16 09:29:54 postgres             | waiting for server to shut down....2022-12-16 12:29:54.444 UTC [48] LOG:  aborting any active transactions</br>2022-12-16 09:29:54 postgres             | 2022-12-16 12:29:54.445 UTC [48] LOG:  background worker "logical replication launcher" (PID 55) exited with exit code 1</br>2022-12-16 09:29:54 postgres             | 2022-12-16 12:29:54.445 UTC [50] LOG:  shutting down</br>2022-12-16 09:29:54 postgres             | 2022-12-16 12:29:54.482 UTC [48] LOG:  database system is shut down</br>2022-12-16 09:29:54 postgres             |  done</br>2022-12-16 09:29:54 postgres             | server stopped</br>2022-12-16 09:29:54 postgres             | </br>2022-12-16 09:29:54 postgres             | PostgreSQL init process complete; ready for start up.</br>2022-12-16 09:29:54 postgres             | </br>2022-12-16 09:29:54 postgres             | 2022-12-16 12:29:54.552 UTC [1] LOG:  starting PostgreSQL 12.13 (Debian 12.13-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit</br>2022-12-16 09:29:54 postgres             | 2022-12-16 12:29:54.552 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432</br>2022-12-16 09:29:54 postgres             | 2022-12-16 12:29:54.552 UTC [1] LOG:  listening on IPv6 address "::", port 5432</br>2022-12-16 09:29:54 postgres             | 2022-12-16 12:29:54.563 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"</br>2022-12-16 09:29:54 postgres             | 2022-12-16 12:29:54.592 UTC [67] LOG:  database system was shut down at 2022-12-16 12:29:54 UTC</br>2022-12-16 09:29:54 postgres             | 2022-12-16 12:29:54.600 UTC [1] LOG:  database system is ready to accept connections</br>

And then the previous messages disappear and the following ones are shown:

[12:29:55 PM] Starting compilation in watch mode...</br>2022-12-16 09:29:55 store-backend-api-1  | </br>2022-12-16 09:29:58 store-backend-api-1  | [12:29:58 PM] Found 0 errors. Watching for file changes.</br>2022-12-16 09:29:58 store-backend-api-1  | </br>2022-12-16 09:29:59 store-backend-api-1  | [Nest] 29  - 12/16/2022, 12:29:59 PM     LOG [NestFactory] Starting Nest application...</br>2022-12-16 09:29:59 store-backend-api-1  | [Nest] 29  - 12/16/2022, 12:29:59 PM     LOG [InstanceLoader] TypeOrmModule dependencies initialized +65ms</br>2022-12-16 09:29:59 store-backend-api-1  | [Nest] 29  - 12/16/2022, 12:29:59 PM     LOG [InstanceLoader] ConfigHostModule dependencies initialized +1ms</br>2022-12-16 09:29:59 store-backend-api-1  | [Nest] 29  - 12/16/2022, 12:29:59 PM     LOG [InstanceLoader] AppModule dependencies initialized +0ms</br>2022-12-16 09:29:59 store-backend-api-1  | [Nest] 29  - 12/16/2022, 12:29:59 PM     LOG [InstanceLoader] ConfigModule dependencies initialized +1ms</br>2022-12-16 09:29:59 store-backend-api-1  | [Nest] 29  - 12/16/2022, 12:29:59 PM     LOG [InstanceLoader] TypeOrmCoreModule dependencies initialized +49ms</br>2022-12-16 09:29:59 store-backend-api-1  | [Nest] 29  - 12/16/2022, 12:29:59 PM     LOG [InstanceLoader] TypeOrmModule dependencies initialized +0ms</br>2022-12-16 09:29:59 store-backend-api-1  | [Nest] 29  - 12/16/2022, 12:29:59 PM     LOG [InstanceLoader] UserModule dependencies initialized +1ms</br>2022-12-16 09:29:59 store-backend-api-1  | [Nest] 29  - 12/16/2022, 12:29:59 PM     LOG [RoutesResolver] AppController {/api}: +7ms</br>2022-12-16 09:29:59 store-backend-api-1  | [Nest] 29  - 12/16/2022, 12:29:59 PM     LOG [RouterExplorer] Mapped {/api, GET} route +4ms</br>2022-12-16 09:29:59 store-backend-api-1  | [Nest] 29  - 12/16/2022, 12:29:59 PM     LOG [RouterExplorer] Mapped {/api/test, GET} route +0ms</br>2022-12-16 09:29:59 store-backend-api-1  | [Nest] 29  - 12/16/2022, 12:29:59 PM     LOG [RoutesResolver] UserController {/api/users}: +1ms</br>2022-12-16 09:29:59 store-backend-api-1  | [Nest] 29  - 12/16/2022, 12:29:59 PM     LOG [RouterExplorer] Mapped {/api/users, POST} route +1ms</br>2022-12-16 09:29:59 store-backend-api-1  | [Nest] 29  - 12/16/2022, 12:29:59 PM     LOG [RouterExplorer] Mapped {/api/users, GET} route +1ms</br>2022-12-16 09:29:59 store-backend-api-1  | [Nest] 29  - 12/16/2022, 12:29:59 PM     LOG [NestApplication] Nest application successfully started +3ms
user3788133
  • 163
  • 2
  • 10

5 Answers5

5

Add this to tsconfig.json.

"watchOptions": {
    // Use native file system events for files and directories
    "watchFile": "priorityPollingInterval",
    "watchDirectory": "dynamicprioritypolling",
    // Poll files for updates more frequently
    // when they're updated a lot.
    "fallbackPolling": "dynamicPriority",
    // Don't coalesce watch notification
    "synchronousWatchDirectory": true,
    // Finally, two additional settings for reducing the amount of possible
    // files to track  work from these directories
    "excludeDirectories": ["**/node_modules", "dist"]
  }
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 26 '22 at 19:13
  • It works but do you know if this has impact on prod environment? – GianPierre Gálvez May 28 '23 at 04:39
1

Try this. I downloaded your code an tested it. You have a permission problem. Remove container and delete docker volumes , then run docker compose with this changes in the Dockerfile:

###################
# BUILD FOR LOCAL DEVELOPMENT
###################

FROM node:18-alpine As development
USER root
# Create app directory
WORKDIR /usr/src/app

# Copy application dependency manifests to the container image.
# A wildcard is used to ensure copying both package.json AND package-lock.json (when available).
# Copying this first prevents re-running npm install on every code change.
COPY package*.json ./

# Install app dependencies using the `npm ci` command instead of `npm install`
RUN npm ci

# Bundle app source
COPY  . .
RUN npm  run build

# Use the node user from the image (instead of the root user)
USER node
Ralle Mc Black
  • 1,065
  • 1
  • 8
  • 16
  • Unfortunately it didn't work for me. I made this project based on the following tutorial. The tutorial has a repository. The live reload of the tutorial repository works for me. The problem doesn't seem to be related to docker files, package.json or dependency versions. https://www.tomray.dev/nestjs-docker-compose-postgres. – user3788133 Dec 16 '22 at 03:48
  • Please give some more information. Your host system, Linux, Windows? Os version. And if possible post the error logs you get into. I tried it on Ubuntu 22.04. It is just a normal docker-compose and dockerfile. Did you destroy containers and volumes before? docker compose up --build? – Ralle Mc Black Dec 16 '22 at 06:08
  • Host system is Windows 10 Pro Version 21H2. There's no visible errors. It make the build, the api works. Just don't update on changes (when a file is saved). – user3788133 Dec 16 '22 at 12:09
  • I'm using 'docker-compose up --build -V -d' or 'docker-compose up --build -V' (works on the tutorial repository). I also tried (several times) to remove containers, images and volumes manually through Docker Desktop. – user3788133 Dec 16 '22 at 12:12
  • If you update your question with all error messages you get, i will try to help you. Please also once the container starts, post the logs from nestjs. I took your repo yesterday and noticed that there are some other errors in the code and fixed them during the test. – Ralle Mc Black Dec 16 '22 at 12:17
  • I updated it with all logs I have. – user3788133 Dec 16 '22 at 13:36
  • I suspecting there's a file permission issue, but in the project files itself, not in Dockerfile comands: https://stackoverflow.com/a/74829258/3788133 – user3788133 Dec 16 '22 at 20:03
0

OK! This is very strange!

I probably solved this by deleting all files inside my project and recreating new files.

Some files, I copied from the tutorial repository below. Other files, I manually recreated. But in all I put the old content, without changes.

Maybe there was some file permission issue that didn't allow hot reloading. I am not sure! But the hot reload is working, for now.

https://www.tomray.dev/nestjs-docker-compose-postgres

user3788133
  • 163
  • 2
  • 10
0

Does someone have followed this https://www.tomray.dev/nestjs-docker-compose-postgres tutorial recently we new versions of nest? because I think that hot reload is not working and it is related to some package versions

0

Add this lines of codes below the compilerOptions in tsconfig.json

  "watchOptions": {
    // Use a dynamic polling instead of system’s native events for file changes.
    "watchFile": "dynamicPriorityPolling",
    "watchDirectory": "dynamicPriorityPolling",
    "excludeDirectories": ["**/node_modules", "dist"]
  }

Then replace the start:dev command with this code (in package.json):

"start:dev": "nest start --tsc --watch",
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 17 '23 at 11:42