2

I've tried to set up a base project with Node and TypeScript.

I cannot figure out what I've missed from my settings or what might be incorrect, I've experimented with different options for tsconfig/nodemon but nothing seems to work properly, and cannot successfully to compile the project.

Error:

/home/temp/ts-test/node_modules/ts-node/dist-raw/node-esm-resolve-implementation.js:383
    throw new ERR_MODULE_NOT_FOUND(
          ^
CustomError: Cannot find module '/home//temp/ts-test/lib/core/api' imported from /home/temp/ts-test/index.ts
    at finalizeResolution (/home/temp/ts-test/node_modules/ts-node/dist-raw/node-esm-resolve-implementation.js:383:11)
    at moduleResolve (/home/temp/ts-test/node_modules/ts-node/dist-raw/node-esm-resolve-implementation.js:818:10)
    at Object.defaultResolve (/home/temp/ts-test/node_modules/ts-node/dist-raw/node-esm-resolve-implementation.js:929:11)
    at /home/temp/ts-test/node_modules/ts-node/src/esm.ts:228:33
    at entrypointFallback (/home/temp/ts-test/node_modules/ts-node/src/esm.ts:179:34)
    at resolve (/home/temp/ts-test/node_modules/ts-node/src/esm.ts:227:12)
    at ESMLoader.resolve (node:internal/modules/esm/loader:422:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:222:40)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:40)
    at link (node:internal/modules/esm/module_job:75:36)

package.json

{
  "name": "ts-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.ts",
  "directories": {
    "lib": "lib"
  },
  "type": "module",
  "dependencies": {
    "dotenv": "^16.0.0",
    "node-fetch": "^3.2.3",
    "nodemon": "^2.0.15",
    "ts-node": "^10.7.0",
    "typescript": "^4.6.3"
  },
  "devDependencies": {
    "@types/node": "^17.0.23",
    "@types/node-fetch": "^2.6.1"
  },
  "scripts": {
    "dev": "nodemon -w src index.ts"
  },
}

nodemon.json

{
    "execMap": {
        "ts": "NODE_ENV=development  NODE_OPTIONS='--loader ts-node/esm' ts-node -T"
    },
    "watch": [
        "./lib"
    ],
    "ext": "ts,js,json,yaml,yml"
}

tsconfig.json

{
    "compilerOptions": {
        "module": "esnext",
        "target": "es6",
        "noImplicitAny": false,
        "moduleResolution": "node",
        "allowSyntheticDefaultImports": true,
        "esModuleInterop": true,
        "baseUrl": ".",
        "skipLibCheck": true,
        "lib": [
            "es6",
            "dom"
        ],
    },
    "include": [
        "lib/**/*",
    ]
}

index.ts

import * as dotenv from 'dotenv';
import { Api} from './lib/core/api';
import { HttpClient } from './lib/core/request';

dotenv.config();
const baseUrl = 'https://example.com';

const httpClient = new HttpClient(baseUrl, process.env.TOKEN);
3
  • Isn't Cannot find module '/home//temp/ts-test/lib/core/api' imported from /home/temp/ts-test/index.ts pretty clear? Commented Apr 11, 2022 at 9:25
  • it is clear, the module exists, if I will change nodemon execMap config or tsconifg option it will give another error like .ts unknown extension or export is not defined Commented Apr 11, 2022 at 17:43
  • Please ask one specific question at a time. Please provide a minimal reproducible example. Commented Apr 11, 2022 at 19:01

1 Answer 1

3

I see your problem, when using ESM index.js is not assumed by default when you specify a directory. You have to specify the filename with the extension mjs.

If you don't want to use the mjs extension, modify your package.json to add "type": "module".

You don't have to add an extension to libraries unless you import from a specific path in the library.

import * as dotenv from 'dotenv';
import { Api} from './lib/core/api/index.js';
import { HttpClient } from './lib/core/request.js';

dotenv.config();

const baseUrl = 'https://example.com';

const httpClient = new HttpClient(baseUrl, process.env.TOKEN);

In your nodemon.json config, you can simplify the execMap.ts script:

{
  "watch": ["lib"],
  "execMap": {
    "ts": "node --loader ts-node/esm"
  },
  "watch": ["lib"],
  "ext": "ts"
}
Sign up to request clarification or add additional context in comments.

Comments

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.