Use require () function without path

I am working on a node.js / express project in which I am using typescript. I have no problem importing things, but I have to use the following format, which is ugly / not scalable

import MyModule = require("../../ModuleFolder/MyModule");

Assuming my module looks like this

export module MyModule
{
    export function doStuff():void
    {
        //do stuff here
    }
}

I cannot figure out how to pull out modules and classes without specifying a path. Ideally, I would like it to be like

import MyModule = require("MyModule");

Is there something I can do to make this possible or improve this implementation?

Update: I ended up using modules for this, which works very well.

+4
source share
2 answers

if you specify the script in

var Name = require ('name')

nodeJS node_modules . , . node_modules, .

+1

- NPM ( , npm publish ) node_modules. require() , , ...

: ./node_modules/MyModule/index.ts

export module MyModule
{
    export function doStuff():void
    {
        //do stuff here
    }
}

npm init ./node_modules/MyModule, package.json.

: ./src/some/other/folder/or/elsewhere/main.ts

import MyModule = require('MyModule');
+2

All Articles