Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute 'npm link' directly on install

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?

like image 970
eisbehr Avatar asked Feb 21 '17 13:02

eisbehr


People also ask

How do I link npm locally?

Example: Let the local-dir is the local directory and project-dir is the project directory and local_module is the local module package you want to install, first go to the local-dir and type npm link and next go to the project directory and type npm link <local_module> this will link your local module to your project.

What is npm link command?

The npm link command is special because it allows you to load a module from anywhere on your computer. Here is an example: 1. Create (or download) an npm module to your computer: cd ~/Desktop. git clone [email protected]:klughammer/node-randomstring.git.

How use npm linked package?

Package linking is a two-step process. First, npm link in a package folder with no arguments will create a symlink in the global folder {prefix}/lib/node_modules/<package> that links to the package where the npm link command was executed. It will also link any bins in the package to {prefix}/bin/{name} .

Does npm link install dependencies?

Installs a set of local node modules into a target folder using npm link . Links all dependencies of the local modules if they are listed in the source folder.


2 Answers

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
like image 112
jccguimaraes Avatar answered Oct 13 '22 08:10

jccguimaraes


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.

like image 36
Jake Holzinger Avatar answered Oct 13 '22 09:10

Jake Holzinger