Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to module.exports another module?

Tags:

javascript

I have a file we'll call file1.ts:

export { default as function1 } from 'function1.ts';
export { default as function2 } from 'function2.ts';

I compile this using Webpack and Babel:

webpack.config.js

const path = require("path");

module.exports = {
  target: "web",
  mode: "production",
  entry: "./src/index.ts",
  output: {
    path: path.resolve(__dirname, "./lib/cjs"),
    filename: "index.js",
    libraryTarget: "commonjs2",
  },
  resolve: {
    extensions: [".ts", ".tsx", ".js"],
  },
  module: {
    rules: [
      {
        test: /\.(ts|tsx)$/,
        exclude: /(node_modules)/,
        loader: "babel-loader",
        options: {
          presets: [
            "@babel/preset-react",
            "@babel/preset-typescript",
            [
              "@babel/preset-env",
              {
                targets: ["last 2 versions"],
              },
            ],
          ],
          plugins: ["babel-plugin-styled-components"],
        },
      },
    ],
  },
};

I publish this to npm. Now I want to import it into what we'll call file2.ts:

import { function1 } from 'package';

However, function1 does not exist because if I do, for example import a from 'package';, a is undefined.

To resolve this, I decided to create another file, we'll call file0.js to do the following:

module.exports = require('./file1.js');

if I console log the require, it will be a module object with function1 and function2 as i'd expect however, module.exports = require('./file1.js'); is undefined... so I tried the following which works:

var test = require('./file1.js');
module.exports = { ...test };
  1. I don't understand why that works but module.exports = require('./file1.js'); doesn't.
  2. I don't know what the correct way I should be doing this (export an es5 module / file so I can import it in es6)
like image 955
Edward Avatar asked Jun 28 '20 16:06

Edward


People also ask

Can you have more than one module exports?

You can only have one default export for any given module, but this does not mean you are then limited to only exporting one function, it simply means you can only export one default export. As long as you only have one default export, you can still have as many named exports as you like.

What can you export with module exports?

By module. exports, we can export functions, objects, and their references from one file and can use them in other files by importing them by require() method.

How do you require module exports?

If you have a module x in which you are exporting some constructs using the module. exports object and you want to import these exported constructs in module y, you then need to require the module x in the module y using the require function.

How many exports can a module have?

Every module can have two different types of export, named export and default export. You can have multiple named exports per module but only one default export.


Video Answer


2 Answers

webpack is not designed to support emitting ES modules. Its ES module support is for apps that use ES modules internally but emit to a different format. I'd recommend using Rollup instead, which has full native ES module support, but can also support CJS with the same config if you still need it.

like image 176
Nick McCurdy Avatar answered Nov 14 '22 22:11

Nick McCurdy


I think the problem that you're seeing is because your module has no default export.

export { default as function1 } from 'function1.ts';
export { default as function2 } from 'function2.ts';

This code exposes the default export of function1.ts as function1 and the default export of function2.ts as function2, but it does not provide a default export of its own.

You could try the following instead

import { default as function1 } from "function1.ts";
import { default as function2 } from "function2.ts";

export default { function1, function2 };

Heres a CodeSandbox showing this in action. (My example mixes ts and js, but it shouldn't make a difference for what you're doing here)

Hope that helps! 👍

like image 21
JuanCaicedo Avatar answered Nov 14 '22 21:11

JuanCaicedo