#Service not ignoring node_modules in higher directory (Monorepo)

83 messages · Page 1 of 1 (latest)

dapper field
#

hi, i have a monorepo w multiple directores/services...

I'm importing a class from my libs high service into my frontend service.

The problem is, im getting type errors about the node modules from that libs directory... i.e it is not ignoring the node_modules folder from higher directories from whihc i import from.

My frontend ts.config:

{
  "compilerOptions": {
    "target": "ES2020",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "ES2020",
    "moduleResolution": "node",
    "maxNodeModuleJsDepth": 10000,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "types": [],
    "jsx": "preserve",
    "incremental": true,
    "plugins": [
      {
        "name": "next"
      }
    ],
    "baseUrl": ".",
    "paths": {
      "@/*": ["/*"],
      "@components": ["/components/*"]
    }
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
  "exclude": ["node_modules", "../../libs/node_modules/*"]
}

My libs ts.config:

#
{
  "compilerOptions": {
    /* Visit https://aka.ms/tsconfig to read more about this file */

    /* Language and Environment */
    "target": "es2022" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,

    /* Modules */
    "module": "es2022" /* Specify what module code is generated. */,
    // "rootDir": "./",                                  /* Specify the root folder within your source files. */
    "moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */,
    "baseUrl": "." /* Specify the base directory to resolve non-relative module names. */,
    "paths": {
      "@src/*": ["/*"],
      "@types/*": ["/libs/types/*"],
      "@globaltypes": ["/libs/types/global"],
      "@etherstypes": ["/libs/types/ethers"],
      "@yc-models/*": ["/yc-models/*"],
      "@utility-models/*": ["/libs/utility-models"]
    } /* Specify a set of entries that re-map imports to additional lookup locations. */,

    "resolveJsonModule": true /* Enable i,// "outFile": "./",                                  /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */,
    "outDir": "./dist" /* Specify an output folder for all emitted files. */,

    /* Interop Constraints */
    "isolatedModules": false /* Ensure that each file can be safely transpiled without relying on other imports. */,
    "allowSyntheticDefaultImports": true,

    "esModuleInterop": true /* Emit 
    "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,

    "strict": true,

    "skipLibCheck": true /* Skip type checking all .d.ts files. */
  },
  "ts-node": {
    "esm": true,
    "experimentalSpecifierResolution": "node",
    "files": true
  },
  "exclude": ["node_modules", "dist", "typechain-types"]
}

native pasture
dapper field
#

i think that its supposed to ignore it which is why there is the exclude field

native pasture
#

node always looks in every parent directory when loading modules

dapper field
#
import { YCClassifications } from "../../../libs/yc-models/yc/classification";

this is how im importing it

native pasture
#

exclude only excludes things that were included by include or files

native pasture
#

it cannot exclude things that are explicitly imported

dapper field
#

include the libs folder and then excluse it's node modules?

native pasture
#

well, if you're getting type errors, you are importing it somehow

native pasture
#

make sure you don't have a phantom dependency

dapper field
native pasture
#

so you're explicitly importing the file that has type errors?

dapper field
#

no

#

i am importing a file form that higher repo

#

and then

#

im getting type errors

#

about one of it's node modules

native pasture
#

can you write out the path of the file that is importing, the file that is being imported, and the file that has type errors?

dapper field
#

or just from the monorepo for ease

#

like their shared parent dir

native pasture
dapper field
#

YCClassification.ts: (File being imported)
/Users/ofirsmolinsky/Desktop/yc-monorepo/libs/yc-models/yc/classification.ts

Layout.tsx (File importing):

/Users/ofirsmolinsky/Desktop/yc-monorepo/services/frontend-new/app/layout.tsx

Node module file with type error:
/Users/ofirsmolinsky/Desktop/yc-monorepo/libs/node_modules/ethers/lib.esm/contract/wrappers.js

#

@native pasture here u go, lmk if thats okay

dapper field
native pasture
#

yeah skipLibCheck

dapper field
#

this is the compile time error im getting (using Next13 if that matters)

error - ../../libs/node_modules/ethers/lib.esm/contract/wrappers.js
Module parse failed: Private field '#interface' must be declared in an enclosing class (29:13)
|     constructor(iface, provider, tx){
|         super(tx, provider);
>         this.#interface = iface;
|     }
|     get logs() {

Import trace for requested module:
../../libs/node_modules/ethers/lib.esm/contract/wrappers.js
../../libs/node_modules/ethers/lib.esm/contract/index.js
../../libs/node_modules/ethers/lib.esm/ethers.js
../../libs/node_modules/ethers/lib.esm/index.js
../../libs/yc-models/yc/YCToken.ts
../../libs/yc-models/yc/classification.ts
./app/layout.tsx
dapper field
#

on both of their corresponding tsconfigs

native pasture
#

that's not a type error

#

I'm pretty sure that's a node error

#

or a runtime error from node

dapper field
#

shouldnt be, because im not getting it in the libs folder itself that owns thos node modules

dapper field
native pasture
#
> class Foo { constructor() { this.#foo = 100 } }
... ;
class Foo { constructor() { this.#foo = 100 } }
                                ^

Uncaught SyntaxError: Private field '#foo' must be declared in an enclosing class
#

its a runtime error from javascript, not a type error

#

so you're importing that module

dapper field
#

da fawk

native pasture
#

and the module has a bug in it

dapper field
#

godamnit gimme a sec

native pasture
#

fixing that module is about your only option

#

you're using pnpm or yarn?

dapper field
#

yarn

#

thats so weird tho

#

its a pretty popular package

#

900k weekly downloads

#

last public was 4 days ago, which is 2 releases about the one i just had (upgraded rn and still the same)

native pasture
#

do yarn patch ethers and add in a #interface; to the class declaration

#

in the appropriate package folder, that is

#

that should fix the issue

native pasture
#

hm, that is really weird, then

#

how are you executing?

#

something is trying and failing to parse that file

dapper field
#

sec lemme look

#

seems like im only using .getAddress from that module.

#

which is a simple synchronous function ,unrelated to that class

native pasture
#

yeah, it's a parse time error. So the class doesn't even need to be instantiated to cause the error

dapper field
#

and still same thing

#
import { getAddress } from "ethers";

native pasture
#

if it imports that file at all, even if you're not using it: bam error

#

and there's no tree-shaking without a bundler

dapper field
#

hmm

#

well why is it even getting that error

#

assuming it does ahve that priv field

native pasture
#

yeah, that part is confusing

#

maybe you're executing and there's some source filter that's mangling the code

dapper field
#

and its not even pointing at the correct line lol

dapper field
native pasture
#

anyway I've got to go. If nobody else visits to help you, you can use !helper to summon someone. See #how-to-get-help for more details

dapper field
#

hopefully i can resolve this soon