I create a lerna monorepo with two packages: A vanilla NestJS app and CRA generated app. Here I used craco to import other packages from the monorepo.
Inside the NestJS package I have a simple class that I want to share / use in the react app. It contains a node import and some methods:
import countries from 'i18n-iso-countries';
export class Cat {
constructor(private readonly name: string) {}
public get from() {
return countries.alpha2ToAlpha3('DE');
}
public makeSound() {
return 'Meow!';
}
}
Running the CRA in dev mode (yarn run start) works fine. I can create lots of cats and call their methods. So basically the monorepo setup works. However, if I run yarn run build in the CRA I get this error: Attempted import error: 'i18n-iso-countries' does not contain a default export (imported as 'countries').
If I remove the i18n-iso-countries import in the cat class, it builds just fine. I tried several variations such as named imports import {alpha2ToAlpha3} from 'i18n-iso-countries' and import * as countries from 'i18n-iso-countries'. They all work fine while using yarn start in CRA, but fail when creatinbg a production build.
My craco config.
const path = require("path");
const { getLoader, loaderByName } = require("@craco/craco");
const absolutePath = path.join(__dirname, "../backend");
http: module.exports = {
webpack: {
alias: {},
plugins: [],
configure: (webpackConfig, { env, paths }) => {
const { isFound, match } = getLoader(webpackConfig, loaderByName("babel-loader"));
if (isFound) {
const include = Array.isArray(match.loader.include)
? match.loader.include
: [match.loader.include];
match.loader.include = include.concat[absolutePath];
}
return webpackConfig;
},
},
};