webpack - Vuejs : Cannot find module "."

标签 webpack vuejs2 webpack-file-loader

当我将字符串传递给计算属性中的文件加载器 require 方法时,它工作正常,如下所示

computedProp () {
  return require('../path/to/file');
}

但是如果我尝试将一些变量传递给它,它会抛出错误

computedProp () {
  const l = '../path/to/file';
  return require(l);
}

错误:错误:找不到模块“。”

我该如何解决这个问题?我想根据某些条件创建相对路径,然后将其传递给 require 方法来获取绝对路径。

最佳答案

非常相似的答案已发布在 Stack 中:

Since Webpack is running in build-time, it can't figure out which modules to bundle when the name is a dynamic variable. You can give it hints by specifying part of the path (for example, if you know all the modules are in a single directory).

Using require('...') with a variable vs. using a string in webpack

因此,为了使其发挥作用,您可以测试:

computedProp () {
  const path = '../path/to'
  const file = 'file';
  return require(path + '/' + file);
}

更多信息HereHere

希望有帮助。

关于webpack - Vuejs : Cannot find module ".",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44673258/

相关文章:

javascript - Webpack 文件加载器未在生产构建中加载 Assets 文件夹

reactjs - Webpack 文件加载器 publicPath

javascript - Webpack2 - 如何内联 SVG 文件

javascript - 升级到 Webpack 5 后无法访问 copy-webpack-plugin 中的 list

javascript - 使用 webpack 目标 Node 从 node_modules 导入 css

postgresql - 如何插入带有 id(自动递增)PostgREST 的记录?

javascript - 如何在 Vue 中切换要加密和未加密的输入字段的内容?

vue.js - 如何使用 VueJS 从 axios 数据响应下载图像 url

webpack - Electron 应用中使用 vue + webpack 引用静态资源

javascript - 在不同项目中导入和使用时,componentDidMount 函数不起作用