0

I have code in a database that has different node modules in it. Like this:

exports.hello = hello

Normally, I would just use hello = require './hello.js'. However, becuase the code is stored in the db, I can't include a path to the file. When I try and do

eval unescape hello_from_db_code doesn't work.

Is there a way to get the require functionality without having a path to the file?

3
  • What do you mean by 'the code is stored in the db'? Can you please edit the question to make it more understandable? Commented Jun 9, 2013 at 12:06
  • by code is stored in the db, means that I have exports.hello = hello stored as a field in a row in a database instead of in a file Commented Jun 9, 2013 at 14:43
  • Just curious, what do you get when you print hello_from_db_code out to the console? Commented Jun 9, 2013 at 16:41

2 Answers 2

1

i don't think you can use require for this. see here...

however, you can read the code from the db, eval it, and then export it.

anyway, try avoiding hacks that will trash your modules cache.

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

Comments

1

If 'doesn't work' is 'module is not exported and there are undefined globals' then you can try to eval with missing references in the context:

vm = require 'vm'
requireFromString = (str, filename) ->
  sandbox =
    module:
      exports: {}
    require: require
    __filename: filename
  vm.runInNewContext(src, sandbox, filename)
  sandbox.module.exports

You may want to add extra references to sandbox, look at Module.prototype._compile function in module.js. Also you can access it directly as hello = module._compile(hello_from_db_code)

Comments

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.