3

I am using npm+node for some simple scripting. Minimal example: I have a node project directory (in which I ran npm init), containing script.js, package(-lock)?.json, and node_modules directory with dependencies. And this script I run from cmdline node "C:/path-to-project/script.js".

I want to share this script between 2 computers. I decided to use npm and publish my scripts to registry.

Problems:

MAIN) Installing the package on the other machine, puts it into node_modules with its dependencies and so cannot be used from the master project directory like on the original machine. Downloading the package with npm-pack results in desired directory structure, but there is no more the capability to npm-update the master project (= script.js).

SECONDARY) I am using local dependency, which is added as a link to the node_modules. I would like to keep it as local link on the original computer, but install it from registry on the second. The dependency is also published, but when installing on second pc, npm fails with Could not install from "node_modules\utils" as it does not contain a package.json file. The package is scoped, so there is no naming collision and something similar is reported as a bug here https://github.com/npm/npm/issues/18266.

So what is the correct way o achieving my goal? Mainly the first part about installing it as master project and not a dependency while keeping the ability to update to newer version.

Lukáš Řádek
  • 1,273
  • 1
  • 11
  • 23

2 Answers2

0

A possible solution for this is to create a node powered cli tool.

Here is a tutorial on how to build cli tool in node. You will ultimately install the tool globally and it will be available as a binary from your cli.

https://www.twilio.com/blog/how-to-build-a-cli-with-node-js

jeeves
  • 1,871
  • 9
  • 25
0

Publish as an executable CLI

Publish the package with the bin option enabled in package.json. You'll need the bin option to point to a script with a shebang for node, that has exec privileges, and runs the project.

This will allow you to run the package as an executable.

Install package locally and run w/ npm script

Then you can install the package into another project as a local dependency with npm i and create an npm script to run the package as a script. NPM scripts look in your $PATH but also in the local project's node_modules/.bin when you give it the name of a command to run. So from the npm script, it will find the name of your package (or the command name you used when setting up it's bin entry)

From the NPM scripts docs:

In addition to the shell’s pre-existing PATH, npm run adds node_modules/.bin to the PATH provided to scripts. Any binaries provided by locally-installed dependencies can be used without the node_modules/.bin prefix.

This means you don't have to install the package globally, and can keep it as a local dependency of a project where you can update it with npm update. (If it's only used during development, install it as a dev-dependency.)

As for your second issue, I'm not sure how to reproduce it and I'd suggest opening a second question to ask it specifically.

Jon Church
  • 2,320
  • 13
  • 23