What is import-local?
The import-local npm package is designed to allow a globally installed package to defer to a locally installed version of itself when it is being run within a project that has that package as a dependency. This is useful for testing local changes to a package without having to publish the package or modify the global version.
What are import-local's main functionalities?
Deferring to local version
This code checks if a local version of the package is available. If it is, it will use the local version and log a message to the console. Otherwise, it will proceed with the global package's code.
if (importLocal(__filename)) {
console.log('Using local version of this package');
} else {
// Code for the global package goes here
}
Other packages similar to import-local
resolve-from
The resolve-from package allows you to resolve a module path from a given root directory. It's similar to import-local in that it helps with module resolution, but it doesn't automatically defer to a local version of a package.
require-local
Require-local is another package that allows for requiring modules from a local context. It's similar to import-local but does not provide the automatic deferring mechanism to local versions when a global version is run.
linklocal
Linklocal can link local dependencies for development purposes. It's similar to import-local in that it helps with using local versions of packages, but it works by creating symlinks rather than deferring when a global version is executed.
import-local
Let a globally installed package use a locally installed version of itself if available
Useful for CLI tools that want to defer to the user's locally installed version when available, but still work if it's not installed locally. For example, AVA and XO uses this method.
Install
npm install import-local
Usage
import importLocal from 'import-local';
if (importLocal(import.meta.url)) {
console.log('Using local version of this package');
} else {
}
You can also pass in __filename
when used in a CommonJS context.