defunctzombie / node-required

identifies which modules your script is using
67 stars 15 forks source link

Blowing up on .json files #20

Closed justinmchase closed 7 years ago

justinmchase commented 8 years ago

It's possible to do this in node:

var package = require('./package.json')

Which will result in an object representing your package.json file. However required seems to blow up if you do this.

Sadly, I have a dependency on nconf which does this so I can't resolve my dependencies. Do you have any recommendations on how I can work around it?

justinmchase commented 8 years ago

This worked around the issue for me:

  var opt = { cache: {} }
  opt.cache[path.join(__dirname, 'node_modules/nconf/package.json')] = { deps: [], src: '' }

  required('lib/worker/harness.js', opt, (err, deps) => {

I tried to make a PR but wasn't able to get it to work... 😢

ghost commented 8 years ago
$ node
> require('package.json')
Error: Cannot find module 'package.json'
    at Function.Module._resolveFilename (module.js:339:15)
    at Function.Module._load (module.js:290:25)
    at Module.require (module.js:367:17)
    at require (internal/module.js:16:19)
    at repl:1:1
    at REPLServer.defaultEval (repl.js:252:27)
    at bound (domain.js:287:14)
    at REPLServer.runBound [as eval] (domain.js:300:12)
    at REPLServer.<anonymous> (repl.js:417:12)
    at emitOne (events.js:95:20)

I think you meant require('./package.json')?

justinmchase commented 8 years ago

Yeah you're right, I will edit.