score:3

Accepted answer

the boilerplate (create-react-app) you used to create your project utilizes the node.js development environment, so you should stick to that development workflow - by creating node modules.

so you need to convert helloworld.js to a node module:

exports.helloworld = function() {
    console.log('hello world!');
};

then use it in other modules of your application (i.e. index.js):

import { helloworld } from './js/helloworld';

helloworld(); // or call from within a react.js component

you should read about how node.js modules work.

score:1

if you check your network tab in your debugger, it might actually load for you. but you're not seeing the effect on your screen because that's not how create-react-app works. it's not serving you a blank index page, it's set up to serve a div that it injects your react app into. you're not finding this on the internet because it's just not how this tool works. read the docs and learn how it's intended to work and you'll be very happy you did. :)

score:4

you could also make it work if you move your helloworld.js file inside of the public folder, as the server using by create-react-app serves all the assets in the public folder but does not serve src. here is the documentation

in your index.html

  <script type="text/javascript" src="js/helloworld.js"></script>

in your project put the js folder inside of public public/js/helloworld.js

the type should be text/javascript as it let your browser know how to interpret the file. the syntax error, you are getting is due to the fact that create-react-app renders the index.html if the file is not found, and then your browser sees html instead of javascript, hence the error : uncaught syntaxerror: unexpected token <

also create-react-app hides a lot of complexity for you and it is going to be difficult to understand all of what happens behind the scene. if you want to go through the complete tooling system in place you could run npm run eject (but read the docs first :) as it can't be reversed.


Related Query

More Query from same tag