亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

導入自定義 CommonJS 模塊失敗

導入自定義 CommonJS 模塊失敗

慕田峪7331174 2022-01-07 20:48:17
我通過以下方式在項目 A 中創建了一個 CommonJS 模塊:const { WebElement } = require('selenium-webdriver');const { By } = require('selenium-webdriver');class VlElement extends WebElement {    constructor(driver, selector) {        ...    }    async getClassList() {        ...    }}module.exports = VlElement;在項目 BI 中使用以下代碼:const VlElement = require('projectA');class VlButton extends VlElement {      constructor(driver, selector) {        super(driver, selector);    }    ...}module.exports = VlButton;運行代碼時,找不到 VLElemlent。它在我的里面package.json,我可以在projectB > node_modules > projectA. 我的出口做錯了什么?
查看完整描述

1 回答

?
莫回無

TA貢獻1865條經驗 獲得超7個贊

確保你有一個projectB/mode_modules/package.jsonwithmain指向定義/導出的文件VlElement,如下所示:


  "main": "path/to/file/with/VlElement.js",

當您調用require('projectA');它時,必須將其解析為 projectA 內的文件,以便可以評估(并返回)該文件的導出。中的main條目package.json允許這樣做(但默認為index.js,所以如果你使用它,你package.json可能不需要它,但無論如何你都應該擁有它)。


您可以擁有多個具有各種導出的文件,但請記住require('projectA');仍然只能返回一件事,所以這樣做的方法通常是有一個index.js看起來像這樣的:


module.exports = {

    'something': require('./something.js'),

    'otherthing': require('./otherthing.js'),

    'etc': require('./etc.js'),

};


查看完整回答
反對 回復 2022-01-07
  • 1 回答
  • 0 關注
  • 279 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號