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

ERR_MODULE_NOT_FOUND CustomError: Cannot find package when using paths #2031

Open
Jazcash opened this issue Jun 21, 2023 · 9 comments
Open

Comments

@Jazcash
Copy link

Jazcash commented Jun 21, 2023

C:\Users\me\Desktop\test\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 '@/stuff' imported from C:\Users\me\Desktop\test\src\index.ts
    at packageResolve (C:\Users\me\Desktop\test\node_modules\ts-node\dist-raw\node-internal-modules-esm-resolve.js:757:9)
    at moduleResolve (C:\Users\me\Desktop\test\node_modules\ts-node\dist-raw\node-internal-modules-esm-resolve.js:798:18)
    at Object.defaultResolve (C:\Users\me\Desktop\test\node_modules\ts-node\dist-raw\node-internal-modules-esm-resolve.js:912:11)
    at C:\Users\me\Desktop\test\node_modules\ts-node\src\esm.ts:218:35
    at entrypointFallback (C:\Users\me\Desktop\test\node_modules\ts-node\src\esm.ts:168:34)
    at C:\Users\me\Desktop\test\node_modules\ts-node\src\esm.ts:217:14
    at addShortCircuitFlag (C:\Users\me\Desktop\test\node_modules\ts-node\src\esm.ts:409:21)
    at resolve (C:\Users\me\Desktop\test\node_modules\ts-node\src\esm.ts:197:12)
    at resolve (C:\Users\me\Desktop\test\node_modules\ts-node\src\child\child-loader.ts:15:39)
    at nextResolve (node:internal/modules/esm/loader:163:28)

I'm importing another file with import { test } from "@/stuff". If I don't use the @ path alias and instead do import { test } from "./stuff" then it works fine. Previously the tsconfig-paths module has worked fine for me, but in this ESM project it seems to not work. Any ideas that don't require abandoning ESM and going back to CJS?

Minimal reproduction

https://codesandbox.io/p/sandbox/fast-dew-p8vcjk?file=%2Ftsconfig.json%3A11%2C27

Specifications

  • ts-node version: ^10.9.1
  • node version: 18.16.0
  • TypeScript version: 5.1.3
  • tsconfig.json:
{
  "compilerOptions": {
    "module": "es2022",
    "moduleResolution": "Node",
    "noEmit": true,
    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"]
    }
  },
  "include": ["src/**/*"],
  "ts-node": {
    "esm": true,
    "require": ["tsconfig-paths/register"]
  }
}
  • package.json:
{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "type": "module",
  "scripts": {
    "build": "ts-node src/index.ts",
    "start": ""
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "ts-node": "^10.9.1",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.1.3"
  }
}
@ptandler
Copy link

Seems, this is currently not supported on purpose, see #2023 (comment)

@aaggarwal-sumo
Copy link

I am facing similar issue can any one please help me out: #2023 (comment)

@mettini
Copy link

mettini commented Aug 15, 2023

This has done the trick for me: #1450 (comment)

@mvarchdev
Copy link

Does not work in node 21 :/

@MyNameIsNeXTSTEP
Copy link

MyNameIsNeXTSTEP commented Dec 16, 2024

I faced the same issue with npx tsx too.

Specifications:

  • "@types/node": "^22.10.1",
  • "typescript": "^5.6.3",
  • tsx@4.19.2
  • node ^22.10.0

tsconfig.json

{
  "extends": "../../tsconfig.json", // referrence below
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "~/*": ["./*"],
    }
  },
"include": ["**/*.ts", "./server/**/*", "./packages/**/*"],
}

package.json

{
  "name": "backend",
  "version": "1.0.0",
  "scripts": {
    "start:server": "npx tsx watch server/server.ts"
  },
  "license": "Proprietary and Confidential",
  "private": true,
  "type": "module",
  "dependencies": {
    "@fastify/autoload": "^6.0.2",
    "@fastify/swagger": "^9.4.0",
    "@fastify/swagger-ui": "^5.1.0",
    "ajv": "^8.17.1",
    "dotenv": "^16.4.5",
    "fastify": "^5.1.0",
    "fastify-cors": "^6.1.0",
    "fastify-plugin": "^5.0.1",
    "postgres": "^3.4.5",
    "redis": "^4.7.0",
    "shared": "workspace:^"
  },
  "devDependencies": {
    "@types/node": "^22.10.1",
    "dotenv": "^16.4.5",
    "typescript": "^5.6.3"
  }
}
refereenced root tsconfig.json (using yarn workspaces):
{
  "compilerOptions": {
    "lib": ["ES2022"],
    "baseUrl": ".",
    "module": "ESNext",
    "composite": true,
    "skipLibCheck": true,
    "allowJs": true,
    "noEmit": true,
    "incremental": true,
    "strict": true,
    "strictBindCallApply": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "moduleResolution": "Node",
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "isolatedModules": true,
    "paths": {
      "*": [".yarn/sdks/typescript/lib/*"],
      "@root/*": ["./*"],
      "@shared/*": ["./packages/shared/*"],
    },
  },
  "include": ["./apps/**/*", "./packages/**/*"],
  "exclude": [
    "node_modules"
  ],
  "files": [] // Prevent this config from compiling anything directly
}

So when using import as:

import psql from '~/packages/db/connection';

For the miported file:

import postgres from 'postgres';

const psql = postgres();
export default psql;

The error is inside the backend server container is:

node:internal/modules/run_main:122
    triggerUncaughtException(
    ^
Error [ERR_MODULE_NOT_FOUND]: Cannot find package '~' imported from /app/<my_app>/apps/backend/server/server.ts

@MyNameIsNeXTSTEP
Copy link

@Jazcash Have you figured out how to resolve this issue properly ?

@Jazcash
Copy link
Author

Jazcash commented Dec 16, 2024

@Jazcash Have you figured out how to resolve this issue properly ?

No, I swapped to using tsx and haven't looked back.

@MyNameIsNeXTSTEP
Copy link

@Jazcash Same thing, still cannot even imagine how to fix that.

Found that the most reliable way from any perspectives for using TypesScript with whatever you need - is to write .d.ts files separately from youre main code, which has to be written is JavaScript, and the connect them together by a package.json configuration of "types" field.

@Jazcash
Copy link
Author

Jazcash commented Dec 18, 2024

@MyNameIsNeXTSTEP I straight swapped ts-node for tsx in my sandbox above and it works just fine. There's also a build tool called tsup that I've used a fair bit which also just tends to work out-the-box.

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

No branches or pull requests

6 participants