Skip to content

Paths set in NODE_PATH are not available in the browser process #11

Closed

Description

@probablycorey

I'm trying to move the classes in main.coffee into multiple files, but the browser process's require doesn't know about the paths set on NODE_PATH. This even fails when the first thing main.coffee does is set the NODE_PATH env variable.

Activity

  1. zcbenz commented on May 18, 2013

    @zcbenz
    Contributor

    When the main.coffee is loaded, the NODE_PATH has already been read and stored by the module system, so subsequent modifications to NODE_PATH won't have any effect on current script.

    To modify the module's search paths in main.coffee, you should append the paths to require('module').globalPaths:

    require('module').globalPaths.push(searchPath);

    This feature (modify the search paths after script is loaded) is atom-shell only, I'll document it.

  2. probablycorey commented on May 20, 2013

    @probablycorey
    Author

    Do you think we should use module.globalPaths instead of NODE_PATH for everything? It would be more consistent, but are there are downsides?

  3. zcbenz commented on May 21, 2013

    @zcbenz
    Contributor

    module.globalPaths does not work in the renderer, because of a hack that forcing module code to be evaluated under window context.

  4. probablycorey commented on May 23, 2013

    @probablycorey
    Author

    I can't get module.globalPaths to work in the browser process.

    I run this code in main.coffee

      console.log require('module').globalPaths
      require('module').globalPaths.push(resourcePath + "/src/app")
      console.log require('module').globalPaths
      require('point')

    And this is the output

    [ '/Users/corey/.node_modules',
      '/Users/corey/.node_libraries',
      '/Applications/Atom.app/Contents/lib/node',
      '/Applications/Atom.app/Contents/Resources/browser/api/lib',
      '/Applications/Atom.app/Contents/Resources/common/api/lib' ]
    [ '/Users/corey/.node_modules',
      '/Users/corey/.node_libraries',
      '/Applications/Atom.app/Contents/lib/node',
      '/Applications/Atom.app/Contents/Resources/browser/api/lib',
      '/Applications/Atom.app/Contents/Resources/common/api/lib',
      '/Users/corey/github/atom/src/app' ]
    uncaughtException:
    Error: Cannot find module 'point'
        at Function.Module._resolveFilename (module.js:351:15)
        at Function.Module._load (module.js:293:25)
        at Module.require (module.js:377:17)
        at require (module.js:393:17)
        at Object.bootstrapApplication [as preMainMessageLoopRun] (/Applications/Atom.app/Contents/Resources/app/src/main.js:76:12)
    
  5. zcbenz commented on May 24, 2013

    @zcbenz
    Contributor

    The ~/github/atom only contains .coffee sources, so you have to require coffee-script:

    require('coffee-script')
    require('coffee-cache').setCacheDir('/tmp/atom-coffee-cache')
    require('module').globalPaths.push(resourcePath + "/src/app")
    require('point')
  6. probablycorey commented on May 24, 2013

    @probablycorey
    Author

    Ahh, thanks!

  7. added a commit that references this issue on Jan 16, 2016
  8. added a commit that references this issue on Jun 26, 2016
  9. added a commit that references this issue on Sep 6, 2016
  10. release-notifier commented on Jan 31, 2018

    @release-notifier

    This PR landed in electron v1.7.12 🎉

  11. added a commit that references this issue on May 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions