Module not found: Error: Cannot resolve module 'fs' in IT technology refers to a missing module error that occurs when the file system module 'fs' cannot be found or resolved.

7
我正在尝试将ADAL JS示例代码集成到SharePoint框架客户端Web部件中。我的代码非常简单,我已经使用NPM安装了adal、fs、node-fs等库。以下是ADAL JS示例代码的链接:https://github.com/AzureAD/azure-activedirectory-library-for-nodejs/blob/master/sample/client-credentials-sample.js
However I see this error

./~/adal-node/lib/util.js
Module not found: Error: Cannot resolve module 'fs' in /Users/luis.valencia/Documents/GraphSamples/Sample1/node_modules/adal-node/lib
resolve module fs in /Users/luis.valencia/Documents/GraphSamples/Sample1/node_modules/adal-node/lib
  looking for modules in /Users/luis.valencia/Documents/GraphSamples/Sample1/node_modules/adal-node/lib
    /Users/luis.valencia/Documents/GraphSamples/Sample1/node_modules/adal-node/lib/fs doesn't exist (module as directory)
    resolve 'file' fs in /Users/luis.valencia/Documents/GraphSamples/Sample1/node_modules/adal-node/lib
      resolve file

我现在的代码是这样的:

我甚至已经注释了 require JS 那一行,但看起来 adal js 库本身使用的 FS 模块似乎没有正确安装?

import {
  BaseClientSideWebPart,
  IPropertyPaneSettings,
  IWebPartContext,
  PropertyPaneTextField
} from '@microsoft/sp-client-preview';

import styles from './Hellomsgraph.module.scss';
import * as strings from 'hellomsgraphStrings';
import { IHellomsgraphWebPartProps } from './IHellomsgraphWebPartProps';
import * as MicrosoftGraph from "microsoft-graph"

const accessToken:string = "";
//var fs = require('fs');
var adal = require('adal-node');
var AuthenticationContext = adal.AuthenticationContext;


export default class HellomsgraphWebPart extends BaseClientSideWebPart<IHellomsgraphWebPartProps> {

  public constructor(context: IWebPartContext) {
    super(context);
  }

  public render(): void {
    this.domElement.innerHTML = `
      <div class="${styles.hellomsgraph}">
        <div class="${styles.container}">
          <div class="ms-Grid-row ms-bgColor-themeDark ms-fontColor-white ${styles.row}">
            <div class="ms-Grid-col ms-u-lg10 ms-u-xl8 ms-u-xlPush2 ms-u-lgPush1">
              <span class="ms-font-xl ms-fontColor-white">Welcome to SharePoint!</span>
              <p class="ms-font-l ms-fontColor-white">Customize SharePoint experiences using Web Parts.</p>
              <p class="ms-font-l ms-fontColor-white">${this.properties.description}</p>
              <a href="https://github.com/SharePoint/sp-dev-docs/wiki" class="ms-Button ${styles.button}">
                <span class="ms-Button-label">Learn more</span>
              </a>
            </div>
          </div>
        </div>
      </div>`;
  }

  protected get propertyPaneSettings(): IPropertyPaneSettings {
    return {
      pages: [
        {
          header: {
            description: strings.PropertyPaneDescription
          },
          groups: [
            {
              groupName: strings.BasicGroupName,
              groupFields: [
                PropertyPaneTextField('description', {
                  label: strings.DescriptionFieldLabel
                })
              ]
            }
          ]
        }
      ]
    };
  }
}

你在使用webpack吗? - Matt Browne
https://dev59.com/SJffa4cB1Zd3GeqP9IQD - Matt Browne
是的,SharePoint Framework 使用 webpack,我阅读了其他链接,但没有找到解决方案。 - Luis Valencia
fs 是 Node 中的内置模块,无需安装。 - arboreal84
1个回答

7

将以下内容添加到 webpack.config.js

target: 'node',

查看这里这里


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接