Skip to content

require.main.require imports do not work #19117

Open

Description

@mjbvz

TypeScript Version: 2.6.0-dev.201xxxxx

From: microsoft/vscode#35489 (comment)

Code

const myModule = require.main.require('./file.js')

Run go to definition on myModule

Expected behavior:
Goes to defintion of myModule

Actual behavior:
Nothing happens. Looks like we don't understand require.main.require style imports

Activity

  1. changed the title [-]require.main.require does not have path intellisense [/-] [+]require.main.require imports do not work[/+] on Oct 11, 2017
  2. mhegazy commented on Oct 11, 2017

    @mhegazy
    Contributor

    the analysis of module imports in .js files is limited to the known require call. require.main is not a pattern the compiler looks for. it is also early in the compilation process to know that require.main.require is an alias to require.

    This is currently a design limitation.

  3. weswigham commented on Oct 11, 2017

    @weswigham
    Member

    Mohamed Hegazy (@mhegazy) Worse yet, require.main.require isn't strictly an alias to require - it's a require relative to the package entrypoint... Something we don't currently define.

  4. rafaelmaeuer commented on Jul 24, 2018

    @rafaelmaeuer

    I am looking forward to this feature too. Intellisense is not able to understand require.main.require as alias for require. But this method helps a lot for readability of code when you have files in deeper folder structures.

  5. TheColorRed commented on Jun 1, 2019

    @TheColorRed

    I am not sure if this is the same issue, but what I want is to have the types in the current package, but I want the root package to include 'mongodb'.

    When I do the following:

    import mongo = require.main.require('mongodb')

    I get the following error:

    Cannot find namespace 'require'.

    However, the following works, but isn't what I want:

    import mongo = require('mongodb')
  6. phramos07 commented on Apr 13, 2020

    @phramos07

    Apparently require.main.require is referencing the Main package of your program, i. e., the entry script. When running with vscode code & debug tool, this package appears to be something else rather than my node js main process, because it fails to find the module require inside the main.

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

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureDomain: JavaScriptThe issue relates to JavaScript specificallySuggestionAn idea for TypeScriptVS Code TrackedThere is a VS Code equivalent to this issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions