[npm link] How to test libraries before deploying with npm link
1. context
you've developed a library and want to test it before deploying it to npmjs
- publishing to npmjs is irreversible.
2. prerequisites
we have two projects as shown below.
- mind-wired - the development project for the library you want to test, let's call it prjA
- sample-prj - a project for testing using mind-wired, let's call it prjB.
organizing your execution environment
- install node with nvm. it is recommended to use the same node version, as explained in the documentation below
- your library project is generating final deliverables in the
dist
directory. you want to test against the final deliverables (i.e. not against the actual code in prjA's src directory) - you use live server in vscode to run a web server for testing
3. npm link
npm's command that creates a symbolic link as if you deployed the library in your local environment
prjA's root directory looks like this
mind-wired root directory[mind-wired]
+- package.json
+- src
+- dist
+- ...
in the root directory with package.json, etc., run the npm link
command like this
npm link executemind-wired git:(master) ✗ npm link
audited 303 packages in 2.344s
34 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
/Users/___/.nvm/versions/node/v14.18.2/lib/node_modules/mind-wired
-> /Users/___/workspace/ws-js/mind-wired
- this creates a symbolic link named
mind-wired
under.nvm
as shown above
if you follow the path created, it looks like this
before running npm link
before generating the ```txt:link ➜ node_modules ls -al total 0 drwxr-xr-x 3 _____ staff 96 Nov 30 23:28 . drwxr-xr-x 4 _____ staff 128 Nov 30 23:28 ... drwxr-xr-x 24 _____ staff 768 Nov 30 23:28 npm
TXT
after running, the link is created as shown below
```txt:After link creation
➜ node_modules ls -al
total 0
drwxr-xr-x 4 ___ staff 128 May 23 10:50 .
drwxr-xr-x 4 ___ staff 128 Nov 30 23:28 ...
lrwxr-xr-x 1 ___ staff 42 May 23 10:50 mind-wired -> /Users/___/workspace/ws-js/mind-wired
drwxr-xr-x 24 ___ staff 768 Nov 30 23:28 npm
4. Testing
now we want to test prjB by including prjA.
from prjB's root directory, link to it as follows.
Command npm link prjAmind-wired-test npm link mind-wired
/Users/___/workspace/ws-js/mind-wired-test/node_modules/mind-wired
-> /Users/___/.nvm/versions/node/v14.18.2/lib/node_modules/mind-wired -> /Users/___/workspace/ws-js/mind-wired
➜ mind-wired-test
if we look at prjB's node_modules, it looks like this
TXT