未能解析Electron v.17应用程序中聚合物3的模块



我从fluidnext获得电子快速启动和电子聚合物,用于我的聚合物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';

这对我来说很有效,这个组件在我的电子应用程序中显示,但我有很多其他组件使用其他参考文献,比如下面的这个。

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的每个新组件,当这个组件中有其他导入时,我会在引用中出现其他错误。

我该如何解决这个问题?

我也遇到了这个问题。我有一个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)

最新更新