Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nestjs using Typescript Project Reference needs adjustments in outputPath and package.json exports #29889

Closed
1 of 4 tasks
danielritter opened this issue Feb 5, 2025 · 1 comment

Comments

@danielritter
Copy link

danielritter commented Feb 5, 2025

Current Behavior

outputPath and exports in package.json for nestjs libraries are set incorrectly:

  1. outputPath: "outputPath": "dist/packages/backend-nest-one" and "outputPath": "dist/packages/backend-nest-two"
  2. exports in package.json:
  "main": "./dist/index.js",
  "module": "./dist/index.js",
  "types": "./dist/index.d.ts",
  "exports": {
    "./package.json": "./package.json",
    ".": {
      "types": "./dist/index.d.ts",
      "import": "./dist/index.js",
      "default": "./dist/index.js"
    }
  },

Expected Behavior

Should be set as follows:

  1. outputPath: "outputPath": "packages/backend-nest-one/dist" and "outputPath": "packages/backend-nest-two/dist"
  2. exports in package.json:
    "main": "./dist/src/index.js",
    "module": "./dist/src/index.js",
    "types": "./dist/src/index.d.ts",
    "exports": {
        "./package.json": "./package.json",
        ".": {
            "types": "./dist/src/index.d.ts",
            "import": "./dist/src/index.js",
            "default": "./dist/src/index.js"
        }
    },

GitHub Repo

Repository
main branch -> Current Behavior
fix branch -> Expected Behavior

Steps to Reproduce

  1. Create new empty nx-workspace with npx create-nx-workspace@latest and preset None
  2. Create nestjs lib 1: nx g @nx/nest:lib packages/backend-nest-one --publishable --importPath=@backend/nest-one
  3. Create nestjs lib 2: nx g @nx/nest:lib packages/backend-nest-two --publishable --importPath=@backend/nest-two
  4. Because the libraries are created with "type": "module", (esm) in package.json the index.ts files need the .js extension for the exports: add that -> export * from './lib/backend-nest-one.module.js'; and export * from './lib/backend-nest-two.module.js';
  5. Add in NestBackendNestTwoModule imports: [NestBackendNestOneModule], with import import { NestBackendNestOneModule } from '@backend/nest-one';
  6. Add "@backend/nest-one": "0.0.1" in dependencies under package.json of @backend/nest-two
  7. nx sync
  8. Try to build lib two with nx run backend-nest-two:build
  9. Returns error

Nx Report

nx report

 NX   Report complete - copy this into the issue template

Node           : 23.5.0
OS             : win32-x64
Native Target  : x86_64-windows
npm            : 11.0.0

nx (global)    : 20.4.0
nx             : 20.4.0
@nx/js         : 20.4.0
@nx/jest       : 20.4.0
@nx/eslint     : 20.4.0
@nx/workspace  : 20.4.0
@nx/devkit     : 20.4.0
@nx/nest       : 20.4.0
@nx/node       : 20.4.0
typescript     : 5.7.3
---------------------------------------
Registered Plugins:
@nx/js/typescript

Failure Logs

> nx run @nest/backend-nest-two:build

Compiling TypeScript files for project "@nest/backend-nest-two"...
/Users/user/Desktop/example/nest/packages/backend-nest-two/src/lib/backend-nest-two.module.ts:1:42 - error TS2307: Cannot find module '@backend/nest-one' or its corresponding type declarations.

1 import { NestBackendNestOneModule } from '@backend/nest-one';
                                           ~~~~~~~~~~~~~~~~~~~

Package type is set to "module" but "cjs" format is included. Going to use "esm" format instead. You can change the package type to "commonjs" or remove type in the package.json file.

—————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— 

 NX   Ran target build for project @nest/backend-nest-two (1s)

   ×  1/1 failed
   √  0/1 succeeded [0 read from cache]

Package Manager Version

npm --version 11.0.0

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

No response

@danielritter
Copy link
Author

danielritter commented Feb 6, 2025

Seems that is has been changed here: 8bd0bcd and without dist/ in package.json, so both outputPaths should now work as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant