添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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

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" alextes, mbujold, hwanpenn, vadim-96, DPANET, matthias-ccri, brent-williams, htbkoo, aliabbas299792, kopkaa, and 6 more reacted with thumbs up emoji onny reacted with eyes emoji All reactions whyboris, brightsparc, KxllSwxtch, ceoshikhar, and whenubelieve reacted with thumbs up emoji wahur666, Oatelaus, misabiko, fr0, rexwhitten, Nufflee, IonelLupu, tuckwat, basickarl, angelch94, and 89 more reacted with thumbs down emoji All reactions

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

winnerwinnerchickdinner, martin-pabst, i474232898, eker247, EsterRibeiro, farrismelk, harshakns, Igur007, caph1993, amoretspero, and 15 more reacted with thumbs up emoji Homsi959 reacted with thumbs down emoji Nandan-Wewhare, sushiljainam-gt, and jeremythille reacted with hooray emoji EsterRibeiro, jap-cs, and aadekoyaheurx reacted with heart emoji All reactions mburst, mdunaev, bastibense, wlodpawlowski, sidelong44, otokunaga2, subnomo, mkonars, KoStard, Sceptwo, and 47 more reacted with thumbs up emoji ng-model, imans777, Sceptwo, amandabeiner, alon6699, pizzaminded, stevemu, NicoleAsqui, DandelionLove, sianliu, and elenaraujo reacted with hooray emoji josingbo, wlodpawlowski, sidelong44, otokunaga2, amandabeiner, gabrielkunkel, pavel-main, alon6699, pizzaminded, scarktt, and 6 more reacted with heart emoji pizzaminded, sianliu, and elenaraujo reacted with rocket emoji All reactions dance2die, subnomo, vadim-96, ORESoftware, inqb, codler, tsibley, felskov, adamgen, megahertz, and 21 more reacted with thumbs up emoji ORESoftware and tomek1149 reacted with hooray emoji ORESoftware, adamgen, tomek1149, and pavel-main reacted with rocket emoji All reactions

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

Tayeb-Ali, Rishabh2325, twiggler, CCBet, WasimM3, ParagMeshram, cyberguroo, and jobwat reacted with thumbs up emoji cyberguroo reacted with hooray emoji cyberguroo and jobwat reacted with heart emoji cyberguroo reacted with rocket emoji All reactions

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'.
  dphochman, OrkhanAlikhanov, tomek1149, dipbd1, Brijeshlakkad, ubershmekel, nullromo, and sunshanpeng reacted with thumbs up emoji
  vuki656 and sordado1 reacted with thumbs down emoji
  OrkhanAlikhanov reacted with heart emoji
    All reactions
          

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

yarn global add typescript
npm i -g typescript

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

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

lamas250, erkinsergey, danielmayor, Nishi05, ferngus, lukaszbudnik, 0xtaosu, and tomonari-t reacted with thumbs up emoji lamas250, erkinsergey, Nishi05, and 0xtaosu reacted with hooray emoji matinhu, apustula, innapospiech, lfelguetac, Nazaroni, sunchenlong111, bastare, ToddEmonster, babajidemm, lamas250, and 4 more reacted with heart emoji lamas250, rmsys, and 0xtaosu reacted with rocket emoji All reactions

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!