Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot find module 'typescript' #707

Closed
s97712 opened this issue Oct 12, 2018 · 38 comments
Closed

Cannot find module 'typescript' #707

s97712 opened this issue Oct 12, 2018 · 38 comments

Comments

@s97712
Copy link

s97712 commented Oct 12, 2018

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"
  }
}
@s97712
Copy link
Author

s97712 commented Oct 12, 2018

I try to run npx ts-node -e 'console.log("test")', it still throwing
Cannot find module 'typescript'

@blakeembrey
Copy link
Member

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

@mbujold
Copy link

mbujold commented Jan 23, 2019

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. (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

@s97712
Copy link
Author

s97712 commented Jan 23, 2019

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

@hwanpenn
Copy link

same problem

@dance2die
Copy link

Same issue (not sure if it's due to NVM though).

image

@blakeembrey
Copy link
Member

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.

@adueck
Copy link

adueck commented Jan 30, 2019

@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.

@blakeembrey
Copy link
Member

@adueck It makes sense, but isn't going to happen in 8.0 right now. I could put it behind a flag though. See reasoning in #697.

@blakeembrey
Copy link
Member

@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.

@ORESoftware
Copy link

ORESoftware commented Feb 7, 2019

@blakeembrey putting it behind a flag would be just fine for me, sounds good. Question of course is what should the flag be called, shall we bikeshed? :) how about --use-global-version or --ugv for short. I am sure someone can think of something better.

@codler
Copy link

codler commented Feb 9, 2019

Why do it need to be behind a flag? Cant you just do a fallback if typescript isn't installed locally, search for globally?

@blakeembrey
Copy link
Member

@codler That creates an inconsistent environment for people, e.g. a dev environment may have it installed locally and another person not installed locally. This can create simple misunderstandings and more issues.

@codler
Copy link

codler commented Feb 9, 2019

Hm, locally is bound to a project in package.json, and when its bound to project I cant see how the other person wouldn't have it locally also without installed the project wrongly.

@blakeembrey
Copy link
Member

What about if you never did npm install? That'll use both the global ts-node and globally typescript automatically. What about if someone else didn't add it in the package.json? Right now, I'm against trying to automatically resolve via two different paths and not sure what value it'd really add.

@vadim-96
Copy link

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

@blakeembrey
Copy link
Member

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

@jajaperson
Copy link

Any progress on the flag?

@jajaperson
Copy link

What if you defaulted to using the global typescript module when ts-node is installed globally?

@blakeembrey
Copy link
Member

blakeembrey commented Feb 16, 2019

@jajaperson #707 (comment). You can always submit a PR too, it's a relatively small change.

@Deamon87
Copy link

Deamon87 commented Jul 1, 2019

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'.

@hamad-Adel
Copy link

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

@basickarl
Copy link

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

@haixiangyan
Copy link

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.

@byteab
Copy link

byteab commented Jun 6, 2020

yarn add -D typescript

@KarmaBlackshaw
Copy link

KarmaBlackshaw commented Jun 18, 2020

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

@matinhu
Copy link

matinhu commented Jul 7, 2020

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!

@cspotcode
Copy link
Collaborator

cspotcode commented Jul 7, 2020 via email

@Jood80
Copy link

Jood80 commented Sep 27, 2020

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

@Wandersonelias
Copy link

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

@installH
Copy link

直接进入blinker-js目录,然后运行 npm install typescript 就能解决问了!

@poobah-fruitful
Copy link

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

this worked for me as well ,thank you!

@mildmojo
Copy link

mildmojo commented May 7, 2021

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)

@cspotcode
Copy link
Collaborator

cspotcode commented May 7, 2021 via email

@mildmojo
Copy link

mildmojo commented May 7, 2021

@cspotcode Ah, yep, updating to npm v7.12.0, I see npx is deprecated in favor of a stripped-down version invoked with npm x. It now interactively prompts about downloading packages, suppressed with -y, so this gets me a REPL without errors in the new npm:

npm x -y ts-node

@cspotcode
Copy link
Collaborator

@mildmojo Thanks, this is good to know and I'm sure will help other people as well..  I added it to the discussion forum to make it easy to find.  #1305

@rosnk
Copy link

rosnk commented May 29, 2021

had "ts-node" and "typescript" installed globally. was still showing error.

i was missing tsconfig.json file.... which you can generate by running

tsc --init

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests