I'm building a package and I wanted to build tests only if a certain function of the same local package is called. I don't want to get too much into the details but I am traversing the code with a Babel plugin to look for a specific module import to perform some actions.
The problem is that when I import this function I use locale paths, like this:
import { myFunction } from './';
I would like instead to have something like:
import { myFunction } from 'my-real-package-name';
I tried to install my package locally but now I am getting this error when trying to build:
error TS5055: Cannot write file '/my-real-package-name/lib/index.d.ts' because it would overwrite input file.
I could always check if the import is from ./ or my-real-package-name but I would prefer to just use my-real-package-name since it's safer to avoid unexpected issue.
Is there another way to achieve this?
I think what you're looking for is npm link or yarn link depending on your package manager.
From npm documentation page:
This is handy for installing your own stuff, so that you can work on it and test iteratively without having to continually rebuild.
Package linking is a two-step process.
First, npm link in a package folder with no arguments will create a symlink in the global folder {prefix}/lib/node_modules/ that links to the package where the npm link command was executed. It will also link any bins in the package to {prefix}/bin/{name}. Note that npm link uses the global prefix (see npm prefix -g for its value).
Next, in some other location, npm link package-name will create a symbolic link from globally-installed package-name to node_modules/ of the current folder.
For example:
cd ~/projects/node-redis # go into the package directory npm link # creates global link cd ~/projects/node-bloggy # go into some other package directory. npm link redis