'How to interactively test the executable of an NPM Node.js package during development without constantly reinstalling the package?
Consider a simple sample module like this one.
The package exposes a dummy function something like:
index.js
const uniq = require('uniq');
function myfunc() {
return uniq([1, 2, 2, 3]).join(' ');
}
exports.myfunc = myfunc;
Then, an executable uses the function:
browserify-hello-world
#!/usr/bin/env node
const browserify_hello_world = require('browserify-hello-world');
console.log(browserify_hello_world.myfunc());
The package publishes both the index.js and the executable with:
package.json
"bin": {
"browserify-hello-world": "browserify-hello-world"
},
"dependencies": {
"uniq": "^1.0.1"
},
My question is, how to test the browserify-hello-world executable interactively during development?
If I just clone the repository and do:
npm install
./browserify-hello-world
then it fails with:
Error: Cannot find module 'browserify-hello-world'
because as documented at https://docs.npmjs.com/cli/install install does not install the current package itself under node_modules.
I got it close to working perfectly with:
npm link
npm link browserify-hello-world
./browserify-hello-world
which runs correctly because:
npm link:- creates a symlink from the global package install:
~/.nvm/versions/node/v10.15.1/lib/node_modules/broserify-hello-worldto the source directory - runs
npm installon the source directory, which createsnode_moduleswith all dependencies
- creates a symlink from the global package install:
npm link browserify-hello-worldcreates a symlink fromnode_modules/browserify-hello-worldto the global directory~/.nvm/versions/node/v10.15.1/lib/node_modules/broserify-hello-world
which works well because everything is done with symlinks, so that updates to the local package are immediately visible to the executable.
The only downside of this method, besides the inconvenience of typing all those commands after install, is that afterwards during development, if I need to install a new dependency into the project, e.g. with:
npm install vaca
then that breaks by symlinks for some reason: node_modules/broserify-hello-world is gone, and I am forced once again to do:
npm link
npm link browserify-hello-world
Note that doind just npm link browserify-hello-world above does not work and fails with:
Error: Cannot find module 'uniq'
because the dependency of our package, uniq, was also removed by npm install vaca.
Is there a way to avoid redoing the link process after every new install?
The npm-safe-install module might exist to address this problem: https://github.com/UD-UD/npm-safe-install but when I tried it with:
npm install -g npm-safe-install
npm-safe-install vaca
although it did keep my node_modules/borwserify-hello-world symlink, it still removed the dependency uniq. I asked about that at: https://github.com/UD-UD/npm-safe-install/issues/4
Related threads:
- https://github.com/npm/cli/issues/533
- https://stackoverflow.com/questions/59357144/npm-package-development-run-executable
- https://github.com/npm/cli/issues/2380
- https://github.com/npm/npm/issues/1573
- https://github.com/npm/npm/pull/8501
Tested with: npm 6.12.0, Node.js 10.15.1 installed with NVM and npm-safe-install 1.0.0.
Solution 1:[1]
Your issue has been resolved and changes are published with some added features. Changes are available in [email protected]. Hope it resolves your issue.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | UJJAL DUTTA |
