yarn build --production causes error while yarn build not

Viewed 640

I am trying to figure out why, when I try to yarn install --production and then yarn build my ts react app it causes Could not find a declaration file for module 'react-router-dom'. '..../node_modules/react-router-dom/index.js' implicitly has an 'any' type. while it's already included in deps:

deps enter image description here

while yarn install and then yarn build succeeds.

1 Answers

include the types package in your root index.d.ts file. You can do this with any @types package that isn't paired with its sans @type/ dependency equivalent

you can use this approach for things like analytics and sdks

it compiles the defs into output making it available in production

you must also set "declaration": true` in tsconfig.json

{
"compilerOptions": {
    "sourceMap": "true",
    "declaration": true,
    "...": "..."
  },
  "include": ["index.d.ts", "src/**/*", "..."]
}

here's my root index.d.ts from a current project

/// <reference types="facebook-js-sdk" />
/// <reference types="facebook-pixel" />
/// <reference types="google.maps" />
/// <reference types="google-libphonenumber" />
/// <reference types="gtag.js" />
/// <reference types="glob" />

declare module "facebook-js-sdk";
declare module "facebook-pixel";
declare module "google.maps";
declare module "google-libphonenumber";
declare module "gtag.js";
declare module "glob";

interface Window {
  dataLayer?: object[];
}

In an additional tsconfig file (tsconfig.build.json),extend your main tscnfig.json then call for paths to exclude

  • tsconfig.build.json
{
    "extends": "./tsconfig.json",
    "exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
}

Note:

once you declare the package, you no longer need to import types provided by that package. They are injected and available globally for consumption

Related