tsconfig-paths icon indicating copy to clipboard operation
tsconfig-paths copied to clipboard

Paths not being transformed

Open aryzing opened this issue 2 years ago • 2 comments

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/[email protected]_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/[email protected]/node_modules/tsconfig-paths
│   ├── ts-node -> .pnpm/[email protected]_awa2wsr5thmg3i7jqycphctjfq/node_modules/ts-node
│   ├── ts-unused-exports -> .pnpm/[email protected][email protected]/node_modules/ts-unused-exports
│   ├── @types
│   │   └── node -> ../.pnpm/@[email protected]/node_modules/@types/node
│   └── typescript -> .pnpm/[email protected]/node_modules/typescript
├── package.json
├── pnpm-lock.yaml
├── README.md
├── src
│   ├── foo
│   │   └── bar.ts
│   └── index.ts
└── tsconfig.json

aryzing avatar Jan 29 '23 19:01 aryzing

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

jnxey avatar Feb 10 '23 04:02 jnxey

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

jnxey avatar Feb 10 '23 07:02 jnxey

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

TristanSimonDev avatar Jul 30 '24 19:07 TristanSimonDev

Switched to Bun

aryzing avatar Jul 31 '24 07:07 aryzing