错误TS2307:找不到模块' @ microsoft / sp-webpart-base'

时间:2017-10-03 12:17:39

标签: javascript typescript gulp visual-studio-code typescript-typings

我有一个具有以下文件夹结构的sharepoint框架项目:

  1. 它有一个名为dataaccess的库和一个webpart工厂方法
  2. Sharepointdataprovider.ts文件

    noti.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
    notificationManager.notify(0, noti);
    

    在我的gulpfile.js中我有这个:

    import {
      SPHttpClient,
      SPHttpClientBatch,
      SPHttpClientResponse
    } from '@microsoft/sp-http';
    import { IWebPartContext } from '@microsoft/sp-webpart-base';
    
    import List from '../models/List';
    
      import IDataProvider from './IDataProvider';
    
      export default class SharePointDataProvider implements IDataProvider {
    
        private _selectedList: List;
        private _lists: List[];
        private _listsUrl: string;
        private _listItemsUrl: string;
        private _webPartContext: IWebPartContext;
    
        public set selectedList(value: List) {
          this._selectedList = value;
          this._listItemsUrl = `${this._listsUrl}(guid'${value.Id}')/items`;
        }
    
        public get selectedList(): List {
          return this._selectedList;
        }
    
        public set webPartContext(value: IWebPartContext) {
          this._webPartContext = value;
          this._listsUrl = `${this._webPartContext.pageContext.web.absoluteUrl}/_api/web/lists`;
        }
    
        public get webPartContext(): IWebPartContext {
          return this._webPartContext;
        }
    
        public getLists(): Promise<List[]> {
          const listTemplateId: string = '171';
          const queryString: string = `?$filter=BaseTemplate eq ${listTemplateId}`;
          const queryUrl: string = this._listsUrl + queryString;
    
          return this._webPartContext.spHttpClient.get(queryUrl, SPHttpClient.configurations.v1)
            .then((response: SPHttpClientResponse) => {
              return response.json();
            })
            .then((json: { value: List[] }) => {
              return this._lists = json.value;
            });
        }
      }
    

    然而,当我尝试跑步时:

      

    gulp update-spdataaccess-typings

    我收到这些错误:

    错误TS2307:找不到模块&#39; @ microsoft / sp-webpart-base&#39;。

    'use strict';
    
    const gulp = require('gulp');
    const build = require('@microsoft/sp-build-web');
    
    //Required libraries to update typings
    var through = require('through2'),
    util = require('gulp-util'),
    spawn = require('child_process').spawn,
    clean = require('gulp-clean'),
    ts = require('gulp-typescript');
    
    //ootb
    build.initialize(gulp);
    
    // required variables to make configuration easier on the gulp tasks below
    var libsPath = 'lib/libraries';
    var srcPath = 'src/libraries';
    var spdataaccessLibraryFolder = 'spdataaccess';
    
    gulp.task('watch-spdataaccess-lib', (cb) => {
        var watcher = gulp.watch(`${srcPath}/${spdataaccessLibraryFolder}/**/*.ts`, ['update-spdataaccess-typings']);
        watcher.on('change', (event) => {
          console.log(`File ${event.path} was ${event.type}, Rebuilding library typings...`);
        });
      });
    
      gulp.task('update-spdataaccess-typings', [
        'update-spdataaccess-typings:clean-old-typings',
        'update-spdataaccess-typings:get-latest-typings',
        'update-spdataaccess-typings:build-lib-typings'
      ], () => {
      });
    
      gulp.task('update-spdataaccess-typings:clean-old-typings', () => {
        return gulp.src(`${libsPath}/${spdataaccessLibraryFolder}/**`, { read: false })
          .pipe(clean());
      });
    
      gulp.task('update-spdataaccess-typings:get-latest-typings', ['update-spdataaccess-typings:clean-old-typings'], () => {
        var tsResult = gulp.src(`${srcPath}/${spdataaccessLibraryFolder}/**/*.ts`)
          .pipe(ts({
            outDir: `${libsPath}/${spdataaccessLibraryFolder}`,
            module: 'umd',
            declaration: true
          }));
        return tsResult.dts.pipe(gulp.dest(`${libsPath}/${spdataaccessLibraryFolder}`));
      });
    
      gulp.task('update-spdataaccess-typings:build-lib-typings', ['update-spdataaccess-typings:get-latest-typings'], () => {
        return gulp.src(`${libsPath}/${spdataaccessLibraryFolder}/**/*.d.ts`)
          .pipe(updateLibTypings('spdataaccessLibrary.d.ts'))
          .pipe(gulp.dest('./typings'));
      });
    
      var updateLibTypings = function (typingsFilePath, opt) {
        var typings = ["declare module 'spdataaccess' {"];
        var latestFile;
    
        function processTypings(file, encoding, cb) {
          if (file.isNull() || file.isStream()) {
            cb();
            return;
          }
    
          latestFile = file;
    
          var contents = file.contents.toString('utf8');
          if (contents.indexOf('export declare class ') === -1) {
            cb();
            return;
          }
    
          contents = contents.replace('export declare class ', 'class ');
          typings.push(contents);
          cb();
        }
    
        function endStream(cb) {
          if (!latestFile) {
            cb();
            return;
          }
    
          typings.push('}');
    
          var file = latestFile.clone({ contents: false });
          file.path = latestFile.base + typingsFilePath;
          file.contents = new Buffer(typings.join('\r\n'));
          this.push(file)
          cb();
        }
    
        return through.obj(processTypings, endStream);
      }
    

    enter image description here

1 个答案:

答案 0 :(得分:1)

今天,当我设置SharePoint框架时遇到了同样的错误。

您需要使用以下命令:

npm install @microsoft/sp-webpart-base