【问题标题】:module.exports value not found when used in another file在另一个文件中使用时找不到 module.exports 值
【发布时间】:2017-07-05 22:24:30
【问题描述】:

不知道为什么,我可以导入这个文件并在我的测试文件中使用expect 就好了,但是我收到了一个 eslint 错误:

error  expect not found in '../../testImports' import/named

这是我导出的地方,testImports.js

import chai from 'chai'
const expect = require('chai').expect
import { BrowserRouter } from 'react-router-dom'
import { jsdom } from './jsdom'

module.exports = {
  BrowserRouter,
  expect,
  jsdom
}

我正在导入文件:import { expect } from '../../testImports'

【问题讨论】:

  • 是的,抱歉一秒
  • 刚刚在您输入时添加了 :)
  • 废话我知道它是什么,我正在使用带有节点模块的 ES6 导入 duh
  • 奇怪的是它仍然有效....

标签: javascript node.js ecmascript-6 eslint es6-modules


【解决方案1】:

问题是eslint-plugin-import 仅适用于 ES6 模块系统导出,而不适用于 ES5。他们的意思是一样的,插件只是没有抓住它。由于插件没有看到您已导出任何内容,因此它会报告错误,但您的代码实际上并没有错。如果你想摆脱错误,只需使用 ES6 的export

export {
  BrowserRouter,
  expect,
  jsdom
};

【讨论】:

  • 酷,我将我的 testImports 更改为仅使用 es6 导出而不是节点的 module.exports,似乎清除了它
  • @PositiveGuy 是的。 module.exports = { ... } 在功能上与 export { ... } 相同,一个是 ES5,另一个是 ES6,如果您想知道为什么尽管有警告它仍然工作。
  • 谢谢!工作。 ESLint 应该给那些刚接触 JS 的人更聪明的警告。
猜你喜欢
  • 2018-09-06
  • 2019-07-31
  • 1970-01-01
  • 1970-01-01
  • 2021-09-28
  • 2016-11-26
  • 1970-01-01
  • 1970-01-01
  • 2019-09-29
相关资源
最近更新 更多