最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - SyntaxError: Unexpected identifier with axios - Stack Overflow

programmeradmin1浏览0评论

I am trying to use axios as following:

import axios from 'axios';

axios.post("http://localhost:3000/test", {"prop1": "value"}, {headers: {'X-Custom-Header': 'foobar'}})

then the compiler complains:

/home/developer/Desktop/reason/interoperate/src/Ax.js:1
(function (exports, require, module, __filename, __dirname) { import axios from 'axios';
                                                                     ^^^^^

SyntaxError: Unexpected identifier
    at new Script (vm.js:74:7)
    at createScript (vm.js:246:10)
    at Object.runInThisContext (vm.js:298:10)
    at Module._compile (internal/modules/cjs/loader.js:670:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
    at Module.load (internal/modules/cjs/loader.js:612:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
    at Function.Module._load (internal/modules/cjs/loader.js:543:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
    at startup (internal/bootstrap/node.js:238:19)
developer@monad:~/Desktop/reason/interoperate/src$ node Ax.js
/home/developer/Desktop/reason/interoperate/src/Ax.js:1
(function (exports, require, module, __filename, __dirname) { import axios from 'axios';  

Do I import wrong path?

I am trying to use axios as following:

import axios from 'axios';

axios.post("http://localhost:3000/test", {"prop1": "value"}, {headers: {'X-Custom-Header': 'foobar'}})

then the compiler complains:

/home/developer/Desktop/reason/interoperate/src/Ax.js:1
(function (exports, require, module, __filename, __dirname) { import axios from 'axios';
                                                                     ^^^^^

SyntaxError: Unexpected identifier
    at new Script (vm.js:74:7)
    at createScript (vm.js:246:10)
    at Object.runInThisContext (vm.js:298:10)
    at Module._compile (internal/modules/cjs/loader.js:670:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
    at Module.load (internal/modules/cjs/loader.js:612:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
    at Function.Module._load (internal/modules/cjs/loader.js:543:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
    at startup (internal/bootstrap/node.js:238:19)
developer@monad:~/Desktop/reason/interoperate/src$ node Ax.js
/home/developer/Desktop/reason/interoperate/src/Ax.js:1
(function (exports, require, module, __filename, __dirname) { import axios from 'axios';  

Do I import wrong path?

Share Improve this question asked Jun 10, 2018 at 16:46 softshippersoftshipper 34.1k61 gold badges226 silver badges450 bronze badges 3
  • which version of nodejs are you using? If I were you, i would try to use require syntax and not import – Jose Mato Commented Jun 10, 2018 at 16:48
  • The node version is v10.4.0. With var axios = require('axios'); it works. But I need with import. – softshipper Commented Jun 10, 2018 at 16:50
  • 1 Why do you need import? At the moment node 10.4.0 works with import as experimental feature and I guess this just include your own modules (with extension mjs) and node core modules (like fs for example). nodejs.org/api/esm.html If you need to use import then I guess you could use babel or some transpiler but nowadays require is the option to go until import start being fully functional – Jose Mato Commented Jun 10, 2018 at 16:54
Add a comment  | 

3 Answers 3

Reset to default 10

Node.js doesn't fully support ES modules yet, which means you cannot use the import keyword. You can use it now with a source code transpiler like Babel and Webpack, but that will require a build step.

Edit: To be fair, Node.js 10.4.0 has an experimental support for ES modules, which is behind a flag: --experimental-modules. This will also require using the .mjs file extension for your JS files.

The feature is currently marked as Stability: 1 - Experimental - Use of the feature is not recommended in production environments.

I was also stuck here but I found the answer here : https://github.com/nuxt/docs/issues/42

Since node.js still doesn't support import in official stable release, we should use require keyword. Something like this: const Axios=require('axios');

Of course, do remember to install axios module from npm.

You can make it work with node 10.4.0 in next way:

// Create a file named index.mjs (the extension is the key)

import axios from 'axios';

console.log(axios);

Run it as:

node --experimental-modules index.mjs

But this feature is still experimental, should be avoided in production systems.

发布评论

评论列表(0)

  1. 暂无评论