Class (module) is not exported

Viewed 726

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:

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

I install the package locally using the command npm install ../ PACKAGE DIRECTORY /. Everything is installed, but the console returns {}. What could be the problem?

Returns an empty object exactly in the minified file (dist / app.js). And if you connect the source - it works.

.babelrc:

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

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

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

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

console.dir(Inspector);
1 Answers

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. 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:

//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.

Related