简体   繁体   English

无法在需要json文件的Angular 4中导入npm软件包

[英]Can't import npm package in Angular 4 that requires a json file

I made a fresh install of an Angular 4 project and I'm trying to load the npm package steem in it like this: 我重新安装了Angular 4项目,并尝试像这样在其中加载npm包steem

app.component.ts app.component.ts

import {Component, OnInit} from '@angular/core';
import * as steem from 'steem';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  title = 'app works!';

  ngOnInit() {
    steem.api.getAccounts(['test', 'test2'], function(err, response){
      console.log(err, response);
    });
  }
}

However, the following error occurs: 但是,发生以下错误:

Failed to compile.

./~/steem/lib/api/index.js
Module not found: Error: Can't resolve './methods' in '/Users/Stephan/Dev/kanzeon/node_modules/steem/lib/api'
 @ ./~/steem/lib/api/index.js 17:15-35
 @ ./~/steem/lib/index.js
 @ ./src/app/app.component.ts
 @ ./src/app/app.module.ts
 @ ./src/main.ts
 @ multi webpack-dev-server/client?http://localhost:4200 ./src/main.ts

It seems to me that this is caused because the ./methods that it can't resolve is a JSON file. 在我看来,这是由于无法解析的./methods是JSON文件引起的。 Angular-cli's webpack config does include a json loader (and I've installed the json-loader npm package), but in the loader the line exclude: 'node_modules' is present. Angular-cli的webpack配置中确实包含一个json加载器(并且我已经安装了json-loader npm软件包),但是在加载器中该行exclude: 'node_modules'存在。

I really wouldn't like to override angular-cli's webpack config with my own (there's a good reason why they hide its implementation, it's configured very efficiently) but this does pose a problem for my app. 我真的不想用我自己覆盖angular-cli的webpack配置(有充分的理由为什么他们隐藏了它的实现,它的配置效率很高),但这确实给我的应用带来了问题。

Is there a way to make sure I can include the steem npm package and have it load its json file without overriding the webpack config? 有没有一种方法可以确保我可以包含steem npm软件包并使其加载json文件而不会覆盖webpack配置?

steem-js requires JSON files internally, so you need JSON loader configured steem-js内部需要JSON文件,因此您需要配置JSON加载程序

https://github.com/steemit/steem-js/tree/master/examples/webpack-example https://github.com/steemit/steem-js/tree/master/examples/webpack-example

So I'm afraid you'll have to extend/override angular-cli's webpack config. 因此,恐怕您将不得不扩展/覆盖angular-cli的webpack配置。 Unfortunately it seems Angular doesn't yet provide a way to extend/override their webpack config . 不幸的是,Angular 似乎尚未提供扩展/覆盖其webpack配置的方法

Are hooks a possibility? 钩子有可能吗?

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

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