I am trying to develop an NPM module that will provide react components. Right now, I'm just trying to set up the package and cannot get JSX in the components to be recognized in an example react project using the library.
My npm package project has one component:
import React from 'react';
class AfWin extends React.Component {
render() {
return <div></div>;
}
}
export default AfWin;
And this component is referenced in the index.js file pointed to as main in the package.json file:
import AfWin from './AfWin';
export default { AfWin };
To pack and import this package, I use a command prompt and type: npm pack. Then, I go to my example create-react-app and import the created .tgz file with npm install C:\path\to\file.tgz. This works, and I am able to get the AfWin component in my example app. However, the <div></div> or any JSX I try causes a problem at runtime:
Module parse failed: C:\path\to\project\node_modules\module-name\AfWin.js Unexpected token (6:15)
You may need an appropriate loader to handle this file type.
|
| render() {
| return <div></div>;
| }
| }
To make the problem more concrete, I published my broken module to npm: npmjs.com/package/react-affine
Related
Anyone knows how I can import each of below function in Vue and React without an error?
Right now I am getting an error if I am importing vueFetch and reactFetch.
So if am importing vueFetch into a Vue project, I am getting an error for React that React is not defined and the same applies if I import reactFetch (Vue ref not defined).
import { vueFetch } from './composables/vue/vue-fetch';
import { reactFetch } from './composables/react/react-fetch';
export { vueFetch, reactFetch };
I am trying to make a Vue and React NPM package, but I only have one index file, and I need to import both Hook into the single index file. So I do not need to create 2 packages.
here is the link to the package: https://www.npmjs.com/package/use-lightweight-fetch
I hope someone can tell me where I am going wrong in trying to get usable libraries
I have created a NPM project using create-react-app --format typescript, I then created the following structure:
|->tsconfig.json
|->package.json
|->config
|->tsconfig.base.json
|->tsconfig.cjs.json
|->tsconfig.esm.json
|->src
|->index.tsx
|->components
|->index.ts
|->ExampleComponent
|->ExampleComponent.component.tsx
|->ExampleComponent.stories.tsx
|->ExampleComponent.types.ts
|->index.ts
In this example the Example Component looks something like the following:
|->tscon
import React, { FC } from 'react';
// Contact specific icons
import Container from 'react-bootstrap/Container';
// Footer Properties
import { ExampleProperties } from './ExampleComponent.types';
export const ExampleComponent: FC<ExampleProperties> = ({ text }) => {
return (<Container fluid>{text}</Container>);
};
for the tsconfig files in 'config' I've lifted the Synk recommendation directly, while tsconfig.json is fairly simple like so:
{
"extends": "./configs/tsconfig.esm.json",
}
If I start the application via 'npm start' I get a website and the component correctly appears, but the issue is trying to import into another project.
I using npm link and npm link #Example/ExampleProject to bring the project in to another website and the index.tsx of that project looks like so:
import React from 'react';
import { createRoot } from 'react-dom/client';
import { ExampleComponent } from '#Example/ExampleProject';
const container = document.getElementById('root');
if (!container) throw new Error('Failed to find the root element') const root = createRoot(container);
root.render(
<React.StrictMode>
<main role={"main"} >
<ExampleComponent/>
</main>
</React.StrictMode> );
But when it starts I am getting this error:
Module not found: Error: Can't resolve './ExampleComponent/index' in '/home/user/IdeaProjects/ExampleProject/dist/esm' Did you mean 'index.mjs'? BREAKING CHANGE: The request './Common/index' failed to resolve only because it was resolved as fully specified (probably because the origin is strict EcmaScript Module, e. g. a module with javascript mimetype, a '*.mjs' file, or a '*.js' file where the package.json contains '"type": "module"'). The extension in the request is mandatory for it to be fully specified. Add the extension to the request.
The only thing I can think is tsc generates src/components/index.ts as a /src/component/index.js (which I rename). But ExampleComponent has .js and .mjs files within its directory
i installed antd to frontend of my app, i have this at the app.jsx
import "antd/dist/antd.css"
import { Button } from 'antd'
and now in my first component i am trying to use like this:
import antd from 'antd'
const { Card } = antd
but now i am getting this error
[plugin:vite:import-analysis] Failed to resolve import
"antd/es/default/style" from ".../component.jsx". Does the file exist?
.../component.jsx:1:9 1 | import 'antd/es/default/style';;import
RefreshRuntime from "/#react-refresh"; | ^
in node_modules there is not have the default 'ant/es/default' folder
If you have installed everything correctly to your node_modules folder (e.g. npm install or yarn install) you should be able to import either antd/dist/antd.css or if you just want to have the styles for the inputs antd/es/input/style/index.css
I am developing application using create-react-app, and using third party module which is not compile, I am including JSX file from this to my project.
getting following error when start or build
******.jsx
Module parse failed: Unexpected token (12:25)
You may need an appropriate loader to handle this file type.
My react application is not eject and don't want to eject.
I don't want to eject from react-script
Sample code
Link.jsx in Library
import React from 'react';
import { string } from 'prop-types';
import './Link.scss';
const STATUS = {
HOVERED: 'hovered',
NORMAL: 'normal',
};
class Link extends React.Component {
constructor(props) {
super(props);
this.onMouseEnter = this.onMouseEnter.bind(this);
this.onMouseLeave = this.onMouseLeave.bind(this);
this.state = {
className: STATUS.NORMAL,
};
}
onMouseEnter() {
this.setState({ className: STATUS.HOVERED });
}
onMouseLeave() {
this.setState({ className: STATUS.NORMAL });
}
render() {
const { className } = this.state;
const { page, children } = this.props;
return (
<a
className={className}
href={page}
onMouseEnter={this.onMouseEnter}
onMouseLeave={this.onMouseLeave}
>
{children}
</a>
);
}
}
Link.propTypes = {
page: string,
children: string,
};
Link.defaultProps = {
page: '#',
children: '',
};
export default Link;
Above code is publish to internal npm repo and used in application
App.jsx in application
import { Link} from '#myScope/myreactlib/Link'; // loaded from node_modules
App.jsx give error
When using create-react-app without ejecting, you will have some restrictions on how you can import modules.
If it is a custom module that you have built, then it needs to be in your src folder, and you import it using a path relative to your current file's path. For example: import MyComponent from './components/MyComponent
If it comes from a third-party dependency package (for example, reactstrap or #blueprintjs/core) which you have already added with npm or yarn, then you import it simply by specifying the package name. For example: import { Button } from 'reactstrap'
In your case, it sounds like you have a sub-folder in your node_modules folder which is for a package that you did not add with npm or yarn. While I doubt that it's recommended to use third-party packages this way, I'll assume that you have a good reason for doing so.
Without being able to see your entire setup, I would propose you try the following workaround:
In your src folder, create a symlink to the folder with your third-party package. For example (while in your project's src folder):
ln -s ../node_modules/#myScope/myreactlib myreactlib
Then, in your App.jsx file, do:
import { Link } from './myreactlib/Link'
Additional Information:
StackOverflow: The create-react-app imports restriction outside of src directory
StackOverflow: Import module from node_modules (create-react-app)
create-react-app documentation on importing a component
I am new to React and want to use the jQWidgets library:
https://www.jqwidgets.com/reactjs-components-documentation/
So, using this link I setup package.json, created the folders jqwidgets-react and jqwidgets and put required .js file as given by them
https://www.jqwidgets.com/reactjs-components-documentation/documentation/create-react-app/index.htm?search=
Now when I go to my app.js file and import any file, it shows me "module not found" like this:
import JqxDateTimeInput from './jqwidgets-react/react_jqxdatetimeinput';
Failed to compile.
Error in ./src/layouts/report_definition/step_one.js Module not found:
./jqwidgets-react/react_jqxdatetimeinput in
/home/amit/identity_pp/src/layouts/report_definition
# ./src/layouts/report_definition/step_one.js 61:30-81
First we need to install the Create React App globally, so we can have it's commands available:
npm install -g create-react-app
Create a Create React App application:
create-react-app my-app
Integrating jQWidgets React Components
In the src folder add an assets folder with the jqwidgets and the jqwidgets-react folders inside of it:
Finally inside App.js file write your widget code:
import React, { Component } from 'react';
import JqxDateTimeInput from './assets/jqwidgets-react/react_jqxdatetimeinput';
class App extends Component {
render() {
return (
<div>
<h4>My First jQWidgets React Component</h4>
<JqxDateTimeInput width={200} height={30} />
</div>
);
}
}
export default App;