2

I have a Node project, and I can npm pack it into a tarball and install it in another directory for testing. When I do that, the set of files in the "bin" clause of my package.json are correctly symlinked into the node_modules/.bin directory there. Thus I can use npx something to run the "something" script and it works as I would expect.

From the actual project directory, however, npm install doesn't do that. The scripts are there in my top-level "bin" directory of course, but npx doesn't find them. Obviously I could just run them directly, but it would be nice if I could run the same shell commands to run them in other installation directories and in the project home directory.

I don't want to install the package globally; I want to keep everything local, at least for now. So can I get npm to make those symlinks for me, or should I just bite the bullet and do it myself?

Here's my package.json:

{
  "name": "@foo/plazadb",
  "version": "0.1.0",
  "description": "Basic database setup with upload/download from CSV",
  "main": "lib/plazadb.js",
  "author": "Pointy",
  "license": "ISC",
  "dependencies": {
    "arg": "^5.0.1",
    "cls-hooked": "^4.2.2",
    "csv-parser": "^3.0.0",
    "csv-stringify": "^6.0.5",
    "neat-csv": "^7.0.0",
    "pg": "^8.7.*",
    "sequelize": "^6.16.*"
  },
  "bin": {
    "conntest": "./bin/conntest.js",
    "download": "./bin/download.js",
    "upload": "./bin/upload.js"
  }
}

The "bin" files all exist (of course, otherwise they would never work). What I'm trying (out of ignorance) is a simple npm install from the project directory.

Pointy
  • 405,095
  • 59
  • 585
  • 614

1 Answers1

1

One way to do is by

  1. specifying a local prefix by using npm config set prefix <path> or one of these methods linked here.
  2. generating symlinks using npm link

As a word of caution, to undo npm link executing npm unlink . may not work. Instead one may have to run npm uninstall -g ..

jadelord
  • 1,511
  • 14
  • 19
  • Thanks. `npm install --prefix .` does not cause the symlinks in `node-modules/.bin` to be created. The `npm link` functionality doesn't look like what I want either. Really, I just want the `.bin` symlinks in the package working directory so that `npx` works the way it does in another local directory where my package has been installed. – Pointy May 29 '22 at 12:33
  • I used something like `export NPM_CONFIG_PREFIX=$PWD/build` followed by `npm install` and `npm link`.From what I see `node_modules/.bin` is where external CLI tools get installed and not the package you are developing. – jadelord Jun 05 '22 at 19:22
  • yes, agreed. Well I'll probably write a script to do it. Thank you again. – Pointy Jun 08 '22 at 19:49