1

I installed a public github repo using the syntax provided in the repository's readme:

npm install https://github.com/user_name/proj_name.git

However, I'm not sure how to load this module into my js code. I've tried:

const my_lib = 'proj_name';

Unfortunately, this is not working for me. How can I load a module that was installed directly from a github repository?

8
  • What name is provided in the package.json file in that repo? Commented Jan 16, 2020 at 7:06
  • @SebastianKaczmarek, it is the same as proj_name in the github url Commented Jan 16, 2020 at 7:07
  • Check your dependencies in package.json file and import module with const my_lib = require('package_name') Commented Jan 16, 2020 at 7:12
  • @AhmetZeybek, I'm not sure what you mean? Commented Jan 16, 2020 at 7:14
  • For example, if you added expressjs with yarn add expressjs/express, this would be seen in package.json as express, so you can import this module with const express = require('express') Commented Jan 16, 2020 at 7:16

1 Answer 1

2

Currently you're just defining a constant as a string with value 'proj_name'. To load a module from node_modules, you have to do the following:

npm install <package_name> --save, where --save write the package and version in your dependencies of package.json. You can also use --savedev to write the package in your devDependencies (both optional).

Use const packageName = require('packageName'); in your e.g. app.js to use the package in your code.

See here for more details about npm install in general, specifying-dependencies and here for ecma script require and import.

Sign up to request clarification or add additional context in comments.

1 Comment

This was helpful. I didn't realize I needed to add the --save flag. It's worth noting that I don't have a full node app/project setup. I just had a single script that I was trying to run that was requiring that package.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.