ts-node: Cannot find module 'typescript'

when I execute: $ npx ts-node, it throwing Cannot find module 'typescript' how should I fix that?

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es2017",
    "noImplicitAny": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "preserveConstEnums": true,
    "outDir": "dist"
  },
  "include": [
    "src/**/*"
  ]
}

src/index.ts

console.log("test");

package.json

{
  "name": "agent",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "start": "tsc && node dist/index.js",
    "dev": "nodemon --watch 'src/**/*.ts' -e ts,tsx --exec 'ts-node' ./src/index.ts",
    "debug": "nodemon --inspect --watch 'src/**/*' -e ts,tsx --exec 'ts-node' ./src/index.ts"
  },
  "dependencies": {
    "@types/koa-router": "^7.0.32",
    "koa": "^2.5.3",
    "koa-router": "^7.4.0",
    "nodemon": "^1.18.4",
    "pg": "^7.5.0"
  },
  "devDependencies": {
    "@types/koa": "^2.0.46",
    "@types/pg": "^7.4.11",
    "ts-node": "^7.0.1"
  }
}

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 17
  • Comments: 38 (8 by maintainers)

Most upvoted comments

TypeScript is required if you want to compile using ts-node.

You’ll need to install typescript locally since v8, it’s using require.resolve which has its own separate limitations but is used to resolve a different issue. That means typescript is resolved from where you’re running it right now.

@blakeembrey It would be really nice to be able to run this with typescript installed globally. I would love to be able to quickly run .ts files in node without having to init a project, install typescript again etc.

ts-node need installed globally too. or both installed locally

This did the trick for me.

For me just running the below command is not enough (though a valid first step):

npm install -g typescript

The following command is what you need (I think deleting node_modules works too but the below command is quicker).

npm link typescript

@Redfish52 There’s a 👍 button if you just want to chime in and aren’t contributing to the issue.

For anyone else, I’d like to know if a flag is adequate enough for now.

I fixed mine by typing npm i -D @types/node typescript ts-node

@Redfish52 I’m not sure what that’s working around, but npm install typescript is the “fix” here.

I solve this problem by reinstalling typescript globally npm i -g typescript

I tried to reinstall the typescript globally, and it works! Try:

yarn global add typescript

or

npm i -g typescript

Try one way, and if it still don’t work, then try the other way.

I have stumbled across this same issue just now.

    "ts-loader": "^6.0.3",
    "ts-node": "^8.3.0",

In ts file:

import ts from 'typescript';

tsconfig:

{
  "compilerOptions": {
    "outDir": "./dist/",
    "sourceMap": true,
    "noImplicitAny": true,
    "module": "ES6",
    "target": "es6",
    "jsx": "react"
  }
}

If I change module to

"module": "commonjs"

it is able to find typescript. But with “ES6” option it magically loses this ability

error TS2307: Cannot find module 'typescript'.

I have this issue with typescript installed globally.

Here is the error:

Error: Cannot find module ‘typescript’ at Function.Module._resolveFilename (internal/modules/cjs/loader.js:580:15) at Function.resolve (internal/modules/cjs/helpers.js:30:19) at Object.register (C:\Users\marcb\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:208:28) at Object.<anonymous> (C:\Users\marcb\AppData\Roaming\npm\node_modules\ts-node\src\bin.ts:108:17) at Module._compile (internal/modules/cjs/loader.js:688:30) at Object.Module._extensions…js (internal/modules/cjs/loader.js:699:10) at Module.load (internal/modules/cjs/loader.js:598:32) at tryModuleLoad (internal/modules/cjs/loader.js:537:12) at Function.Module._load (internal/modules/cjs/loader.js:529:3) at Function.Module.runMain (internal/modules/cjs/loader.js:741:12) npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! vipor-rest@0.1.0 dev: ts-node ./src/server.ts npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the vipor-rest@0.1.0 dev script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\marcb\AppData\Roaming\npm-cache_logs\2019-01-23T10_03_59_349Z-debug.log

Happens with the following packages: ts-node@8.0.1 typescript@3.2.4

yarn add -D typescript

Once in a while, I’m in a stripped-down environment where I’d like to run ts-node via npx without permanently installing anything (e.g. kubectl exec -it prod-api /bin/sh). Since typescript isn’t a dependency, the temporary package downloaded by npx ts-node throws this error.

You can get around this by telling npx to also download a temporary copy of typescript:

npx -p typescript -p ts-node ts-node

(tested with npx v6.14.8)

I fixed mine by typing npm i -D @types/node typescript ts-node

this worked for me as well ,thank you!

@Jood80 thanks also fixed my error npm i -D @types/node typescript ts-node

This did the trick for me.

For me just running the below command is not enough (though a valid first step):

npm install -g typescript

The following command is what you need (I think deleting node_modules works too but the below command is quicker).

npm link typescript

i have no idea why this occurred, but the command npm link typescript saved my day; thank you!

I had to do a reinstall of my local node modules then it worked.

Any progress on the flag?

As a temporary solution I used these settings: "code-runner.executorMap": { "typescript": "tsc $fileName && node $fileNameWithoutExt" }