Javascript 如何修复在 Angular 4 中找不到模块“typescript”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44611526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:39:08  来源:igfitidea点击:

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

javascriptnode.jsangular

提问by redshot

I generated Angular 4 app 3 weeks ago using the @angular/cli. After 2 weeks, I tried to run it with the command line "ng serve"but I am prompted an error below:

我在 3 周前使用 @angular/cli 生成了 Angular 4 应用程序。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.

你知道如何解决这个问题吗?谢谢。

回答by Sajeetharan

This should do the trick,

这应该可以解决问题,

npm install -g typescript

回答by DanAbdn

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

对我来说,仅运行以下命令是不够的(尽管这是有效的第一步):

npm install -g typescript

npm install -g 打字稿

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 链接打字稿

回答by redshot

I was able to solve this problem by removing node_modules then running "npm install"

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

回答by Torsten Barthel

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. After I copied that file over all was running as expected.

来自@angular/cli 项目的根目录。在我复制该文件后,一切都按预期运行。

回答by freedev

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

回答by vicatcu

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.jsonto the subfolder along with everything else. After noticing that and moving the file into the subdirectory, all was back to normal.

将工作项目移动到文件系统上的新子目录后,我遇到了非常相似的问题。事实证明,我未能将命名.angular-cli.json的文件与其他所有内容一起移动到子文件夹中。注意到并将文件移动到子目录后,一切都恢复正常。

回答by Agustin Larreinegabe

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.

回答by Abdollah

If you use yarninstead of npm, you can install typescriptpackage 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.

可用于任何项目。

回答by SATISH RAJNALE

Run 'npm install' it will install all necessary pkg .

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