Reputation: 5989
In my node.js (express.js) project, I try to wrap axios to create a HttpClient like this:
import axios from 'axios';
const httpClient = axios.create({ baseURL: 'http://locahost:3003' });
...
export const httpClient = {
...
};
When I run it, I get error SyntaxError: Cannot use import statement outside a module
which complains the line import axios from 'axios'
. How can I get rid of this error? (I come from React-native world, I used the same thing in React-Native, it is fine)
Upvotes: 0
Views: 2641
Reputation: 859
The issue is by default express uses CJS format of importing as you might have seen const axios = require("axios")
.
What you are trying to do is an ESM format of import which is not understood by the JS as natively. To work import statements you can either add the following property to your package.json file - "type": "module"
or you can use ".mjs" extension instead of default ".js" extension.
Hope this link helps further.
Upvotes: 1