0

I have this file indexjs where I am exporting data

exports.updateMobileNumber = updateMobileNumber;

and in my appjs I am importing it.

var index = require('./../helpers/index');

Now another file mainjs is on external URL like

https://github.com/../models/catalog/main.js

How to import this file in my appjs

I don't want to download the file as it has its own server. Is it possible to make http request from one nodejs to another nodejs running simultaneously so that I get the required value. If yes, please help how to do it!

I have 1st node running at 3000 and another running at 3003, what should be the next step?

asked Feb 4, 2016 at 9:19
10
  • 1
    I'm not sure there's any way of doing this, and for good reason - executing code from a remote server is a pretty bad idea! What's the use case here? Commented Feb 4, 2016 at 9:23
  • there are some validation like mobile number validation, otp verification, mail verification so I want to keep it in separate file so this won't be disturbed while running code. Commented Feb 4, 2016 at 9:25
  • Hmm, I don't think there's a way to do this. You could GET a remote JSONP file, but NodeJS files must be locally accessible. Commented Feb 4, 2016 at 9:28
  • you can of course "download" or request the file by an url but as already mentioned this is most of the time not a convenient way. Commented Feb 4, 2016 at 9:33
  • 1
    @Sumit you could expose the file using a route on one server, e.g. to have the url 127.0.0.1:3000/mainFile and then get it on your other node instance using a GET request to that route. This way you would be using HTTP. Does this make sense? Commented Feb 4, 2016 at 9:59

2 Answers 2

2

NPM is designed for you to install and manage remote modules. Modules must be locally accessible and installed when a NodeJS instance is started up.

If you require a module not known to your package.json file, an error will be thrown.

Edit: require is synchronous, so it can't load non-local files (which would be an asynchronous operation). You could load data asynchronously though, using request.

answered Feb 4, 2016 at 9:30
Sign up to request clarification or add additional context in comments.

Comments

2

Short answer for the bare question (as it's written in title):

Download the file and place it inside your project, so you can use it with require.

--

You should think about: (as already mentioned in a lot of comments):

  1. If your remote file is data only (most of the time json) it's a convenient and correct way requesting the file via http, save it and then use it with require again!

    more about http inside node:

  2. If the remote file is not about data only and you thought about executing remote code - it's the wrong way! think about it! your remote file is in your case a kind of a "vendor library / util / helper" which you should implement strictly in awareness. (keywords: npm, modularization, vendors, helpers)

answered Feb 4, 2016 at 9:24

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.