I’m having issues with instances of the same package over different JavaScript projects and I’d like to know if there is any workaround other than modifying the original package. My project setup is the following:
Project A
In this project there are several classes that are used on other places. This project is uploaded as a package to the npm repository.
Project B
This project has Project A as dependency (via npm) and creates instance generators of the classes provided there. It’s kind of a repository of functions that create Project A instances.
Project C
This project has Project A as dependency (via npm) as well. The project attempts to generate the instances from Project B and check if there are valid Project A instances.
The problem comes when Project C attempts to check the instanceof
from Project B generated instances, because it alwais returns false
. Instances are being created importing from a project to the other with a require(filepath)
the generator function and then executing it.
Maybe it’s easier to understand with an example:
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 tried to upload de Project A as an npm package because I thought that way the definitions would be somehow shared between projects but clearly it didn’t work.