0

Trying to run src/index.js I get the following error:

Error: Cannot find module './utils/spinner'

index.js import looks like this:

const { startSpinner, stopSpinner } = require('./utils/spinner')

Folder structure:

fold_struc

tsconfig.json looks like this:

{
  "extends": "@tsconfig/node12/tsconfig.json",
  "compilerOptions": {
    "outDir": "lib",
    "rootDir": "src",
    "sourceMap": true,
    "allowJs": true
  },
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "src/**/__tests__/*",
    "src/**/*.test.ts"
  ]
}

@tsconfig/node12/tsconfig.json looks like this:

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "display": "Node 12",

  "compilerOptions": {
    "lib": ["es2019", "es2020.promise", "es2020.bigint", "es2020.string"],
    "module": "commonjs",
    "target": "es2019",

    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  }
}


I have tried adding baseUrl to tsconfig.json and set it to .src , src, /src and tried to the same with every variation for rootDir.

3
  • You cannot import a TypeScript file from a JavaScript file Commented Mar 3, 2022 at 20:48
  • 1
    Change index.js to index.ts and run the whole thing with ts-node: npmjs.com/package/ts-node Commented Mar 3, 2022 at 20:51
  • @BennoGrimm that worked! I think the previous owner of the project must have been running it this way. If you reply again with this as the answer, I'd be glad to accept it. PS I didn't even have to change index.js to index.ts. Just running it with ts-node worked. Commented Mar 4, 2022 at 0:13

1 Answer 1

1

You can solve this by renaming index.js to index.ts and running the whole thing with ts-node: https://npmjs.com/package/ts-node.

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.