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 };
module.exports = require('./file1.js');
doesn't.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.
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.
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.
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.
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.
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! 👍
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With