- Saved searches
- Use saved searches to filter your results more quickly
- Cannot find module ‘typescript’ #707
- Cannot find module ‘typescript’ #707
- Comments
- Typescript node cannot find module
- # Cannot find module ‘X’ Error in TypeScript
- # Install the third-party module
- # Delete your node_modules and reinstall your dependencies
- # Make sure your import statements use consistent casing
- # Set moduleResolution to node in your tsconfig.json file
- # Conclusion
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
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
Cannot find module ‘typescript’ #707
Comments
when I execute: $ npx ts-node , it throwing
Cannot find module ‘typescript’
how should I fix that?
< "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" >>
The text was updated successfully, but these errors were encountered:
TypeScript is required if you want to compile using ts-node .
I have this issue with typescript installed globally.
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
ts-node need installed globally too. or both installed locally
Same issue (not sure if it’s due to NVM though).
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.
@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.
@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.
@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.
Why do it need to be behind a flag? Cant you just do a fallback if typescript isn’t installed locally, search for globally?
@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.
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.
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.
As a temporary solution I used these settings:
«code-runner.executorMap»:
@Redfish52 I’m not sure what that’s working around, but npm install typescript is the «fix» here.
What if you defaulted to using the global typescript module when ts-node is installed globally?
@jajaperson #707 (comment). You can always submit a PR too, it’s a relatively small change.
I have stumbled across this same issue just now.
import ts from 'typescript';
it is able to find typescript. But with «ES6» option it magically loses this ability
error TS2307: Cannot find module 'typescript'.
I solve this problem by reinstalling typescript globally
npm i -g typescript
I had to do a reinstall of my local node modules then it worked.
I tried to reinstall the typescript globally, and it works!
Try:
yarn global add typescript
Try one way, and if it still don’t work, then try the other way.
For me just running the below command is not enough (though a valid first step):
The following command is what you need (I think deleting node_modules works too but the below command is quicker).
For me just running the below command is not enough (though a valid first step):
The following command is what you need (I think deleting node_modules works too but the below command is quicker).
i have no idea why this occurred, but the command npm link typescript saved my day;
thank you!
You should also be able to do a normal `npm install typescript`. Typically, folks in the npm ecosystem will recommend installing dependencies locally per-project, tracking version numbers in package.json. (that’s what the above command does) This allows projects to function in isolation without potentially being broken by a different globally-installed version number. `npm link typescript` creates a symlink from your local project pointing at the globally-installed version of typescript. So it’s as if your globally installed typescript was also installed locally. `npm install typescript`, on the other hand, will install locally and track the version number in your `package.json`. This means others can quickly `npm install` and automatically get the same local environment as you, which simplifies on-boarding and makes everything more predictable and obvious.
On Tue, Jul 7, 2020, 4:30 PM MatinhoGlobal ***@***.***> wrote: 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! — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub , or unsubscribe .
Typescript node cannot find module
Last updated: Jan 20, 2023
Reading time · 3 min
# Cannot find module ‘X’ Error in TypeScript
The «Cannot find module or its corresponding type declarations» occurs for multiple reasons:
- Forgetting to install a third-party package or its type definitions.
- Having a glitched node_modules directory.
- Specifying an incorrect path to the module you are importing from.
- Setting the moduleResolution property to an incorrect value in tsconfig.json .
# Install the third-party module
If the module is a third-party module, make sure you have it installed.
Copied!npm install module-name
If the module has type definitions, make sure to install them as well.
Copied!npm install --save-dev @types/module-name
# Delete your node_modules and reinstall your dependencies
Try removing your node-modules directory and your package-lock.json file, re-run npm install and reload your IDE.
If you are on macOS or Linux, issue the following commands in bash or zsh .
Copied!# for macOS and Linux rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # 👇️ clean npm cache npm cache clean --force # 👇️ install packages npm install
If you are on Windows, issue the following commands in CMD.
Copied!# for Windows rd /s /q "node_modules" del package-lock.json del -f yarn.lock # 👇️ clean npm cache npm cache clean --force # 👇️ install packages npm install
Make sure to reload your IDE as VS Code often glitches and needs a reboot.
# Make sure your import statements use consistent casing
Some operating systems like Windows treat file and directory names as case-insensitive.
However, macOS and Linux treat file and directory names as case-sensitive.
Make sure you haven’t misspelled the path to the module in your import statement.
Copied!import country > from './myFile'; import country > from './MyFile';
The two import statements do the same on Windows machines because file names on Windows are case-insensitive.
However, if you run this code on a Unix-like operating system like Linux or macOS, you would get an error, because they treat file names case-sensitively and these are 2 completely different files.
A best practice is to use the forceConsistentCasingInFileNames option in your tsconfig.json file.
Copied!"compilerOptions": "forceConsistentCasingInFileNames": true, // . > >
When the forceConsistentCasingInFileNames option is set to true , TypeScript will issue an error if you try to include a file by using a different casing from the casing of the file name on the disk.
For example, if you try to import from MyFile.ts , but the name of the file is myFile.ts , you’d get an error.
When this option is set to false , TypeScript follows the case sensitivity rules of the file system it’s running on.
This can be very confusing if some of the developers on your team use Windows machines and other Unix-like OS, because if you try to import myFile.ts by specifying MyFile.ts , it will be found on Windows, but not on macOS or Linux.
# Set moduleResolution to node in your tsconfig.json file
If that doesn’t help or TypeScript can’t locate your local modules, try setting moduleResolution to node in your tsconfig.json file.
Copied!"compilerOptions": "moduleResolution": "node", // 👇️ . rest > >
You can read more about classic vs node module resolution in the TypeScript docs.
If that doesn’t help, make sure the module you are trying to import is tracked by TypeScript.
It should be covered in your include array setting and not be present in the exclude array in your tsconfig.json file.
Copied!"compilerOptions": // . >, "include": ["src/**/*"], "exclude": ["node_modules", "src/**/*.spec.ts"] >
For example, if the module is out of the src directory whilst using the configuration from the code snippet, TypeScript wouldn’t be able to find it.
Make sure you haven’t excluded the module by adding it to your exclude array.
If your error message changes to «Could not find declaration file for module ‘module-name'», then TypeScript has located the module you are trying to import, but can’t find its type declarations.
If that is the case, check out my other article — Could not find declaration file for module ‘X’ Error.
If you got the error when using any of the following packages, click on the relevant article:
# Conclusion
The «Cannot find module or its corresponding type declarations» error occurs when TypeScript cannot locate a third-party or local module in our project.
To solve the error, install the module and try setting moduleResolution to node in your tsconfig.json file.
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.