0

I know that maybe I get some negative votes. but it is not a big problem.I have no other option. I tried too much to send data to my node.js server without using form. I used axios. but my main.js doesn't know axios. i installed it

   npm install axios

I imported it

  import axios from ('axios')

but I got the error ( unexpected Identifier)

after that I tried require

 var axios = require ('axios')

but I got the error ( require is not defined)

I installed browserify

 npm install broweserify

after that I bundled it with this code

browserify main.js -o bundle.js

but the same error still remains

require is not defined.

has somebody a solution.

APPRECIATED

mojtaba1
  • 53
  • 11
  • How do you execute this `main.js`? – shkaper Nov 13 '18 at 23:10
  • This might help https://stackoverflow.com/questions/28696511/require-is-not-defined-error-with-browserify – RedPandaz Nov 13 '18 at 23:16
  • @RedPandaz thank you. that is exactly what I did. but I don't know why it doesn't work – mojtaba1 Nov 13 '18 at 23:51
  • @shkaper , thanks. I have many functions in my main.js . these functions are being called through index.html and other pages. one page is articles.ejs that wants to send data to server, without using forms. but until now I couldn't do it – mojtaba1 Nov 13 '18 at 23:51

1 Answers1

0

Your import syntax seems to be wrong, maybe that is the reason. You don't need the parantheses in import:

import axios from 'axios'

I'm also guessing that you are running this code in front-end and not in node.js. that's probably why you get 'require is not defined' error. (see: https://stackoverflow.com/a/9901097/7228779 )

It also looks like you misspelled browserify (not broweserify) in your install command, so that might be an issue too.

selmanbey
  • 318
  • 1
  • 8
  • thanks a lot. these mistakes are my misspelling just here and not in my code. ja I am using it in front-end because as I said, I want to send data to server without using form. about browserify . I have installed it successfully and I bundeled main.js with bundle.js etc.... somebody told me that I can use fetch api. do you know about it? appreciated – mojtaba1 Nov 14 '18 at 09:22
  • "unexpected identifier" usually indicates that you have a misspelling, syntax error etc in your code. you can find it by paying attention to your error message. the message is supposed to point out where the error might be (it's not always super helpful, but it usually is). `fetch()` allows you to make a promise-based async requests with a clean syntax, so sure, you can use it to send data back and forth between your client and server. but I doubt it has anything to do with your question/problem. if you want to learn more about fetch, this is a good read: https://davidwalsh.name/fetch – selmanbey Nov 14 '18 at 13:12