简体   繁体   English

Class(模块)未导出

[英]Class (module) is not exported

Module:模块:

import typeIs from './helpers/typeIs';

/**
 * @description Class of checking and throwing a custom exception.
 */
export default class Inspector {
// Some code
}

In package.json specified the path to the file:package.json指定文件路径:

{
// ....
"main": "dist/app.js",
// ...
}

I install the package locally using the command npm install../ PACKAGE DIRECTORY / .我使用命令npm install../ PACKAGE DIRECTORY /在本地安装 package。 Everything is installed, but the console returns {} .一切都已安装,但控制台返回{} What could be the problem?可能是什么问题呢?

Returns an empty object exactly in the minified file ( dist / app.js ).返回缩小文件 ( dist / app.js ) 中的空 object。 And if you connect the source - it works.如果你连接源 - 它有效。

.babelrc : .babelrc

{
  "presets": ["@babel/preset-env"],

  "env": {
    "test": {
      "plugins": ["transform-es2015-modules-commonjs"]
    }
  }
}

package.json : package.json :

  "devDependencies": {
    "@babel/core": "^7.10.5",
    "@babel/preset-env": "^7.10.4",
    "babel-eslint": "^10.1.0",
    "babel-jest": "^26.1.0",
    "babel-loader": "^8.1.0",
    "babel-plugin-transform-es2015-modules-commonjs": "^6.26.2",
    "clean-webpack-plugin": "^3.0.0",
    "cross-env": "^7.0.2",
    "eslint": "^7.5.0",
    "eslint-config-google": "^0.14.0",
    "eslint-loader": "^4.0.2",
    "jest": "^26.1.0",
    "webpack": "^4.44.0",
    "webpack-cli": "^3.3.12"
  },
  "browserslist": "> 0.25%, not dead",
  "dependencies": {
    "@babel/polyfill": "^7.10.4"
  }

UPD I check so UPD我检查

const Inspector = require('inspector-with-exceptions');

console.dir(Inspector);

One of the common errors in importing modules is forgetting to make them relative.导入模块的常见错误之一是忘记使它们成为相对的。 The difference between require('inspector-with-exceptions') and require('./inspector-with-exceptions') is that first one looks your node_modules and the second looks your file system. require('inspector-with-exceptions')require('./inspector-with-exceptions')之间的区别在于,第一个查看您的 node_modules,第二个查看您的文件系统。 I think the problem in your code is that.我认为您的代码中存在问题。

Edit: I have quite a lot had this problem since last week.编辑:自上周以来,我遇到了很多这个问题。 Luckily I found the way to solve it.幸运的是我找到了解决它的方法。 So the easiest way to handle this issue is to make your output property in webpack.config.js like below:因此,处理此问题的最简单方法是在 webpack.config.js 中创建 output 属性,如下所示:

//Rest of you webpack file
output: {
  path: path.resolve(__dirname, 'dist'),
  filename: <project-name>.js,
  libraryTarget: 'commonjs2',
  libraryExport: 'default',
  library: <ProjectName>
}

Use the cases as I have shown in <>.使用我在 <> 中显示的案例。 Hope this helps.希望这可以帮助。

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

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