首页 > 解决方案 > submit data to node.js server without form. no require no import , they don't work

问题描述

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

标签: javascriptnode.jsaxiosbrowserifyrequire

解决方案


您的导入语法似乎是错误的,也许这就是原因。导入时不需要括号:

import axios from 'axios'

我还猜测您是在前端而不是在 node.js 中运行此代码。这可能就是您收到“未定义要求”错误的原因。(见:https ://stackoverflow.com/a/9901097/7228779 )

看起来您broweserify在安装命令中拼错了 browserify (not ),所以这也可能是一个问题。


推荐阅读