Reputation: 21
I tried to publish my own react component wrote with TypeScript to npm. I published it succussfully, but when I created a new react project and import the npm package which I built, some error occured:
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
But I am sure that I did'nt misuse the named export and default export. So I think maybe something wrong with my webpack config or tsconfig.
my webpack.config.json
const path = require('path');
module.exports = {
mode: 'none',
entry: path.join(__dirname, 'src', 'index.tsx'),
target: 'web',
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx', '.css']
},
module: {
rules: [
{
test: /\.(ts|tsx)$/,
use: "babel-loader",
exclude: '/node_modules/',
},
{
test: /\.(ts|tsx)$/,
use: 'awesome-typescript-loader',
exclude: '/node_modules/'
},
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
},
],
},
output: {
filename: 'index.js',
path: path.resolve(__dirname, 'dist')
}
}
my tscongig.json
{
"compilerOptions": {
"outDir": "./dist",
"target": "es6",
"module": "commonjs",
"skipLibCheck": true,
"allowSyntheticDefaultImports": true,
"noImplicitAny": false,
"strictNullChecks": false,
"removeComments": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"isolatedModules": true,
"esModuleInterop": true,
"jsx": "react",
"typeRoots": [
"dist/index.d.ts",
"node_modules/@types"
]
},
"include": [
"./src/*",
],
"exclude": [
"node_modules",
"dist"
]
}
package.json
{
"name": "myname",
"version": "1.0.42",
"description": "some text",
"main": "dist/index.js",
"types": "dist",
"scripts": {
"build": "webpack --config webpack.config.js --mode production"
},
"repository": {
"type": "git",
"url": "some url"
},
"author": "secret",
"license": "MIT",
"dependencies": {},
"devDependencies": {
"@babel/core": "^7.12.3",
"@babel/preset-env": "^7.12.1",
"@babel/preset-react": "^7.12.1",
"@babel/preset-typescript": "^7.12.1",
"@types/react": "^16.9.53",
"@types/react-dom": "^16.9.8",
"@types/webpack": "^4.41.23",
"awesome-typescript-loader": "^5.2.1",
"babel-loader": "^8.1.0",
"css-loader": "^5.0.0",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"style-loader": "^2.0.0",
"typescript": "^4.0.3",
"webpack": "^5.2.0",
"webpack-cli": "^4.1.0"
}
}
and my entry point index.tsx
import someComponent from './someComponent';
export { someComponent };
and I am sure that someComponent is default export from other file.
Can anyone help me to solve this problem?
Upvotes: 2
Views: 794
Reputation: 3492
I had this problem and the solution for me was to make sure that I run npm run build
before running npm publish
. This is to ensure that the types are generated correctly before publishing. My scripts in package.json look like this:
"scripts": {
"start": "webpack serve --mode development",
"build:types": "tsc --emitDeclarationOnly",
"build": "npm run build:types && webpack --mode production",
"refreshVSToken": "vsts-npm-auth -config .npmrc"
},
Upvotes: 0