4

I created a subfolder in /src called /libs. When I run npm run build, the subfolder isn't included in /dist.

I'm assuming that I have to modify a build script? If so, which file would that be?

Edit #1

This all stems from trying to require a custom module located in src/libs from my controller. I've tried various patterns: ../libs/module_name, ./libs/module_name. The only way to have it work was to hard code the path from the root (i.e. /home/me/app/src/libs/module_name).

If I do: console.log(__dirname) in the controller that is attempting to require the module from /lib, I see a reference to /dist. I went looking into /dist and /libs wasn't there.

TechFanDan
  • 3,329
  • 6
  • 46
  • 89

3 Answers3

6

lb-tsc is a thin wrapper for TypeScript compiler (tsc), you can find the source in loopback-next:packages/build/bin/compile-package.js

Among other options, it provides a new flag --copy-resources to copy non-TypeScript files from src to dist. I think it may work equally well (if not better) as your cp -r solution.

"scripts": {
    ...
    "build": "lb-tsc es2017 --outDir dist --copy-resources"
    ...
}

Personally, I would use a different solution:

  • use src only for TypeScript files to be compiled
  • put JavaScript sources and other files into a different directory, e.g. lib (instead of src/lib).
Miroslav Bajtoš
  • 10,667
  • 1
  • 41
  • 99
1

With the help of a colleague, I figured it out. In package.json, I appended a copy command to bring the libs folder into dist in the build section.

"scripts": {
    ...
    "build": "lb-tsc es2017 --outDir dist && cp -r src/libs dist",
    ...
}
TechFanDan
  • 3,329
  • 6
  • 46
  • 89
0

For those using Nest CLI, please note that it also does not automatically move your "assets" (non-TS files) to the dist folder during the build process.

For these non-TS files you just need to add them to the "compilerOptions": {} property in the nest-cli.json file.

 "compilerOptions": {
    "assets": ["**/myfolder/**"]
    ...
    }
  • The syntax "**/father-folder/**" causes all folders & files in myfolder to be included in the distribution folder (dist)

  • My resource for this was from Derryl Thomas

  • Link to NestJS documentation on assets : Link...

Yariv
  • 1,212
  • 10
  • 21