nx icon indicating copy to clipboard operation
nx copied to clipboard

Nx workspaces and projects references not working with allowImportingTsExtensions flag set to true

Open igortas opened this issue 8 months ago • 1 comments

Current Behavior

Hi,

I have following the link below, and I can't make it work. I have multiple services in /apps folder and one packages/shared, the problem is I'm using allowImportingTsExtensions option so I can have .ts imports. How to use this flag together with typescript references, when I need to import shared logic from packages/shared in /apps folder.

emitDeclarationOnly or noEmit enabled not working

https://www.typescriptlang.org/tsconfig/#allowImportingTsExtensions

https://nx.dev/technologies/typescript/recipes/switch-to-workspaces-project-references

I have some simple structure in packages/shared where I have multiple .ts files, just re-exported through index.ts file.

index.ts

export * from './lib/config.ts'; 

When I run bun dev, I get the error that can't find ./lib/config.ts

I've created the packages/shared structured with this files:

package.json

{
  "name": "@myorg/shared",
  "version": "0.0.1",
  "private": true,
  "main": "./src/index.ts",
  "types": "./src/index.ts",
  "exports": {
    ".": {
      "types": "./src/index.ts",
      "import": "./src/index.ts",
      "default": "./src/index.ts"
    },
    "./package.json": "./package.json"
  },
}

tsconfig.lib.json

{
  "extends": "../../tsconfig.base.json",
  "compilerOptions": {
    "baseUrl": ".",
    "rootDir": "src",
    "outDir": "./out-tsc/lib",
    "types": ["node"]
  },
  "include": ["src/**/*.ts"],
  "references": []
}

tsconfig .json

{
  "extends": "../../tsconfig.base.json",
  "files": [],
  "include": [],
  "references": [
    {
      "path": "./tsconfig.lib.json"
    }
  ]
}

nx root level tsconfig.base.json

{
  "compilerOptions": {
    "composite": true,
    "declarationMap": true,
    "emitDeclarationOnly": true,
    "importHelpers": true,
    "isolatedModules": true,
    "lib": ["es2022"],
    "module": "nodenext",
    "moduleResolution": "nodenext",
    "noEmitOnError": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitOverride": true,
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "skipLibCheck": true,
    "strict": true,
    "target": "es2022",
    "customConditions": ["development"],
    "allowImportingTsExtensions": true,
    "declaration": true
  }
}

Expected Behavior

I expected nx to continue working without problem, even i have imports than ends with .ts extension

GitHub Repo

No response

Steps to Reproduce

  1. Create simple monorepo with nx and add one node.js service in /apps
  2. Create the folder structure in packages/shared (can be with nx g lib option too), files should be as described above
  3. Try to run nx serve 'nameoftheservice'

Nx Report

Node           : 24.0.2
OS             : linux-x64
Native Target  : x86_64-linux
bun            : 1.2.17

nx (global)    : 21.2.0
nx             : 21.2.1
@nx/js         : 21.2.1
@nx/jest       : 21.2.1
@nx/eslint     : 21.2.1
@nx/workspace  : 21.2.1
@nx/devkit     : 21.2.1
@nx/esbuild    : 21.2.1
@nx/node       : 21.2.1
typescript     : 5.8.3
---------------------------------------
Registered Plugins:
@nx/js/typescript
@nx/jest/plugin
@nx/js/typescript
---------------------------------------
Cache Usage: 3.00 MB / 100.69 GB

Failure Logs


Package Manager Version

No response

Operating System

  • [ ] macOS
  • [ ] Linux
  • [ ] Windows
  • [x] Other (Please specify)

Additional Information

I'm using WSL2 arch linux

Error (dist folder can't find ./libs/config.ts which is re-exported through index.ts file in packages/shared when importing something from myorg/shared):

Debugger listening on ws://localhost:9229/1f267c6c-e1c0-4208-9d1f-29c7cf6ea83b
Debugger listening on ws://localhost:9229/1f267c6c-e1c0-4208-9d1f-29c7cf6ea83b
For help, see: https://nodejs.org/en/docs/inspector

Error: Cannot find module './lib/config.ts'
Require stack:
- /home/myapp/apps/gateway/dist/packages/shared/src/index.js
- /home/myapp/apps/gateway/dist/apps/gateway/src/main.js

igortas avatar Jun 29 '25 17:06 igortas

any updates on this?

Forceres avatar Dec 06 '25 20:12 Forceres