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

"Cannot find package" when running mocha and importing between packages in monorepo #2103

Open
alicegherbison opened this issue Jan 16, 2024 · 1 comment

Comments

@alicegherbison
Copy link

Search Terms

I think this is similar or related to #2031 and #2023 but not sure.

Expected Behavior

When running mocha and pulling in modules from between packages they should resolve and be found when a test runs.

Actual Behavior

When a test script runs a function that itself imports a variable (or anything) from a another package in the monorepo, the variable is not resolved.

Error: Cannot find package 'packages' imported from C:\Users\AliceHerbison\import-error\packages\extra1\functions\exampleFunction.ts
    at packageResolve (C:\Users\AliceHerbison\import-error\node_modules\ts-node\dist-raw\node-internal-modules-esm-resolve.js:757:9)
    at moduleResolve (C:\Users\AliceHerbison\import-error\node_modules\ts-node\dist-raw\node-internal-modules-esm-resolve.js:798:18)
    at Object.defaultResolve (C:\Users\AliceHerbison\import-error\node_modules\ts-node\dist-raw\node-internal-modules-esm-resolve.js:912:11)
    at C:\Users\AliceHerbison\import-error\node_modules\ts-node\src\esm.ts:218:35
    at entrypointFallback (C:\Users\AliceHerbison\import-error\node_modules\ts-node\src\esm.ts:168:34)
    at C:\Users\AliceHerbison\import-error\node_modules\ts-node\src\esm.ts:217:14
    at addShortCircuitFlag (C:\Users\AliceHerbison\import-error\node_modules\ts-node\src\esm.ts:409:21)
    at resolve (C:\Users\AliceHerbison\import-error\node_modules\ts-node\src\esm.ts:197:12)
    at nextResolve (node:internal/modules/esm/loader:163:28)
    at ESMLoader.resolve (node:internal/modules/esm/loader:838:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:77:40)
    at link (node:internal/modules/esm/module_job:76:36)

Steps to reproduce the problem

yarn
yarn test

Minimal reproduction

This is a minimal reproduction using the same setup and folder structure for our corporate monorepo. The example functions and tests are basic but illustrate exactly the same concept - https://github.com/alicegherbison/import-error

Specifications

  • ts-node version: ^10.9.2
  • node version: 18.16.1
  • TypeScript version: 5.3.3
  • tsconfig.json, if you're using one:
{
  "compileOnSave": false,
  "compilerOptions": {
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "alwaysStrict": true,
    "baseUrl": ".",
    "checkJs": true,
    "declaration": false,
    "esModuleInterop": true,
    "importsNotUsedAsValues": "error",
    "incremental": true,
    "isolatedModules": false,
    "jsx": "react-jsx",
    "lib": ["dom", "dom.iterable", "ES2019"],
    "module": "ES2020",
    "moduleResolution": "node",
    "noEmit": true,
    "noImplicitAny": true,
    "noImplicitReturns": false,
    "noImplicitThis": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "outDir": "dist",
    "paths": {
      "public": ["public/"]
    },
    "plugins": [
      {
        "name": "typescript-plugin-css-modules",
        "options": {
          "customMatcher": "\\.scss$"
        }
      }
    ],
    "pretty": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "sourceMap": true,
    "strictNullChecks": false,
    "target": "ES2019",
    "rootDirs": ["packages"]
  },
  "include": ["packages/**/*", "./**/*.ts", "**/*.tsx", "**/*.mdx"],
  "exclude": ["node_modules/", ".yarn/"],
  "ts-node": {
    "esm": true,
    "experimentalSpecifierResolution": "node",
    "require": ["tsconfig-paths/register", "ts-node/register"]
  }
}

  • package.json:
{
  "name": "import-error",
  "packageManager": "[email protected]",
  "workspaces": [
    "packages/*"
  ],
  "dependencies": {
    "@mdx-js/loader": "^3.0.0",
    "eslint-mdx": "^3.0.0",
    "eslint-plugin-mdx": "^3.0.0",
    "jsdom-global": "^3.0.2",
    "react": "17.0.2",
    "react-dom": "17.0.2",
    "react-redux": "7.2.9",
    "react-router": "6.20.1",
    "react-router-dom": "6.20.1",
    "react-router-hash-link": "^2.4.3",
    "redux": "4.2.1",
    "rehype-external-links": "2.0.1",
    "rehype-react": "7.1.1",
    "source-map-support": "^0.5.21",
    "ts-node": "^10.9.2",
    "tsconfig-paths": "^4.2.0"
  },
  "devDependencies": {
    "@testing-library/react": "14.1.2",
    "@types/chai": "4.3.11",
    "@types/chai-dom": "1.11.3",
    "@types/mocha": "10.0.6",
    "@types/node": "20.10.4",
    "@types/react": "17.0.71",
    "@types/react-dom": "17.0.25",
    "@types/react-router-dom": "5.3.3",
    "@typescript-eslint/eslint-plugin": "6.14.0",
    "@typescript-eslint/parser": "6.14.0",
    "chai": "5.0.0",
    "chai-dom": "1.12.0",
    "cross-env": "7.0.3",
    "css-loader": "6.8.1",
    "css-minimizer-webpack-plugin": "5.0.1",
    "cssnano": "6.0.1",
    "eslint": "8.55.0",
    "eslint-config-prettier": "9.1.0",
    "eslint-plugin-css-modules": "2.12.0",
    "eslint-plugin-react": "7.33.2",
    "eslint-plugin-react-hooks": "4.6.0",
    "eslint-plugin-storybook": "^0.6.15",
    "fork-ts-checker-webpack-plugin": "9.0.2",
    "global-jsdom": "9.2.0",
    "jsdom": "23.0.1",
    "lint-staged": "15.2.0",
    "mocha": "10.2.0",
    "node-abi": "3.52.0",
    "node-gyp": "10.0.1",
    "node-loader": "2.0.0",
    "npm-run-all": "4.1.5",
    "postcss": "8.4.32",
    "postcss-loader": "7.3.3",
    "postcss-scss": "4.0.9",
    "prettier": "3.1.1",
    "sass": "1.69.5",
    "sass-loader": "13.3.2",
    "semver": "^7.5.4",
    "style-loader": "3.3.3",
    "typescript": "5.3.3",
    "typescript-plugin-css-modules": "5.0.2",
    "webpack": "5.74.0",
    "webpack-bundle-analyzer": "4.10.1",
    "webpack-cli": "5.1.4",
    "webpack-dev-server": "4.15.1",
    "webpack-merge": "5.10.0",
    "workbox-webpack-plugin": "7.0.0"
  },
  "engines": {
    "node": ">=16"
  },
  "scripts": {
    "test": "cross-env TS_NODE_FILES=true TS_NODE_BASEURL=. TS_NODE_PROJECT=tsconfig.json mocha --enable-source-maps"
  },
  "type": "module"
}

  • Operating system and version: Windows 10

This is my first issue raised here - can provide more details if needed. Any help is much appreciated, as have tried what feels like every solution regarding tsconfig-paths/register, path mapping etc to no avail.

@luosansui
Copy link

luosansui commented Jan 31, 2024

I also encountered a similar error. When I use

"imports": {
    "#fs": {
      "node": "fs",
      "default": "./src/fs.ts"
    }
 }

in the package

A

E:\Dev\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 'fs' imported from E:\Dev\package.json
    at packageResolve (E:\Dev\node_modules\ts-node\dist-raw\node-internal-modules-esm-resolve.js:757:9)
    at resolvePackageTargetString (E:\Dev\node_modules\ts-node\dist-raw\node-internal-modules-esm-resolve.js:424:16)
    at resolvePackageTarget (E:\Dev\node_modules\ts-node\dist-raw\node-internal-modules-esm-resolve.js:464:12)
    at resolvePackageTarget (E:\Dev\node_modules\ts-node\dist-raw\node-internal-modules-esm-resolve.js:510:26)
    at packageImportsResolve (E:\Dev\node_modules\ts-node\dist-raw\node-internal-modules-esm-resolve.js:620:26)
    at moduleResolve (E:\Dev\node_modules\ts-node\dist-raw\node-internal-modules-esm-resolve.js:793:21)
    at Object.defaultResolve (E:\Dev\node_modules\ts-node\dist-raw\node-internal-modules-esm-resolve.js:912:11)
    at E:\Dev\node_modules\ts-node\src\esm.ts:218:35
    at entrypointFallback (E:\Dev\node_modules\ts-node\src\esm.ts:168:34)
    at E:\Dev\node_modules\ts-node\src\esm.ts:217:14
[nodemon] app crashed - waiting for file changes before starting...

error will appear.

When I change the value of node under imports #fs to ./src/fs.ts , it works normally. I think maybe ts-node cannot parse the node built-in module in Subpath imports.

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

2 participants