I've been using npm without giving much thought... Here's a quick post on a couple of options that I wanted to know more about.
npm install [package name] -g (or --global)
- Installs a package in global mode, and not under your local project/package. For Windows,
%APPDATA%\npm(e.g.,C:\Users\[Username]\AppData\Roaming\npm). Note that this is not where Node.js and npm is installed, that's usuallyC:\Program Files\nodejs. - It's mainly for packages with executables, as they will become available from everywhere, since above directory is added to the PATH when you install Node.js (when installed with default options).
- It does not modify
package.jsonof your current project.
An example – let's install Angular CLI:
npm install @angular/cli -g
After running the command, in the directory mentioned above:
ng(for bash) andng.cmd(for Windows) will be added, so you can runngfrom any command line window.node_modules\@angularwill be added.
npm install [package name] --save-dev (or -D)
The package will appear under devDependencies. This means that when you run npm install in production mode, the package will not be installed.
devDependencies are things you need during development and not necessarily in production, such as a unit testing framework.
You might come across npm --save. This has been deprecated, since it's the default option now – npm will add the package under the dependencies section in addition to installing it under node_modules.
How do you run npm install in production mode?
npm install --production- If
NODE_ENVis set toproduction, then simplynpm install - To force, regardless of
NODE_ENVvariable, thennpm --only=prod
You can't have a package in both devDependencies and dependencies.
Production mode is more important for server-side apps, i.e., using Node.js. For front-end, if you're just publishing the files after running through a package manager such as WebPack, probably should still be okay to use them as intended, though may need further investigation...
Some other tidbits
- If you run
npm install, npm will traverse up to find the root of your project, i.e., the directory withpackage.json, and install under node_modules from the directory accordingly. Ifpackage.jsonis not found, it will just use the current directory as root. - In npm lingo,
prefixis the root directory of your project, or the global npm directory if-gis used.
No comments:
Post a Comment