Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installing "global" npm dependencies via package.json [duplicate]

I have a few "global" dependencies (jshint, csslint, buster, etc..) that I'd like to have automatically installed and executable via the command line when my package is installed via npm install. Is this possible?

Currently, I'm doing the following manually:

  1. npm install -g <package_name>
  2. from within my project: npm link <package_name>

Update: Just came across this feature request for npm. It seems like the scripts config within package.json is the way to go?

Update Again: Or, after reading the npm docs, maybe I'm supposed to use a .gyp file? I'm confused.

like image 286
GxXc Avatar asked Feb 02 '13 00:02

GxXc


People also ask

How install all dependencies in package json react?

Install the dependencies to the local node_modules folder. In global mode (ie, with -g or --global appended to the command), it installs the current package context (ie, the current working directory) as a global package. By default, npm install will install all modules listed as dependencies in package. json .

How do I install all dependencies at once?

Installing all dependencies: yarn or yarn install. Installing one and only one version of a package: yarn install --flat. Forcing a re-download of all packages: yarn install --force. Installing only production dependencies: yarn install --production.

How install npm install globally?

Install Package Globally NPM installs global packages into /<User>/local/lib/node_modules folder. Apply -g in the install command to install package globally.

Where is global npm package json?

the package is installed in the current file tree, under the node_modules subfolder. As this happens, npm also adds the lodash entry in the dependencies property of the package. json file present in the current folder.


2 Answers

It's not possible to specify dependencies as "global" from a package.json. And, this is by design as Isaac states in that feature request you referenced:

Yeah, we're never going to do this.

But, "binaries" can still be used when a package is installed locally. They'll be in .../node_modules/.bin/. And, you should be able to queue them up with a preinstall script.

Though, if the series of commands is rather lengthy (as "jshint, csslint, buster, etc.." would suggest), you may want to look into using a build tool such as grunt to perform the various tasks:

{     // ...,      "scripts": {         "preinstall": "grunt"     } } 
like image 108
Jonathan Lonowski Avatar answered Sep 19 '22 16:09

Jonathan Lonowski


I really like the pattern where you install local dependencies, then use a bash script that sets your PATH to ./node_modules/.bin.

File: env.sh

# Add your local node_modules bin to the path for this command export PATH="./node_modules/.bin:$PATH"  # execute the rest of the command exec "$@" 

Then, you can use this script before any bash command. If you pair that with a Makefile or npm script:

File: Makefile

lint :     ./env.sh csslint my_styles 

File: package.json

"scripts": {   "lint": "./env.sh csslint my_styles" } 

This tasks in these files look like they reference csslint in some global location, but they actually use the version in your node_modules bin.

The really awesome benefit of this is that these dependencies can be versioned easily, just like your other node modules. If you stick with a global install solution, you could be clobbering some specific version on the user's system that is required for one of their other projects.

like image 20
EndangeredMassa Avatar answered Sep 19 '22 16:09

EndangeredMassa