I'm facing issues with using the same package in different JavaScript projects and I'm looking for a solution other than modifying the original package. Here is how my project setup looks like:
Project A This project contains various classes that are utilized in multiple places. It is published as a package on the npm repository.
Project B Project B depends on Project A (via npm) and creates instances of the classes provided by it. It serves as a function repository that generates instances of Project A.
Project C Similar to Project B, Project C also depends on Project A (via npm). The goal of this project is to generate instances from Project B and verify if they are valid Project A instances.
The issue arises when Project C tries to check the instanceof
for instances generated by Project B, as it always returns false
. Instances are created by importing the generator function from one project to another using require(filepath)
and then executing it.
Perhaps an example would clarify things better:
Project A
class A {
// ...
}
Project B
import { A } from 'npm-package-name';
export const generator = () => new A();
Project C
import { A } from 'npm-package-name';
const { generator } = require('...');
const a = generator();
return a instanceof A; // false
I attempted to publish Project A as an npm package, believing that the definitions would be shared across projects somehow. Unfortunately, that approach did not work as expected.