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
?
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.
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.
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} .
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With