Angular2:安装npm后无法找到包

时间:2016-09-02 10:34:05

标签: angular npm npm-install

我尝试通过npm install安装几个软件包。例如:

npm install ng2-storage

软件包已正确安装在node_modules目录中。

我尝试通过以下方式导入库:

import { StorageSettings } from 'ng2-storage';

但是当我开始npm开始时,我得到了这个错误:

app/main.ts(3,33): error TS2307: Cannot find module 'ng2-storage'.

我对每个第三方软件包都有这个问题。

更新:将其添加到我的system-config:

/**
 * System configuration for Angular 2 samples
 * Adjust as necessary for your application needs.
 */
(function(global) {
  // map tells the System loader where to look for things
  var map = {
    'app':                        'dist', // 'dist',
    '@angular':                   'node_modules/@angular',
    'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api',
    'rxjs':                       'node_modules/rxjs',
    'ng2-storage':                'node_modules/ng2-storage'
  };
  // packages tells the System loader how to load when no filename and/or no extension
  var packages = {
    'app':                        { main: 'main.js',  defaultExtension: 'js' },
    'rxjs':                       { defaultExtension: 'js' },
    'angular2-in-memory-web-api': { main: 'index.js', defaultExtension: 'js' },
    'ng2-storage':                { main: 'index.js', defaultExtension: 'js' },
  };
  var ngPackageNames = [
    'common',
    'compiler',
    'core',
    'forms',
    'http',
    'platform-browser',
    'platform-browser-dynamic',
    'router',
    'router-deprecated',
    'upgrade',
  ];
  // Individual files (~300 requests):
  function packIndex(pkgName) {
    packages['@angular/'+pkgName] = { main: 'index.js', defaultExtension: 'js' };
  }
  // Bundled (~40 requests):
  function packUmd(pkgName) {
    packages['@angular/'+pkgName] = { main: 'bundles/' + pkgName + '.umd.js', defaultExtension: 'js' };
  }
  // Most environments should use UMD; some (Karma) need the individual index files
  var setPackageConfig = System.packageWithIndex ? packIndex : packUmd;
  // Add package entries for angular packages
  ngPackageNames.forEach(setPackageConfig);
  var config = {
    map: map,
    packages: packages
  };
  System.config(config);
})(this);

main.ts

import { StorageSettings } from 'ng2-storage';

同样的问题......

1 个答案:

答案 0 :(得分:3)

您需要将第三方软件包添加到systemjs.config.js文件中,这是您告诉应用在哪里查找文件的位置。例如,我几分钟前安装了ng2-bootstrap,我不得不添加:

var map = {
'app': 'app', // 'dist',
'@angular': 'node_modules/@angular',
'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api',
'rxjs': 'node_modules/rxjs',
'moment': 'node_modules/moment',
'ng2-bootstrap': 'node_modules/ng2-bootstrap',
};

var packages = {
'app': { main: 'main.js', defaultExtension: 'js' },
'rxjs': { defaultExtension: 'js' },
'angular2-in-memory-web-api': { main: 'index.js', defaultExtension: 'js' },
'ng2-bootstrap': { main: 'ng2-bootstrap.js', defaultExtension: 'js' },
'moment': { main: 'moment.js', defaultExtension: 'js' },
};

搜索您的node_modules文件夹,查看您需要为库添加的内容。