primers

When I encountered the situation of debugging NPM local package again, I couldn’t remember, so I looked at the document and actually operated it, and found some things that were not written in the document.

introduce

According to the documentation, linking packages is a two-step process.

The first step

Executing NPM link in a package folder creates a symlink in the global {prefix}/lib/node_modules/ file that points to where the NPM link command is executed.

The second step

Go to another directory and run the NPM link packageName command to create a symbolic link from the globally installed packageName to node_modules in the current file.

Note that packageName is taken from the package.json field of the package, not the folder name.

Package names may have scope prefixes, and if so, packageName should be added accordingly.

practice

Based on the document, combined with the actual operation, look at the result.

Runtime environment

  • The project is based on webPack simple configuration, locally running server.
  • Node is managed using NVM.
  • The package has been referenced by the project and needs to be modified and debugged on top of it.
  • The system is macOS.

operation

Run the NPM link command in the root directory of the package.

Symlink: symlink: symlink: symlink: symlink: symlink: symlink

I tried to change the local source file and found that the contents of the corresponding file in the global package also changed.

NPM Link packageName: NPM link packageName:

Node_modules’ dependency package has changed:

The package is the same as the package generated by the server. The package has been updated. Note that the folder icon is marked with an arrow, which was not present before the link.

After restarting the service, I went to the source library to modify the source code, and found that the modified content was synchronized under the project node_modules. Webpack also detected the change and automatically refreshed.

What should I do if I want to restore the original package after modification? Strangely, no explanation can be found in the official NPM documentation.

Remove the link

Go to the project and execute the following command:

npm unlink --no-save package && npm install
Copy the code

As you can see from the NPM uninstall documentation, unlink is an alias for Uninstall, which essentially removes the package.

If the package is not needed, you are advised to remove it. Run the following command in the package directory:

npm unlink
Copy the code

The resources

Recently, I listened to Yang Zhao’s audio lecture on Records of the Grand Historian, which gave me a different understanding of qin Shihuang’s Confucianism.

In Records of the Grand Historian, The First Emperor of qin wanted to live forever, so he kept many ways. One day, after discussing two ways, he felt that he could not stay with the first Emperor any longer and ran away. When Emperor Qin Shi Huang heard this, he was very angry. He investigated the methods of fraud. 460 people were implicated and all of them were killed and made known to the world. Pit Confucianism is referring to this thing.

Above just said roughly once, detailed can listen to.