NestJS copy assets files
Asked Answered
I

5

12

I'm working on an EmailModule that reads a pug template.

I couldn't get the build to include the *.pug templates file:

I have followed the instruction based on this: https://github.com/nestjs/nest-cli/issues/320

Adding the assets property in nest-cli.json

"email": {
  "type": "library",
  "root": "libs/email",
  "entryFile": "index",
  "sourceRoot": "libs/email/src",
  "compilerOptions": {
    "tsConfigPath": "libs/email/tsconfig.lib.json"
  },
  "assets": ["**/*.pug"]
},
Impresario answered 25/2, 2020 at 0:11 Comment(1)
BTW if you are using tsc to build your nestjs app you can use {"command": "cp -R ./apps/backend/src/assets ./dist/apps/backend/src/assets"} in commands when you are building your app with nx:run-commandsAgony
C
12

My use case involved setting up static file serving via the @nest/serve-static package. The tutorial recommeds putting those assets in the client folder at the root of the project, and configuring the module like so:

ServeStaticModule.forRoot({
   rootPath: join(__dirname, '..', 'client'),
}),

Unfortunately, this did not work for me since the client folder wasn't getting copied to the dist folder. It took some tries but eventually I got the assets configuration to do just that, here it is:

"assets": [
  { "include": "../client/**", "outDir": "dist/client", "watchAssets": true }
]

You have to include it in the nest-cli.json, inside the compilerOptions top-level property, e.g.

{
  "$schema": "https://json.schemastore.org/nest-cli",
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "deleteOutDir": true,
    "assets": [
      { "include": "../client/**", "outDir": "dist/client", "watchAssets": true }
    ]
  }
}
Charette answered 20/6, 2023 at 15:14 Comment(0)
H
7

The documentation is a bit unclear about this, and how nestjs currently ( version 10.2.2) implements the copying of assets is not very logical.

What to keep in mind is that assets will ONLY be copied from your source (normally src) folder.

Let's say that you have a folder named assets in your src folder, and you want to include it in your dist/src folder. You would then assume that a nest-cli.json like the one below would copy that same folder to dist/src/assets:

{
  "$schema": "https://json.schemastore.org/nest-cli",
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "assets": ["assets/**/*"]
    ]
  }
}

But the above settings will instead copy the assets folder to the root of your dist folder. It will not be put in the dist/src folder, which will break any relative linking to these files from other files in your dist folder.

So, if you want to maintain the same structure in your dist folder, the proper way to do this is like this:

{
  "$schema": "https://json.schemastore.org/nest-cli",
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "assets": [
        {
            "include": "assets/**/*",
            "outDir": "dist/src/"
        }
    ]
  }
}

The above settings will copy all files and folders from src/assets to dist/src/assets.

Haffner answered 30/8, 2023 at 16:38 Comment(0)
P
3

The assets property should be inside the compilerOptions property

"email": {
  "type": "library",
  "root": "libs/email",
  "entryFile": "index",
  "sourceRoot": "libs/email/src",
  "compilerOptions": {
    "tsConfigPath": "libs/email/tsconfig.lib.json",
    "assets": ["**/*.pug"]
  }
},
Pinon answered 9/9, 2020 at 8:6 Comment(2)
I am also having this issue. I include a config file but, its not copied to the distribution file. I followed the documentation provided here docs.nestjs.com/cli/monorepo#assetsPogey
Did you ever solve this? Having same problemDavidoff
M
1

This is bug with old nest-cli versions. I just updated this package to "@nestjs/cli": "^8.2.5", and everything worked fine with copying folders/subfolders/files.

Maseru answered 24/4, 2022 at 5:28 Comment(0)
Z
0

For those who have a specific structure or want to isolate assets within a module.

I have assets in the folder src/modules/ai/_modules/generation/assets.

Here is my nest-cli.json:

{
  "$schema": "https://json.schemastore.org/nest-cli",
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "deleteOutDir": true,
    "assets": [
      {
        "include": "modules/ai/_modules/generation/assets/**/*.*",
        "outDir": "dist/src",
        "watchAssets": true
      }
    ],
    "plugins": [
      {
        "name": "@nestjs/swagger",
        "options": {
          "classValidatorShim": true,
          "introspectComments": false
        }
      }
    ]
  }
}

Zee answered 23/8, 2024 at 18:8 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.