简体   繁体   English

当文件导入另一个命名的导出文件时,ES6动态导入失败

[英]ES6 Dynamic Import is failing when the file imports another named export

I have a file called a.js : 我有一个名为a.js的文件:

a.js : a.js

import { set } from 'lodash';
import { myFunc } from '../helpers';

export default 'hello';

Then I am dynamically importing the above file ( a.js ) in b.js : 然后我在b.js动态导入以上文件( a.js ):

const aFile = await import('a.js');

When I webpack the files above I get errors such as: Can't resolve 'fs' , Can't resolve 'cldr' , Can't resolve 'cldr/supplemental' , Can't resolve 'cldr/event' , Can't resolve 'cldr/supplemental' , Can't resolve 'net' , Can't resolve 'dns' . 当我对上面的文件进行webpack打包时,出现以下错误: Can't resolve 'fs'Can't resolve 'cldr'Can't resolve 'cldr/supplemental'Can't resolve 'cldr/event'Can't resolve 'cldr/supplemental' Can't resolve 'net' Can't resolve 'cldr/supplemental'Can't resolve 'net'Can't resolve 'dns'

But when I remove the import { myFunc } from '../helpers'; 但是当我import { myFunc } from '../helpers';删除import { myFunc } from '../helpers'; from a.js , the webpack runs perfectly fine. a.js ,webpack运行完美。 I am positive the path ../helpers exists and myFunc is a named export in the ../helpers file. 我肯定路径../helpers存在,而myFunc../helpers文件中的命名导出。

Why is it that when I remove that line webpack build passes but otherwise it fails? 为什么当我删除该行时,webpack构建会通过,但失败了?

See if helpers.js file is importing fs, cldr, etc. Try moving myFunc from helpers.js to a separate file and see if that fixes the issue. 查看helpers.js文件是否正在导入fs,cldr等。尝试将myFunc从helpers.js移到单独的文件中,看看是否可以解决问题。

If that doesn't fix the issue try specifying target: 'node' in your wepback config like so: 如果这样不能解决问题,请尝试在wepback配置中指定target: 'node' ,如下所示:

target: 'node',
node: {
  fs: 'empty',
  net: 'empty',
  ...
  dns: 'empty',
}

The target: 'node' option tells webpack not to touch any built-in modules like fs or net. target:'node'选项告诉webpack不要触摸任何内置模块,例如fs或net。 I'm not sure why, but adding the 'empty' strings seems to fix Can't resolve errors as well, depending on your configuration. 我不确定为什么,但是添加'empty'字符串似乎Can't resolve错误,具体取决于您的配置。

See this article: https://jlongster.com/Backend-Apps-with-Webpack--Part-I And github issue: https://github.com/webpack-contrib/css-loader/issues/447#issuecomment-285600188 看到这篇文章: https ://jlong​​ster.com/Backend-Apps-with-Webpack--Part-I和github问题: https : //github.com/webpack-contrib/css-loader/issues/447#issuecomment- 285600188

I ran into similar issues and this seemed to help me resolve the issue. 我遇到了类似的问题,这似乎可以帮助我解决问题。 Webpack is tricky. Webpack是棘手的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM