Mac environment uses soft link npm link

When developing local npm packages, you often need to change things, but you don’t know whether the changes are correct. At this time, most people may package a version for project testing, which is neither elegant nor cumbersome. In fact, the npm official has provided a soft link debugging method such as npm link.

1. Soft chain

If you are familiar with linux, it should be clearer and easier to understand. In fact, it is a jump mapping. For example, when accessing a, a stores the path of b, and when accessing a, it is actually the path of b.


2. How to create a soft link

2.1 First execute npm link under the path of the npm package to create a global link.

image (1).png

2.2 Use in the project package
image.png

At this time, you can use the local npm package in the project, and you don't need to modify it and package it once.

⚠️Note:

  1. npm link packageName, where packageName is the name in package.json in the local package.

  2. If the command "packageName" not found appears after executing the second step after executing the first step, you may need to restart the command line.


3. How to remove the soft link

3.1 First remove npm unlink packageName in the project

3.2 Unlink npm unlink in the npm package


4. Other

View all created global link names:

npm ls --global --depth 0

Forcibly un-create a specific global link:

sudo npm rm --global packageName


5. Step on the pit

5.1 The npm package has been linked to the global, but this path is still not found in the project

Pasted image 20230116141918.png

Pasted image 20230116141929.png

Troubleshooting process: The path to find the package should be the node_modules inside the project first. If you can’t find it, you will find the global one. If you have installed a node manager such as n, fnm, nvm, you need to check whether the global path version of the link is consistent.
You can use: npm root -g to view the current global node_modoles path

Pasted image 20230116142136.png

It was found to be consistent, and then checked the main and module configurations in the package.json of the npm package. I remembered that the import path in the project was wrongly matched. It should be stdeditmap/lib/stdeditmap.esm.
Pasted image 20230116142550.png

PS: At this point, you should run both projects to see the changes. Since I am using the packaged product, I still need to use build (the project does not support ts, so I refer to the packaged product)

Guess you like

Origin blog.csdn.net/jexxx/article/details/128703253