Metro Bundler Symlink Support

2.0.0 · active · verified Tue Apr 21

metro-symlinked-deps is a utility package designed to configure the Metro bundler, primarily used in React Native development, to overcome its inherent lack of support for symlinks. This deficiency often hinders local development workflows involving `yarn link` or `npm link` for shared dependencies by preventing Metro from resolving modules correctly across symlinked directories. Version 2.0.0 is the current stable release, which maintains compatibility with various `metro-config` versions by designating it as a peer dependency, thereby avoiding duplicate installations. The package provides a streamlined, automatic configuration function, `applyConfigForLinkedDependencies`, which intelligently manages Metro's `resolver.blacklistRE` and `watchFolders` settings. It addresses a long-standing issue in Metro (and its reliance on `jest-haste-map`) by allowing developers to integrate symlinked packages into their build process seamlessly. While not on a strict release cadence, it is updated as needed to address dependency changes and maintain its workaround effectiveness as long as Metro's native symlink issue persists.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to integrate `metro-symlinked-deps` into your `metro.config.js` to enable symlink support. It automatically applies the necessary configurations, explicitly sets the project root for robustness, and includes common options like blacklisting modules and adding watch folders.

const { applyConfigForLinkedDependencies } = require('metro-symlinked-deps');
const { getDefaultConfig } = require('metro-config');

module.exports = (async () => {
  const defaultConfig = await getDefaultConfig();
  return applyConfigForLinkedDependencies(
    {
      ...defaultConfig,
      // Add any custom Metro configuration here that you need to merge
      transformer: {
        babelTransformerPath: require.resolve('react-native-babel-transformer'),
      },
      resolver: {
        ...defaultConfig.resolver,
        extraNodeModules: {
          // Example for a specific linked module if needed
          'some-linked-module': __dirname + '/../some-linked-module',
        },
      },
    },
    {
      projectRoot: __dirname, // Explicitly provide the project root
      blacklistLinkedModules: ['react-native'], // Blacklist common collision source
      additionalWatchFolders: [
        // Add paths to any top-level linked packages here if not automatically detected
        __dirname + '/../path-to-your-linked-package-root'
      ]
    },
  );
})();

view raw JSON →