Configuring type definitions when depending on bokehjs npm package

I’m currently trying to build a Javascript library which builds on bokehjs. I’ve added the bokehjs npm package as a dependency in my package.json file and am importing trying to import from the bokehJS modules, e.g.:

import {Document} from "bokehjs/build/js/bokeh/document"

The problem I’m facing is that when compiling the code it cannot find the typing files, so I get errors like:

Could not find a declaration file for module 'bokehjs/build/js/lib/document'. ' 

./node_modules/bokehjs/build/js/lib/document/index.js' implicitly has an 'any' type.
Try `npm install @types/bokehjs` if it exists or add a new declaration (.d.ts) file containing `declare module 'bokehjs/build/js

I realize the types are shipped in bokehjs/build/js/types but I can’t figure out how to make nodejs find them. Am I importing from the wrong place or is there a way to make nodejs discover the types? Alternatively is the bokehjs npm package simply not set up to export all of bokehjs?

Since 1.1 bokehjs’ npm package configures types, but that only works for import {something} from "bokehjs". Importing individual modules requires additional configuration to make it work. I would follow the approach we use in our tests. Add this to your tsconfig.json:

https://github.com/bokeh/bokeh/blob/master/bokehjs/test/tsconfig.json#L25-L27

(just replace .. with ./node_modules/bokehjs or something that works for you) and then you will be able to import individual modules like this:

import {Document} from "@bokehjs/document"

This may also be future proof, if we decide to split bokehjs up under @bokehjs namespace. On the other hand, maybe you don’t have import individual modules and just import from “bokehjs” (Document is exported by default). This is what our examples use, but they target bundled bokehjs artifacts. Importing individual modules is useful though if you want to include only necessary modules.

Alternatively we could keep *.js and *.d.ts together to reduce the need for configuration, but importing from build/js/lib seems pretty ugly.

1 Like

I moved this to the “Development” Category since it’s arguably touching on technical decisions we might make around BokehJS as its own library. (Also to just get things rolling there :slight_smile:)

Thanks, that worked great!