无法解析 Electron v.17 应用程序中的 Polymer 3 模块

发布于 2025-01-09 11:20:28 字数 1299 浏览 2 评论 0原文

我得到 electron-quick-start电子聚合物 来自 Fluidnext,用于我的 Polymer 3 应用程序。

当我尝试将 Web 组件导入到项目中时,出现以下错误:Uncaught TypeError: 无法解析模块说明符“@polymer/polymer/polymer-element.js”。相对引用必须以“/”、“./”或“../”开头。 为了解决这个问题,我使用了../node_modules/my-folder/my-component.js 但这仅在我导入仅引用其默认文件夹的组件时才有效。

Example: 

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

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

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:无法解析模块说明符“@polymer/polymer/polymer-legacy.js”。相对引用必须以“/”、“./”或“../”开头。

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

我该如何解决这个问题?

I get electron-quick-start and electron-polymer from fluidnext to use for my Polymer 3 application.

When i try to import my webcomponents to the project, i have got this error: Uncaught TypeError: Failed to resolve module specifier "@polymer/polymer/polymer-element.js". Relative references must start with either "/", "./", or "../". to solve this i used ../node_modules/my-folder/my-component.js but this only worked when i import a component who has only references to his default folder.

Example: 

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

This have worked for me, this component shows in my Electron Application, but i have a lot of others components who use other references, like this below.

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';

When i import this component i got this error, similar to the first: Uncaught TypeError: Failed to resolve module specifier "@polymer/polymer/polymer-legacy.js". Relative references must start with either "/", "./", or "../".

And that is my problem now, every new component i need to add ../node_modules before the default polymer import and when this component has others imports inside i got other errors to the references.

How can i solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微凉 2025-01-16 11:20:28

我也遇到了这个问题。我有一个 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 '/\1", file_content)
                    file_content = re.sub(r"from \"([^/\.~])", r"from \"/\1", file_content)
                    file_content = re.sub(r"import '([^/\.~])", r"import '/\1", file_content)
                    file_content = re.sub(r"import \"([^/\.~])", r"import \"/\1", file_content)
                    file_content = re.sub(r"require\('([^/\.~])", r"require('/\1", file_content)
                    file_content = re.sub(r"require\(\"([^/\.~])", r"require(\"/\1", file_content)
                with open(file_path, 'w') as f:
                    f.write(file_content)

I also ran into this problem. I have a Flask application where I wanted to embed web components in my templates. I solved it in the following way. I have written the following python file and am calling it in the startup file. Now everything works.

# 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 '/\1", file_content)
                    file_content = re.sub(r"from \"([^/\.~])", r"from \"/\1", file_content)
                    file_content = re.sub(r"import '([^/\.~])", r"import '/\1", file_content)
                    file_content = re.sub(r"import \"([^/\.~])", r"import \"/\1", file_content)
                    file_content = re.sub(r"require\('([^/\.~])", r"require('/\1", file_content)
                    file_content = re.sub(r"require\(\"([^/\.~])", r"require(\"/\1", file_content)
                with open(file_path, 'w') as f:
                    f.write(file_content)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文