How to write Node AND Browser code?

If we have a.js that must run in browser and node js, what is the best strategy, to write 2 files one that loads tfjs-node and other that loads tfjs ?

It really depends what you are trying to do. Obviously the TFJS imports for Node will be different to a script tag import on the client side. So it may be easier to have the JS that runs on both in 1 file and then the imports that are specific to node / browser in different files. So then they just include the appropriate imports depending on the deployment point they want to run on and the rest of the logical JS code is common between them once the library is loaded to minimize code duplication and maintenance for the common code.

1 Like

Thanks for reaching back @Jason. I have this structure currently:

src/
├── baseTypes.ts
├── findObjects.browser.ts
├── findObjects.ts
├── index.ts
├── steps
│   ├── postprocess.browser.ts
│   ├── postprocess.ts
│   ├── preprocess.browser.ts
│   ├── preprocess.ts
│   └── __tests__
...

For the most part they only differ in the tfjs import for tfjs-node or browser tfjs .

I do not seem to find a simple solution to have just one kind.