Search code examples
javascriptnode.jslernamonorepo

Can you undo a lerna bootstrap?


I've run a lerna bootstrap --hoist. I now want to undo this and unlink all of the created symlinks. Is there a command to do this?


Solution

  • Lerna link (or bootstrap) will create symlinks either directly underneath a node_modules directory or, for packages with an @-scoped name, in a subdirectory with that name. Assuming your packages are all in ./packages/*, Any symlinks directly underneath ./node_modules or in ./packages/*/node_modules. For instance, after bootstrap, lerna has created a couple symlinks to my creatively-named @myscope/foo package:

    .
    ├── lerna.json
    ├── node_modules
    │   └── @myscope
    │       └── foo -> ../../packages/foo
    ├── package.json
    ├── package-lock.json
    └── packages
        ├── bar
        │   ├── node_modules
        │   │   └── @myscope
        │   │       └── foo -> ../../../foo
        │   └── package.json
        └── foo
            └── package.json
    

    I can dig those out with find:

    $ find . -type l -and \( -path './node_modules/*' -or -path './packages/*/node_modules/*' \)
    ./packages/bar/node_modules/@myscope/foo
    ./node_modules/@myscope/foo
    

    This isn't perfectly selective because it could find symlinks that you created with npm link.

    I can use ls -l to dump the source and the symlink target (vs. readlink which would just print the target):

    $ find . -type l -and \( -path './node_modules/*' -or -path './packages/*/node_modules/*' \) -exec ls -l {} \;
    lrwxrwxrwx 1 eric eric 12 janv.  4 09:46 ./packages/bar/node_modules/@myscope/foo -> ../../../foo
    lrwxrwxrwx 1 eric eric 18 janv.  4 09:45 ./node_modules/@myscope/foo -> ../../packages/foo
    

    If you're feeling confident about selectivity, you can -exec rm {} \; to remove them. It's not as good as a built-in undo, but at least it helps you see what lerna did.