0

I have this node js application:

require('babel-register');
const router = require("./src/web/router");

and I am getting this error:

enter image description here

how can I import react component in node js?

13
  • Du you have a .babelrc file with the babel-preset-react added? If yes, please include it in your question. If not – that's the cause of the error. Commented Jan 2, 2019 at 8:35
  • It seems that you have imported the router properly but you are not parsing React's JSX Commented Jan 2, 2019 at 8:35
  • @NedkoDimitrov, this is absolutely the problem my friend, but how can I solve it? Commented Jan 2, 2019 at 8:37
  • @PatrickHund, this is my .babelrc: { presets: ["react"] } Commented Jan 2, 2019 at 8:40
  • the fastest way is to run npx create-react-app enter the newly reacted directory and run npm start it will start example React application. Than you can move your existing code Commented Jan 2, 2019 at 8:41

2 Answers 2

2

possible using esm node pacakge, esm is a fast, production ready, zero-dependency ES module loader for Node 6+.

esm on github

Sign up to request clarification or add additional context in comments.

Comments

0

If you are not interested in server side rendering your react component I made a small library that makes it easy to render react components from a node application https://github.com/tswayne/react-helper#getting-started. If you are trying to server side render, or import the react component for any other reason, you will need to compile your server side code with babel or use something like babel-node to compile at runtime.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.