0

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-world to the source directory
    • runs npm install on the source directory, which creates node_modules with all dependencies
  • npm link browserify-hello-world creates a symlink from node_modules/browserify-hello-world to 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:

Tested with: npm 6.12.0, Node.js 10.15.1 installed with NVM and npm-safe-install 1.0.0.

1 Answer 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!! Worth mentioning that resolution was done in response to: github.com/UD-UD/npm-safe-install/issues/4 at github.com/UD-UD/npm-safe-install/pull/5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.