2

I am just getting myself familiar with command line tools like npm. I've been searching around for the answer but was not able to find a clear one.

What I am trying to do is to install materialize-css package into my test package, as well as its devDependencies, like "autoprefixer". This is materializeCSS's package.json file.

Here's what I do: Under my newly created and blank folder "testProject", I use npm init to create a package.json file for my test package:

{
  "name": "create_project",
  "version": "1.0.0",
  "description": "Setting up a project",
  "main": "index.html",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "shenk wen",
  "license": "MIT"
}

Then, I do

npm install materialize-css

I was expecting the above command would install all the dependencies and devDependencies of materialize-css, but only the dependencies is being installed. I read this question and the accepted answer suggests adding --dev to the command. But that seems not the answer I am looking for because --dev would only make materialize-css a devDependency of my test package, but not installing its own devDependencies. The other answers are not so straightforward. Is there any parameter I can use to achieve this? Or do I have to change the env_variable which I don't know how to?

Community
  • 1
  • 1
shenkwen
  • 3,536
  • 5
  • 45
  • 85

2 Answers2

2

In older npm versions, 'npm install --dev' installed all devDependencies of all your dependencies. This also used to work in a recursive fashion, so you end up downloading also devDependencies of devDependencies of your dependencies and so on. This resulted in enormously long install time / download size.

Also, the purpose of the feature is questionable: Why should you care about devDeps of your deps? For these reasons --dev was removed from npm:

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

Current behavior for 'npm install' is: install all deps and devDeps for the 'main' package (the one you 'npm install'-ed in the first place), but when recursing, install only deps (and no devDeps).

If you want to install & save the dependency to your package.json, you should use --save or --save-dev, I don't think --dev does this.

Tomas Kulich
  • 14,388
  • 4
  • 30
  • 35
  • `npm i materialize-css --save-dev` will not install materialize-css package's devDependencies. Insteadly it will only install materialize-css as a devDependency to my test project. – shenkwen Dec 29 '15 at 15:14
  • yeah, exactly. There is no point in installing devDependencies of materialize-css; you're not developing materialize-css, you're just using it as it comes in your project. For just using the project installing not-dev-dependencies is enough - which is exactly what npm install does. – Tomas Kulich Dec 29 '15 at 21:09
  • I am aware that but I was asking whether there is a straightforward way to install devDependencies. What if I wanna fork materialize-css? – shenkwen Dec 29 '15 at 21:20
  • If you run 'npm install' on your materialize-css fork, you WILL get all the deps + devDeps (try it!). This is because npm distinguish, whether materialize-css is installed as the top-level (main) package or whether it is just some dependency of something another (such as, your test project). If it is the main package, you'll get the deps + devDeps. If it is just the dependency, you'll get just deps and no devDeps. – Tomas Kulich Dec 30 '15 at 09:08
0

If you want the devDependencies of a module you've installed as a dependency to your project, you almost certainly want to git clone that module's repo or fork it instead. When you run npm install in your cloned repo, that will also install all of the module's devDependencies.

(I'm not a developer by trade and my npm-fu was a bit rusty, so I confused myself about what I was trying to do. Tomas Kulich's question "Why should you care about devDeps of your deps?" helped me realize the error of my ways.)

jdunning
  • 1,356
  • 1
  • 14
  • 26