Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript npm Basics (retiring) Installing Packages with npm Managing Dependencies in the package.json File

Howard Goldstein
Howard Goldstein
9,399 Points

How to add module to package.json if it is already a global package

There's one aspect of managing dependencies that I'm not following...

Let's say I have a main root directory in which I install several node modules - into the /node_modules folder. Now I'm starting a new project several folders down - say /project_folder/project1.

If I want my new project to use one of the modules that I installed globally (e.g., express) - should I still use npm install express --save from the within the /project_folder/project1 directory? This seems really inefficient because it seems like it then adds a new node_modules folder within that directory which is duplicative with the node_modules folder in my root directory. Should I just manually add express to the package.json file for my new project? Or is there a better way to handle this?

2 Answers

Ari Misha
Ari Misha
19,323 Points

Hiya there! Node comes with a lots of built-in packages and you can defo use 'em in your project without installing it as --save-dev flag. For example, Node comes with Path package, which lets you define absolute path for your webpack configurations , right? Now below is a snippet of code like how you can squeeze in globally installed package:

// your-app/webpack.config.js

const path = require('path');  // "path" is a global installed package

module.exports = {
    entry: './src/app.js',
    output: {
        path: path.resolve(__dirname, 'bin'),
        filename: 'app.bundle.js'
    }
};

You just need to take advantage of require syntax. And Path package will be available at runtime in build process by webpack 'coz Path is installed globally.

Btw Node is just an example. You can use anything in your webpack.config.js file , from your own plugin to third party libraries. And everything follows the same syntax of require(). But if something is not installed globally but exists locally on your machine , all you have to do is import it in webpack.config.js file.

I hope this helped! Happy Learning!

~ Ari

Cory Harkins
Cory Harkins
16,500 Points

run npm install (package) --save-dev It will add to your package.json

You should have a task manager utilizing gulp/grunt/yarn etc. one of those that handles your builds.

I have a gulp-build on my laptop that handles all my needs inside that folder, and when I'm ready to publish, it will create a dist folder, place my build inside of it, and "clean" my working directory so I can start fresh.

https://www.youtube.com/watch?v=LmdT2zhFmn4&list=PLv1YUP7gO_viROuRcGsDCNM-FUVgMYb_G