简体   繁体   English

在 Chrome 控制台中编译 JavaScript 代码的 Angular 的 JIT/AOT 编译后出错(编译成功)

[英]Error after JIT/AOT compilation of Angular in Chrome console compiled JavaScript code ( it compiled successfully)

I know some people will think my question is repeated but I have searched and tried all the solutions given in other Stack Overflow posts.我知道有些人会认为我的问题重复了,但我已经搜索并尝试了其他 Stack Overflow 帖子中给出的所有解决方案。 And also I have not found a similar question where the error came up after Angular compilation.而且我还没有发现在 Angular 编译后出现错误的类似问题。

Uncaught SyntaxError: cannot use import statement outside a module未捕获的语法错误:不能在模块外使用导入语句

After clicking on the error script in the console it leads to单击控制台中的错误脚本后,它会导致

import {Observable} from 'rxjs';

Things I have tried but not worked for me:我尝试过但对我不起作用的事情:

  1. I have already added "type":"module" in the package.json我已经在 package.json 中添加了"type":"module"

  2. There are no script tags in index.html to add type="module" index.html 中没有脚本标签需要添加type="module"

  3. There are a lot of places where I have used import {this} from "that";我在很多地方都使用了import {this} from "that"; but only showing an error in rxjs part above.但仅在上面的 rxjs 部分显示错误。

  4. Tried adding "compilerOptions": { "module": "CommonJS" } , in the tsconfig file, along with setting type to module in package.json and without it, as well.尝试在 tsconfig 文件中添加"compilerOptions": { "module": "CommonJS" } ,并在 package.json 中将type设置为module ,并且没有它。

  5. Also, I am using Node.js version 16 LTS.另外,我使用的是 Node.js 版本 16 LTS。

  6. Tried updating module "target": "esnext", "module": "commonjs" , in the tsconfig file.尝试在 tsconfig 文件中更新模块"target": "esnext", "module": "commonjs"

  7. Getting error when I am trying to use require .当我尝试使用require时出现错误。

dependencies version依赖版本

I am using Angular 8.2 node.js v16.14.2我正在使用 Angular 8.2 node.js v16.14.2

tsconfig.js
compileOnSave:false,
compilerOptions"{
sourceMap:true
declaration:false
downleveliteration:true
experimentalDecorators:true
module:esnext
moduleResolution:node
importHelpers:true
allowSyntheticDefaultImports:true
target"es2015
typeRoots: [node_modules/#types]
lib[es2018,dom]
angularCompilerOptions{
fullTemplateTypeCheck:true,
strictInjectionParameters:true

This might be a non-starter but I think I ran into this a couple months ago during an upgrade and I believe I resolved it by updating the target and the lib I also have my module pinned but don't think that is it.这可能不是一个启动器,但我认为我在几个月前的升级过程中遇到了这个问题,我相信我通过更新目标和库解决了它我也固定了我的模块但不认为是这样。

    "compilerOptions": {
        "module": "es2020",
        "target": "es2017",
        "lib": ["es2020", "dom"]

You should try this...你应该试试这个...

import Observable from 'rxjs';

and check your version of 'rxjs' with check compatibility of nodejs version并通过检查 nodejs 版本的兼容性检查你的'rxjs' nodejs

I would strongly advise you to or update your angular (my preference) or downgrade your nodeJS...我强烈建议您更新或更新您的 angular(我的偏好)或降级您的 nodeJS ...

In this blog the author compiled a list of compatibility between angular and nodejs versions. 在这篇博客中作者整理了一份angular与nodejs版本的兼容性列表。

Angular 8.x is designed to be compatible with node 10.x (or 10.9.x as per blog) because those were the LTS versions at that moment in the year 2019. Maybe node 12.x would still have a better chance of compatibility. Angular 8.x 旨在与节点 10.x(或博客中的 10.9.x)兼容,因为这些是 2019 年那个时候的 LTS 版本。也许节点 12.x 仍然有更好的兼容性机会.

As Angular 8.x is no longer supported, using it with node 16.x (the current LTS) is risky.由于不再支持 Angular 8.x,将它与节点 16.x(当前的 LTS)一起使用是有风险的。 (might be the cause of your issue). (可能是您问题的原因)。

There is nothing wrong with this syntax:这种语法没有错:

import {Observable} from 'rxjs';

that's the reason for my belief that your issue can be caused by incompatibility with nodeJs.这就是我认为您的问题可能是由与 nodeJs 不兼容引起的原因。

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

相关问题 ngfactory 文件本身是否必须在 Angular 2 AOT 编译中编译? - Do the ngfactory files themselves have to be compiled in Angular 2 AOT compilation? Angular 5的默认编译是什么? (AOT或JIT) - What is the default compilation for Angular 5? (AOT or JIT) AOT(Angular)中预编译的含义是什么 - What is the meaning of pre-compiled in AOT (Angular) Angular AOT编译的应用程序不会像预期的那样摇晃 - Angular AOT compiled app not tree shaking as expected 在 VS 代码中 Angular 应用程序已成功编译,但在浏览器中没有最近的更改 - In VS code Angular application is compiled successfully ,but in browser recent changes are not there 代码编译成功但在浏览器中不起作用 - Code is compiled successfully but not working in browser 在Angular中使用JIT编译而不是使用AOT有什么优势? - Is there any advantage of using JIT compilation in Angular in favor of using AOT? 角度库中的AOT编译错误 - AOT Compilation error in angular library 如何防止Angular AOT Webpack插件编译TypeScript代码块 - How to prevent TypeScript code blocks from being compiled by the Angular AOT Webpack plugin “错误:需要使用 JIT 编译器编译可注入的‘SocialAuthService’,但‘@angular/compiler’不可用。” 在 Angular 12 - "Error: The injectable 'SocialAuthService' needs to be compiled using the JIT compiler, but '@angular/compiler' is not available." in Angular 12
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM