dividab / tsconfig-paths

Load node modules according to tsconfig paths, in run-time or via API.
MIT License
1.8k stars 100 forks source link

Paths not being transformed #237

Closed aryzing closed 1 month ago

aryzing commented 1 year ago

After setting up a barebones project, with a minimal tsconfig, the the aliased paths are not properly converted when running the app with ts-node. This is the exact config being used,

{
  "include": ["src"],

  "compilerOptions": {
    // Type Checking
    "strict": true,

    // Modules
    "baseUrl": ".",
    "module": "ESNext",
    "moduleResolution": "node",
    "paths": {
      "@foo/*": ["src/foo/*"]
    },

    // Emit
    "noEmit": true,

    // Interop Constraints
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "isolatedModules": true,

    // Language and Environment
    "jsx": "react-jsx",
    "lib": ["DOM", "DOM.Iterable", "ESNext"],
    "target": "ESNext",

    // Completeness
    "skipLibCheck": true
  },
  "ts-node": {
    "esm": true,
    "experimentalSpecifierResolution": "node",
    "require": ["tsconfig-paths/register"]
  }
}

The error is,

$ pnpm ts-node src/index.ts 
/path/node_modules/.pnpm/ts-node@10.9.1_awa2wsr5thmg3i7jqycphctjfq/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:757
  throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath(base));
        ^
CustomError: Cannot find package '@foo/bar' imported from /path/src/index.ts

The file structure is as follows,

├── node_modules
│   ├── tsconfig-paths -> .pnpm/tsconfig-paths@4.1.2/node_modules/tsconfig-paths
│   ├── ts-node -> .pnpm/ts-node@10.9.1_awa2wsr5thmg3i7jqycphctjfq/node_modules/ts-node
│   ├── ts-unused-exports -> .pnpm/ts-unused-exports@9.0.2_typescript@4.9.4/node_modules/ts-unused-exports
│   ├── @types
│   │   └── node -> ../.pnpm/@types+node@18.11.18/node_modules/@types/node
│   └── typescript -> .pnpm/typescript@4.9.4/node_modules/typescript
├── package.json
├── pnpm-lock.yaml
├── README.md
├── src
│   ├── foo
│   │   └── bar.ts
│   └── index.ts
└── tsconfig.json
jnxey commented 1 year ago

@aryzing Is the problem solved? I also encountered the same problem

jnxey commented 1 year ago

I found the answer here: https://github.com/TypeStrong/ts-node/discussions/1450#discussioncomment-1806115

TristanSimonDev commented 1 month ago

in your package . json add:

"scripts": {"test": "ts-node -r tsconfig-paths/register src/index.ts" },

src/index.ts is just an example, so replace it with your main file

then start with npm run test

aryzing commented 1 month ago

Switched to Bun