1

I am trying to import a local module on a node.js application and I do not seem to be able to.

modules/date.js :

const getDate = () => {...};
const getDay = () => {...};
export {getDate, getDay};

app.ts :

import {getDay, getDate} from "/modules/date";
//import {getDay, getDate} from "/modules/date.js"; doesn't work either

The error : Cannot find module '/modules/date' or its corresponding type declarations.ts(2307)

Config files

package.json :

 {
  "dependencies": {
    "ejs": "^3.1.8",
    "express": "^4.18.2",
    "mongoose": "^6.8.3",
    "nodemon": "^2.0.20",
    "typescript": "^4.9.4"
  },
  "name": "todolist",
  "version": "1.0.0",
  "description": "A simple todo list",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Infinite Learner",
  "license": "ISC",
  "devDependencies": {
    "@types/express": "^4.17.15"
  },
  "type": "module"
}

tsconfig.json:

{
  "compilerOptions": {
    "target": "es2016",                                  /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
    "lib": ["es6"],                                      /* Specify a set of bundled library declaration files that describe the target runtime environment. */
   
    /* Modules */
    "moduleResolution": "node",
    "rootDir": "./",                                     /* Specify the root folder within your source files. */
   
    /* Emit */
    "outDir": "build",                                   /* Specify an output folder for all emitted files. */
   
    /* Interop Constraints */
    "esModuleInterop": true,                             /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
    "forceConsistentCasingInFileNames": true,            /* Ensure that casing is correct in imports. */

    /* Type Checking */
    "strict": true,                                      /* Enable all strict type-checking options. */

    /* Completeness */
    "skipLibCheck": true                                 /* Skip type checking all .d.ts files. */
  }
}

Do you know what I am missing ?

Thank you.

0

1 Answer 1

1

Well because you re trying to import a Js file into a Ts file, either you change into date.ts or add some setting into tsconfig.json, maybe check this threat : NodeJS How to import JS file into TypeScript

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, it was exactly this problem !

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.