reactjsnpmnpm-linkpushd

Undo npm link when syncing node_modules, react & react-dom


I found this fix to an issue that I was having trying to use hooks in a package I was developing and getting an error trying to use multiple instances of React. This worked for me, but I'm wondering how to "undo" all of this when I'm ready to test my package from NPM. When I was just using 'npm link' I would do 'npm unlink'. How would I apply this here and get my app back to it's original state, especially with the first section for in app, with the pushd & popd.

I was using npm link between an app and a module I was actively working on,
and I had to fix it by linking the app's react and react-dom to the module,
then linking the module to the app:

in app:
pushd node_modules/react && npm link; popd
pushd node_modules/react-dom && npm link; popd

in module:
npm link  # this creates a global link for module.
npm link react && npm link react-dom # link app's react and react-dom, so they're in sync.

in app:
npm link [module-name]  # this replace the module lib in node_modules with a link to the module's local copy.

Solution

  • Run an npm install in your app, that'll reinstall the module you linked.

    You don't really need to worry about the links you created on react and react-dom. That won't have any effect on your app.

    If it really bothers you, you can go to the following directory and manually remove them: <your_home_dir>/nvm/nodejs/<you_node_version>/lib/node_modules/react -> <your_app's_react_dir> <your_home_dir>/nvm/nodejs/<you_node_version>/lib/node_modules/react-dom -> <your_app's_react-dom_dir>

    If you don't use them, they're just some links sitting there doing nothing.