13

I have an internal project where I want to link a command to a file with bin. Like expect this package.json:

{
  "name": "my-project",
  "bin": {
    "cli-name": "./bin/my-executable.js"
  },
  "dependencies": {
    "node-red": "^1.0.0"
  }
}

When executing npm install, all dependencies will be installed, and the bin configuration of node-red will be created too.

But my own bin will be completely ignored. It's not possible to use cli-name in cmd. It's necessary to execute npm link too, in a second step. Then cli-name will be available as command in console. I've even tried to use a postinstall script of npm with npm link in it, but then I got a loop ...

Is there a way to do this in one step on npm install?

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
eisbehr
  • 12,243
  • 7
  • 38
  • 63

7 Answers7

2

You can try changing you package.json to something like this:

{
  "name": "my-project",
  "script": {
    "cli-name": "./bin/my-executable.js",
    "postinstall": "npm run cli-name"
  },
  "dependencies": {
     "node-red": "^1.0.0"
  }
}

And just run:

npm install
jccguimaraes
  • 129
  • 1
  • 8
  • Thank you for your answer. But this is not what I was looking for. I need an own registered name for this, and not the execution of a script by npm. I want the same result as running `npm link`. – eisbehr Feb 21 '17 at 14:13
  • my bad, did not understand that part – jccguimaraes Feb 21 '17 at 14:32
  • 1
    @eisbehr is there a reason you cannot use `npm link` as the `postinstall` script? `postinstall` is executed after `install` automatically by npm. https://docs.npmjs.com/misc/scripts Edit: I see that you end up in an infinite loop, as do I. – Jake Holzinger Mar 14 '17 at 01:59
  • This answer actually helped for an unrelated question :) – Cully Oct 03 '19 at 20:12
2

There seems actually no build-in way to execute this tasks togeter automatically. So I've created an request for this feature directly to npm. It seems that a new command like npm il could be implemented.

I will updates this here when this or another way is available.

https://github.com/npm/npm/issues/15999

eisbehr
  • 12,243
  • 7
  • 38
  • 63
2

Surprised that all the other answers are basically wrong. To avoid a loop, simply:

"preinstall": "npm link"
George43g
  • 565
  • 10
  • 20
1

npm-link is for a different purpose really. The moment you do npm install, you should be able to use it in the command line already.

npm link is for linking your current development folder to the installed directory of your package. This is so that, as you are making changes in your development folder, it is been automatically avaliable on the cli and you do not have to re-install.

After installing, try going to usr/local/bin <- This is where it should be. if you cannot find my-executable.js here. Then something is wrong along that installation process.

Subomi
  • 380
  • 1
  • 2
  • 14
1

In order to run a global binary (bin) module you need to install it globally.

npm -g install

https://bretkikehara.wordpress.com/2013/05/02/nodejs-creating-your-first-global-module/

Another option (if you're on linux) is to use $(npm bin)/<module> if it is not installed globally.

Jake Holzinger
  • 5,783
  • 2
  • 19
  • 33
  • Thank your for your feedback. But I think it's not the best advice to install everything globally just for link binaries (I've even not tested if this works). And `npm link` will work without global installation too. So I was hoping for a better way ... – eisbehr Mar 14 '17 at 14:23
  • You can run the binaries without `-g`, that's just the easiest way to do it, `$(npm bin)` will resolve the binary without it being installed globally. – Jake Holzinger Mar 15 '17 at 17:23
0

You can create a script in your package.json to do both tasks in one command like so:

{
  "name": "my-project",
  "bin": {
    "cli-name": "./bin/my-executable.js"
  },
  "scripts": {
    "install-link": "npm install && npm link my-project"
  },
  "dependencies": {
    "node-red": "^1.0.0"
  }
}

Then just run npm run install-link, and it will run both commands.

grizzthedj
  • 7,131
  • 16
  • 42
  • 62
  • Hello and thanks for your reply. But this is exactly what I didn't want to have. – eisbehr Mar 10 '17 at 08:02
  • If this isn't what you want and `postinstall` won't work for you, I think your only option here, that is if you "insist" on modifying the behaviour of `npm install`, is to fork the [npm](https://github.com/npm/npm) repo and make your changes there, or create a pull request, and become a contributor. Seems like overkill though just to be able chain commands/functionality together. – grizzthedj Mar 10 '17 at 12:21
0

Try npm ln. An alias to npm link by npm documentation, worked for me on PowerShell 5.

npm-link documentation.