添加我的npm模块到导入时出错。

huangapple go评论50阅读模式
英文:

Error when adding my npm module to imports

问题

我安装并在Stackblitz中引用npm Angular2-wizard后,出现以下错误,您能帮助我理解原因吗?

我有这个Stackblitz,我安装了angular2-wizard。安装完后,我像文档中建议的那样在test.module.ts文件中引用它:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { StripeComponent } from './stripe/stripe.component';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { NgxStripeModule } from 'ngx-stripe';
import { FormWizardModule } from 'angular2-wizard';

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    ReactiveFormsModule,
    FormWizardModule
  ],
  declarations: [StripeComponent],
  exports: [
    StripeComponent
  ]
})
export class TestModule { }

我在屏幕上看到以下错误:

添加我的npm模块到导入时出错。

英文:

Any help in understanding why I'm getting this error below installing and then referencing npm Angular2-wizard in my Stackblitz.

I have this Stackblitz and I installed angular2-wizard. After I installed it I referenced it in my test.module.ts file like this (according to the docs):

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

import { NgModule } from &#39;@angular/core&#39;;
import { CommonModule } from &#39;@angular/common&#39;;
import { StripeComponent } from &#39;./stripe/stripe.component&#39;;
import { FormsModule, ReactiveFormsModule } from &#39;@angular/forms&#39;;
import { NgxStripeModule } from &#39;ngx-stripe&#39;;
import { FormWizardModule } from &#39;angular2-wizard&#39;;

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    ReactiveFormsModule,
    FormWizardModule
  ],
  declarations: [StripeComponent],
  exports: [
    StripeComponent
  ]
})
export class TestModule { }

<!-- end snippet -->

I see this error on the screen:

添加我的npm模块到导入时出错。

答案1

得分: 1

我有一种预感,这是由angular2-wizard从他们的库中的index.ts导出这些组件的方式引起的:

export * from './src/wizard.component';
export * from './src/wizard-step.component';

https://github.com/maiyaporn/angular2-wizard/blob/master/index.ts

当Stackblitz初始化时,它将代码封装/隔离到webcontainer环境中。当它尝试解析'./src/wizard.component'路由时,它很可能查看项目中定义的根src目录,而不是正确的node_modules/angular2-wizard/src目录。这可能解释了为什么在本地运行正常,但在这里却不行。

我认为修复此问题的方法是将src目录重命名为其他名称,但似乎Stackblitz对于Angular项目存在一个bug,因为它始终尝试引用/~/src/main.ts,而不管main.ts所在的目录名称如何:

Cannot read file '/~/src/main.ts': Error: ENOENT: No such file or directory., '/~/src/main.ts'. The file is in the program because: Root file specified for compilation

我将在Stackblitz上提出一个错误报告(在我提出后,将链接到这个问题),但目前看起来你运气不太好。

https://github.com/stackblitz/core/issues/2456
https://github.com/stackblitz/core/issues/2455

英文:

I have a hunch that this is caused by the the manner in which angular2-wizard are exporting these components from their library in index.ts:

export * from &#39;./src/wizard.component&#39;;
export * from &#39;./src/wizard-step.component&#39;;

https://github.com/maiyaporn/angular2-wizard/blob/master/index.ts

When Stackblitz initializes it encapsulates/sandboxes the code into a webcontainer environment. When it tries to resolve the &#39;./src/wizard.component&#39; routes it is most likely looking at the root src directory that is defined in your project instead of the correct node_modules/angular2-wizard/src directory. This may explain why this works for you locally but not here.

I figured that the fix for this would be to rename the src directory to something else, however it appears that there may be a bug in Stackblitz for Angular projects because it always tries to reference /~/src/main.ts regardless of the name of the directory main.ts is in:

Cannot read file &#39;/~/src/main.ts&#39;: Error: ENOENT: No such file or directory., &#39;/~/src/main.ts&#39;. The file is in the program because: Root file specified for compilation

I am going to raise a bug for this against Stackblitz (will link to this question when I have), but for now it looks like you are out of luck.

https://github.com/stackblitz/core/issues/2456&lt;br>
https://github.com/stackblitz/core/issues/2455

答案2

得分: -1

请参考以下内容:

而不是使用 npm 安装 angular2-wizard

尝试下载最新发布版本的源代码,然后将src/目录中的文件(包括 wizard.component.tswizard-step.component.tswizard.component.spec.tswizard-step.component.spec.ts)复制到你的 src/ 目录中。

与将 angular2-wizard 安装为一个包不同,你只需将这些组件添加到你的项目中。

英文:

Rather than using npm to install angular2-wizard

Try downloading the source of the latest release and copying the files in the src/ directory (contains the files wizard.component.ts, wizard-step.component.ts, wizard.component.spec.ts, wizard-step.component.spec.ts) to your src/ directory.

Instead of installing angular2-wizard as a package, you're just adding the components to your project.

huangapple
  • 本文由 发表于 2023年3月4日 08:41:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定