TS2307: Cannot find module or its corresponding type declarations
Asked Answered
M

1

17

I have a private GitHub repo my-parser-generator which uses pegjs. All underlying logic is written in TypeScript, which is then compiled and fed to pegjs, which generates the final parser.

Then this repo a is used a package dependency in another private repo parser-consumer, which is written in TypeScript (compiled with --declaration option). So my-parser-generator basically offers JavaScript files to a TypeScript consumer.

But when I try to import the newly generated parser I get the following error: TS2307: Cannot find module 'my-parser-generator' or its corresponding type declarations

I've tried most of the solutions on the internet, but nothing has worked for me

my-parser-generator

package.json (most of the lines are omitted)

{
  "private": true,
  "version": "0.4.9",
  "name": "my-parser-generator",
  "files": [
    "dist/lib/**/*.js",
    "dist/excel.browser.js",
    "dist/**/*.d.ts",
    "dist/**/*.js.map"
  ],
 "main": "dist/excel.browser.js",
 "typings": "dist/index.d.ts",
 "type": "module",
 "dependencies": {
   "pegjs": "^0.11.0-master.b7b87ea"
 }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es2019",
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "noFallthroughCasesInSwitch": true,
    "outDir": "dist/lib/",
    "sourceMap": true
  },
  "exclude": ["node_modules", "dist", "tests", "excel.browser.js"],
  "include": ["src/**/*"]
}

dist/excel.browser.js (generated by pegjs so all code is omitted in favor of what is exported)

export {
  peg$SyntaxError as SyntaxError,
  peg$parse as parse
};

export default {
  SyntaxError: peg$SyntaxError,
  parse: peg$parse
};

dist/index.d.ts - my attempt to add some typings for the generated file above

declare namespace MyParserGenerator {
    type PegLocation = {
        offset: number,
        line: number,
        column: number
    }

    type Options = {
        startRule: string,
        filename: string
    }

    function SyntaxError (message: string, expected: string, found: string, location: PegLocation): void;
    function parse <T extends Options>(input: string, options: T): any;
}

declare module "my-parser-generator" {
    export default MyParserGenerator
}

parser-consumer

package.json

{
  ...,
  dependencies: {
    "my-parser-generator": "git+ssh://[email protected]/my-repo/my-parser-generator.git#master",
    ...,
  },
  ...,
}

node_modules/my-parser-generator - package structure

dist/
  lib/
    grammar.js
    grammar.d.ts
    grammar.js.map
  excel.browser.js
  index.d.ts
package.json
README.md

index.ts - the code that uses the parser and throws the error

import parser from 'my-parser-generator';

Interestingly enough, WebStorm can easily locate and auto-complete the package

error screenshot

Mesocratic answered 10/6, 2021 at 9:36 Comment(0)
A
7

About the declaration for your parser you could use the pegjs/typings/generated-parser.d.ts from your node modules as a starting point.

For the message Cannot find my-parser-generator or its corresponding declarations. I suspect it could be because (1) you are saving the index.d.ts in the dist folder instead of the package root, (2) the js file is dist/excel.browser.js, this is easily associated with dist/excel.browser.d.ts.

I would add your my-parser-generator/package.json, accordingly to the typescript publication guidelines

{
  "types": "dist/index.d.ts"
}
Alpenglow answered 16/6, 2021 at 14:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.