1

I would like to use a parent NPM package from inside a subfolder, as part of the development process. Let's call my library "logary".

Structure:

.
|- package.json
|- tsconfig.json
|- src
 |- ... all the source code
|- dist
 |- ... compiled TS as .js, .js.map and .d.ts
|- examples
 |- with-nextjs
  |- package.json
  |- tsconfig.json

The library is written in TypeScript and so is the example. The parent compiles with outDir: ./dist in tsconfig.json. I have multiple entry points (one for the main lib, one for each peer dependency as for example: import withLogary from 'logary/plugins/nextjs'.

I would like to be able to write import Logary from 'logary'; inside ./examples/with-nextjs and have Logary, the constructor, have types attached to it be fully "resolvable" by the TypeScript compiler. I want changes in . to be reflected when ./examples/with-nextjs is reloaded.

It's not a monorepo (in the common sense). I've tried yarn link in . and then yarn link logary inside ./examples/with-nextjs, which doesn't seem to solve it (cannot import module). Furthermore, as I have bumped the version in . to 6.0.0, as I yarn install inside ./examples/with-nextjs, yarn complains that version is not published to npmjs.com and asks me to select a previous version, which it then downloads.

I know there's Lerna ("Splitting up large codebases into separate independently versioned packages is extremely useful for code sharing." — this is a tiny code-base and only has a single package, I just want to reference it in a single direction from an example) and Yarn Workspaces ("It allows you to setup multiple packages in such a way that you only need to run yarn install once to install all of them in a single pass." — which is not my aim)

I'm not interested in adding any special config (like paths) to tsconfig.json in ./examples/with-nextjs because then it's not just to copy the example to as your own app.

➡ How do I best structure a TypeScript library project with multiple peer dependencies and with an examples folder?

Maybe related?

RobC
  • 16,905
  • 14
  • 51
  • 62
Henrik
  • 9,303
  • 4
  • 49
  • 83

0 Answers0