英文:
Why do I get a 'TypeError: Cannot read properties of null (reading 'useState')' error when installing my React UI package through an absolute path?
问题
我已经为React编写了一个UI包,并通过Rollup包管理器进行编译。当我将其发布到我的个人Bitbucket存储库并从云中安装它们,例如:npm i https:lik-to-the-repo
,包可以正常工作,但当我通过绝对路径进行安装时,例如:npm i / Users/myname/Projects/Work/project-name"
。
我使用了 external: [/node_modules/]
来告诉Rollup,所有我的包都是外部的,并且应该在运行时导入。
但是,当我使用 npm pack
打包我的包并在我的存储库中链接打包文件,即 'file:../pack-dir/pack-name.tgz' 时,它可以正常工作
我面临以下错误
TypeError: Cannot read properties of null (reading 'useState')
at Object.useState (http://localhost:5173/node_modules/.vite/deps/chunk-ONATIWGS.js?v=e6bdb091:1066:29)
我指定给npm安装的项目目录是我的package.json文件所在的地方。在 /Work/project-name/package.json 中,它成功地将我的包安装到了 node_modules 中,名称与我的 package.json 中指定的一样。
我在 package.json 中添加了入口路径
"main": "dist/index.cjs",
"module": "dist/index.js",
"files": [
"dist"
],
"types": "dist/index.d.ts",
"peerDependencies": {
"react": "^18.0.0",
"react-dom": ">=16.6.0"
},
我的Rollup配置如下:
export default [
{
input: 'src/index.ts',
output: [
{
dir: 'dist',
format: 'cjs',
entryFileNames: '[name].cjs',
exports: 'auto',
// ...outputOptions,
},
{
dir: 'dist',
format: 'esm',
// ...outputOptions,
},
],
external: [/node_modules/],
plugins: [
peerDepsExternal({ includeDependencies: true }),
resolve(),
commonjs(),
// ...其他插件
],
},
// 其他配置...
];
我尝试使用 npm link
,但仍然遇到相同的问题。我正在使用React与Vite和SWC模块。
英文:
I have written a ui package for react and I am compiling them through rollup package manager. When I publish to my personal bitbucket repo and install them from the cloud eg: npm i https:lik-to-the-repo
the package works fine but when I am doing an install it through the absolute path eg; `npm i / Users/myname/Projects/Work/project-name".
I am using external: [/node_modules/]
to let rollup know that all all my packages are external and should imported on the run-time.
But it works fine when i pack my package using npm pack
and link the packed file in my repo ie. 'file:../pack-dir/pack-name.tgz'
I am facing this error
TypeError: Cannot read properties of null (reading 'useState')
at Object.useState (http://localhost:5173/node_modules/.vite/deps/chunk-ONATIWGS.js?v=e6bdb091:1066:29)
The project directory I specify for npm to install is where my package.json file exists. Inside /Work/project-name/package.json, It successfully installs my package in node_modules with the name specified in my package.json
Added the entry path in package.json
"main": "dist/index.cjs",
"module": "dist/index.js",
"files": [
"dist"
],
"types": "dist/index.d.ts",
"peerDependencies": {
"react": "^18.0.0",
"react-dom": ">=16.6.0"
},
my roll-up config
export default [
{
input: 'src/index.ts',
output: [
{
dir: 'dist',
format: 'cjs',
entryFileNames: '[name].cjs',
exports: 'auto',
...outputOptions,
},
{
dir: 'dist',
format: 'esm',
...outputOptions,
},
],
external: [/node_modules/],
plugins: [
peerDepsExternal({ includeDependencies: true }),
resolve(),
commonjs(),
preserveDirectives(),
terser(),
typescript({
tsconfig: './tsconfig.json',
exclude: [
'node_modules',
'src/icons',
'**/icons/**',
'**/examples/**',
'**/stories/**',
'**/*/stories/**',
'**/tests/**',
'./styles.css',
'**/*.stories.ts',
'**/*.stories.tsx',
],
}),
typescriptPaths(),
],
},
// Icons
{
input: 'src/icons/index.ts',
output: [
{
dir: 'dist/icons',
format: 'cjs',
entryFileNames: '[name].cjs',
exports: 'auto',
preserveModules: true,
},
{
dir: 'dist/icons',
format: 'esm',
preserveModules: true,
},
],
external: [/node_modules/],
plugins: [
resolve(),
peerDepsExternal({ includeDependencies: true }),
commonjs(),
preserveDirectives(),
terser(),
typescript({
tsconfig: './tsconfig.json',
declarationDir: 'dist/icons',
rootDir: 'src/icons',
exclude: [
'**/examples/**',
'**/stories/**',
'**/*/stories/**',
'**/tests/**',
'./styles.css',
'**/*.stories.ts',
'**/*.stories.tsx',
],
}),
// typescriptPaths(),
],
},
{
input: 'src/assets/scss/imports.scss',
output: [
{
file: 'dist/lib-ui.css',
},
],
plugins: [
postcss({
plugins: [postcssImport()],
extract: path.resolve('dist/lib-ui.css'),
minimize: true,
modules: false,
}),
],
},
{
input: 'dist/index.d.ts',
output: [{ file: 'dist/index.d.ts', format: 'esm' }],
plugins: [dts()],
external: [/\.css$/, /\.scss$/],
},
];
I tried using npm link
then too I face the same issue. I am using react with vite and swc modules.
答案1
得分: 1
从错误信息中,我可以看到React/JSX语法存在问题。
这让我产生了一些疑问:
- 您的项目 "project-name" 中是否正确安装了所有React依赖项?
- 您是否需要使用React插件?
import react from '@rollup/plugin-react'
plugins:[
react(),
],
英文:
from the error i can see something is wrong with react/JSX syntax
this makes me come a couple of doubts:
-
every react dependency is correctly installed in your project "project-name"?
-
don't you have to use the react() plugin too?
import react from '@rollup/plugin-react' plugins:[ react(), ],
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论