SyntaxError: Cannot use import statement outside a module
Verify that you have the latest version of Node.js installed (or, at least 13.2.0+). Then do one of the following, as described in the documentation:
Option 1
In the nearest parent package.json
file, add the top-level "type"
field with a value of "module"
. This will ensure that all .js
and .mjs
files are interpreted as ES modules. You can interpret individual files as CommonJS by using the .cjs
extension.
// package.json
{
"type": "module"
}
Option 2
Explicitly name files with the .mjs
extension. All other files, such as .js
will be interpreted as CommonJS, which is the default if type
is not defined in package.json
.
MUI5 not working with jest - SyntaxError: Cannot use import statement outside a module
First you have two exports in your Select.tsx
file. Just use the default export, so change line 20 to:
const Select: React.FC<ISelect> = ({ label, id, children, options, ...props }) => {
Then in your Select.test.tsx
file change to:
import Select from './Select'
Finally to fix the import issue change your code in Select.tsx
to:
import { styled } from '@mui/material'
This is a fairly common issue as can be seen here.
Typescript SyntaxError: Cannot use import statement outside a module (side file containing functions)
Fixed by changing:
import {util} from "./utils/util"
To:
const util = require('./utils/util')
// or
const { sleep, random, eatAny, clickItem, lookAtEntity} = require('./utils/util')
Afterall I don't know why compiling with tsc
had been working well while webstorm was throwing out error. I just changed the import
ES6 syntax to require()
CommonJS.
How to fix 'SyntaxError: Cannot use import statement outside a module'?
Move ormconfig.ts
to your src
folder (change imports if necessary), and add the include
after compilerOptions
to include everything in the src
. It's also a good idea to use exclude
node_modules and tests:
{
"compilerOptions": { ... },
"include": ["src/**/*.ts"],
"exclude": ["node_modules", "tests"]
}
It might be because TypeScript is including ormconfig.ts in the compilation output: as you can see in the build folder there is an index.js and ormconfig.js AND duplicate src
folder.
Related Topics
Angular 4:How to Get Index of Selected Value in a Datalist
How to Display Base64 Encoded Pdf
Next.Js Implement External ".Js" With <Script>
404 Error When Trying to Register Serviceworker
How to Sort Json Array Elements in Descending Order
How to Use a Function from Another File in React
Prevent Sweetalert to Be Closed on Clicking Outside the Popup Window
How to Check If Prevprops and Nextprops Are the Same in React
How to Stop Page Getting Refresh on Validating Form
How to Disable a Message When the Password Value Is Empty
How to Load Script in React Component
Finding Sequential Values in Js Array
Passing Variable from Button to Modal
Ajax - How to Use a Returned Array in a Success Function
Vuetify: How to Preselect Active Tab
How to Parse Excel (Xls) File in Javascript/Html5
"Cannot Read Property of Undefined" When Using Chrome.Tabs or Other Chrome API in Content Script