简体   繁体   中英

Importing external dependencies in Typescript

I am a newbie in typescript/node. I have a typescript file "order.ts" from which I would like to import an external config dependency from "config.ts"

My config file code is as below

let config = {
    mongoAddress: "mongodb://localhost:27017/dts",
    dataDirectory: "../../data/"
};

module.exports = config;

I am importing the config file in the order file as below

import { config  } from "../../config";

However I am getting the TS compiler throwing error "... file not a module". Any clues how I should I be importing my external dependency in typescript

在此处输入图片说明

The main part here is you want to export your object instance. You're on the right track with that, but there may be an easier way for you.

In this case something like wrapping it in a class and exporting that:

export class Config {
    mongoAddress = 'mongodb://localhost:27017/dts';
    dataDirectory = '../../data/';
}

Notice the export before class. The same can be applied to interfaces, enums etc. By exporting it this way you can then import it and initialise it:

import { Config } from '../config';

var c = new Config();
console.log(c.mongoAddress);

This will not make it a variable, like in your original example, but you'll simply wrap it in a class. This is also why you have to initialise it first using new Config() .

Now, I'm assuming you want these properties simply to be accessed globally. And perhaps even static/readonly, so you don't have to initialise the class each time. Making use of the static typing of TypeScript, the sample would in this case be better refactored to something like this:

export class Config {
    public static readonly mongoAddress: string = 'mongodb://localhost:27017/dts';
    public static readonly dataDirectory: string = '../../data/';
}

With this, calling it is even less obtrusive - and very type safe:

console.log(Config.mongoAddress);
console.log(Config.dataDirectory);

Now exporting this way is just one of the options. It actually depends entirely on the library structure you're using throughout your application (or from third partie libraries, for that matter). It's a bit of dry reading, but I recommend you have a look at the different structures to get acquainted with terms like UMD and modules and how they relate to an import.

Hope this helps!

There are 2 ways you can do import and export.

1) default export

// config.ts
export const config = {
    mongoAddress: "mongodb://localhost:27017/dts",
    dataDirectory: "../../data/"
};
export default config;

// your other file

import configs from './config';

Note: Here you can give any name for the imported module;

2) normal export with exact declaration name while importing.

// config.ts
export const config = {
    mongoAddress: "mongodb://localhost:27017/dts",
    dataDirectory: "../../data/"
};

// your other file
import { config  } from './config';

Note: Here you have to give the exact name of the module that you exported.

Best practices to follow while exporting configs.

create a static class with static variables in the code. Which likely means that these configs are fixed stuffs.

module.exports is the node syntax for exporting modules. Typescript has a keyword names export so you can just use this:

export const config = {
    mongoAddress: "mongodb://localhost:27017/dts",
    dataDirectory: "../../data/"
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM