0

I'm trying to convert an existing JavaScript codebase to TypeScript one file at a time. Hitting a problem with the imports.

Converting:

// JavaScript
const AWS = require("aws-sdk");

to:

// TypeScript
import AWS = require("aws-sdk");

works as the AWS SDK is installed in node_modules. When I try to import one of my own modules, converting:

// This works, but is it "correct"? Path here is relative to compiled .js file
const C = require('../utils/constants');

to:

import C = require('../utils/constants');

it fails with Cannot find module '../utils/constants'.ts(2307).

For context, here are the paths of the concerned files (all paths relative to project root):

  1. Module file I am trying to use: utils/constants.js
  2. Original .js file: utils/sesUtil.js
  3. Equivalent .ts file: src/utils/sesUtil.ts (this is the one we're discussing here)
  4. Compiled .js file: dist/sesUtil.js

Leads me to some questions:

  1. What is the correct way to import my own (CommonJS) JavaScript modules?
  2. Should the path be relative to where the .ts file exists (src/utils/ses.ts) or relative to where the compiled .js file will go (dist/ses.js)?

Here's my tsconfig.json file:

{
    "compilerOptions": {
        "module": "commonjs",
        "esModuleInterop": true,
        "target": "es6",
        "noImplicitAny": true,
        "moduleResolution": "node",
        "sourceMap": true,
        "outDir": "dist",
        "baseUrl": ".",
        "paths": {
            "*": [
                "node_modules/*",
                "src/types/*"
            ]
        }
    },
    "include": [
        "src/**/*"
    ]
}
1
  • As already posted in answer - do not use require. Use import * as C from '' or import C from ''. Also, make sure to set rootDir and outDir like "mirroring". Your outDir structure will be mirroring your rootDir structure. Commented Apr 30, 2020 at 7:33

2 Answers 2

1

Regard to problems with path. Make sure to set, at least, rootDir and outDir properties - reference.

--rootDir - specifies the root directory of input files. Only use to control the output directory structure with --outDir.

--outDir - redirect output structure to the directory.

E.g. you have a source file in src/animal/Dog.ts. It will appear in dist/animal/Dog.js with the following setup:

"rootDir": "src",
"outDir": "dist"

include field is also must to be set as described here. These are the usual UNIX glob patterns for the files to include for compilation. You also can use files, but files is for including the exact file, it is not a glob pattern. So community prefers to use include.

Regard to require in TypeScript code. Preferably to forget about require at all (except some edge cases). If you want to import evertything from the module, use:

import * as MyModule from './MyModule';
import MyModule from './MyModule';
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. What do I do with the include in tsconfig.json? Leave it as it is or remove it completely?
Leave it as is. It is a proper glob pattern for "include everything from sources". I'll update the answer.
0

Replacement for:

const C = require('../utils/constants');

is:

import * as C from '../utils/constants';

Regarding import paths - relative to the file you are importing into, or relative to project's root directory, I decide based on context.

  1. Are those 2 files part of the same implementation and likely to move together (for example to new folder)? Then use ../ paths, relative to each other.
  2. Is file I am importing part of separate unit/implementation? Then use path from root directory (eg src/...).

1 Comment

This didn't work for me. I've added a list explaining the locations of the various files. Maybe that will help explain better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.