简体   繁体   English

如何修复在 Angular 4 中找不到模块“打字稿”?

[英]How to fix Cannot find module 'typescript' in Angular 4?

I generated Angular 4 app 3 weeks ago using the @angular/cli.我在 3 周前使用 @angular/cli 生成了 Angular 4 应用程序。 After 2 weeks, I tried to run it with the command line ng serve but I am prompted an error below: 2 周后,我尝试使用命令行ng serve运行它,但提示以下错误:

Cannot find module 'typescript'
Error: Cannot find module 'typescript'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (C:\Users\mypc\Documents\Angular Projects\my-angular-app\node_modules\@angular\cli\models\config\config.js:5:12)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)

Do you have any idea how to fix this?你知道如何解决这个问题吗? Thanks.谢谢。

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)您需要以下命令(我认为删除 node_modules 也可以,但以下命令更快)

npm link typescript

这应该可以解决问题,

npm install -g typescript

我能够通过删除node_modules然后运行npm install来解决这个问题

I had a similar problem when I rearranged the folder structure of a project.当我重新排列项目的文件夹结构时,我遇到了类似的问题。 I tried all the hints given in this thread but none of them worked.我尝试了这个线程中给出的所有提示,但没有一个有效。 After checking further I discovered that I forgot to copy an important hidden file over to the new directory.进一步检查后,我发现我忘记将一个重要的隐藏文件复制到新目录中。 That was那是

.angular-cli.json .angular-cli.json

from the root directory of the @angular/cli project.从 @angular/cli 项目的根目录。 After I copied that file over all was running as expected.在我复制该文件后,所有文件都按预期运行。

If you don't have particular needs, I suggest to install Typescript locally.如果你没有特别的需求,我建议在本地安装 Typescript。

NPM Installation Method NPM安装方法

npm install --global typescript # Global installation
npm install --save-dev typescript # Local installation

Yarn Installation Method纱线安装方法

yarn global add typescript # Global installation
yarn add --dev typescript # Local installation

Run: npm link typescript if you installed globally运行: npm link typescript如果你全局安装

But if you have not installed typescript try this command: npm install typescript但是如果你还没有安装 typescript 试试这个命令: npm install typescript

If you use yarn instead of npm , you can install typescript package for that workspace by running:如果您使用yarn而不是npm ,则可以通过运行为该工作区安装typescript包:

yarn add typescript

or you can install it globally by running:或者你可以通过运行全局安装它:

sudo yarn global add typescript

to be available for any project.可用于任何项目。

I had the same problem.我有同样的问题。 If you have installed first nodejs by apt and then you use the tar.gz from nodejs.org, you have to delete the folder located in /usr/lib/node_modules .如果您通过 apt 安装了第一个 nodejs,然后使用 nodejs.org 中的 tar.gz,则必须删除位于/usr/lib/node_modules中的文件夹。

I had a very similar problem after moving a working project to a new subdirectory on my file system.将工作项目移动到文件系统上的新子目录后,我遇到了非常相似的问题。 It turned out I had failed to move the file named .angular-cli.json to the subfolder along with everything else.事实证明,我未能将名为.angular-cli.json的文件与其他所有内容一起移动到子文件夹中。 After noticing that and moving the file into the subdirectory, all was back to normal.在注意到这一点并将文件移动到子目录后,一切都恢复了正常。

运行 'npm install' 它将安装所有必要的 pkg 。

如果你已经从 git 或其他地方克隆了你的项目,那么首先你应该输入npm install

The best way is to do: npm install or npm i最好的方法是: npm installnpm i

this way all dependencies will be added.这样所有依赖项都将被添加。

I had this same problem using npm run start-dev as configured my package.json ( "start-dev": "nodemon src/index.ts" ).我使用npm run start-dev配置了我的 package.json ( "start-dev": "nodemon src/index.ts" )时遇到了同样的问题。 When i used nodemon src/index.ts in cmd work just fine, but to use npm run start-dev i had to use npm link typescript , it solve the problem.当我在 cmd 中使用nodemon src/index.ts时工作得很好,但是要使用npm run start-dev我必须使用npm link typescript ,它解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM