【问题标题】:Failed to resolve module of Polymer 3 in Electron v.17 application无法解析 Electron v.17 应用程序中的 Polymer 3 模块
【发布时间】:2022-12-30 23:01:21
【问题描述】:

我从 fluidnext 获得 electron-quick-startelectron-polymer 用于我的 Polymer 3 应用程序。

当我尝试将我的 web 组件导入项目时,出现此错误:Uncaught TypeError: Failed to resolve module specifier "@polymer/polymer/polymer-element.js". Relative references must start with either "/", "./", or "../". 解决此问题我使用了 ../node_modules/my-folder/my-component.js 但这仅在我导入仅引用其默认文件夹的组件时有效。

Example: 

    import {
    html,
    PolymerElement
} from '../node_modules/@polymer/polymer/polymer-element.js';

这对我有用,这个组件显示在我的 Electron 应用程序中,但我有很多其他组件使用其他引用,如下所示。

import {
    html,
    PolymerElement
} from '../node_modules/@polymer/polymer/polymer-element.js';

// import { sharedStyles } from './shared-styles.js';

import '../node_modules/@polymer/paper-input/paper-input.js';
import '../node_modules/@polymer/iron-icon/iron-icon.js';
import '../node_modules/@polymer/iron-icons/iron-icons.js';

当我导入这个组件时,我得到了这个错误,类似于第一个:Uncaught TypeError: Failed to resolve module specifier "@polymer/polymer/polymer-legacy.js". Relative references must start with either "/", "./", or "../".

这就是我现在的问题,我需要在默认聚合物导入之前添加每个新组件../node_modules,并且当该组件内部有其他导入时,我在引用中遇到了其他错误。

我该如何解决这个问题?

【问题讨论】:

    标签: javascript electron polymer polymer-3.x


    【解决方案1】:

    我也遇到了这个问题。我有一个 Flask 应用程序,我想在我的模板中嵌入 Web 组件。我通过以下方式解决了它。我已经编写了以下 python 文件并在启动文件中调用它。现在一切正常。

    # We need to go to node_modules and add a / before the all imports paths that don't start with ~ .  or /
    
    import os
    import re
    
    def fix_relative_references():
        path_to_node_modules = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../node_modules')
    
        for root, dirs, files in os.walk(path_to_node_modules):
            for file in files:
                if file.endswith('.js'):
                    file_path = os.path.join(root, file)
                    with open(file_path, 'r') as f:
                        file_content = f.read()
                        file_content = re.sub(r"from '([^/.~])", r"from '/", file_content)
                        file_content = re.sub(r"from "([^/.~])", r"from "/", file_content)
                        file_content = re.sub(r"import '([^/.~])", r"import '/", file_content)
                        file_content = re.sub(r"import "([^/.~])", r"import "/", file_content)
                        file_content = re.sub(r"require('([^/.~])", r"require('/", file_content)
                        file_content = re.sub(r"require("([^/.~])", r"require("/", file_content)
                    with open(file_path, 'w') as f:
                        f.write(file_content)
    

    【讨论】:

      猜你喜欢
      • 2017-04-20
      • 2016-03-29
      • 2020-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-01
      相关资源
      最近更新 更多