The target environment doesn't support dynamic import() syntax error

I got a “The target environment doesn’t support dynamic import() syntax so it’s not possible to use external type ‘module’ within a script” error when running npm run build.

What does this error mean? And how can I fix it? (deploy log attached below)

3:44:27 AM: NPM modules installed
3:44:27 AM: Started restoring cached go cache
3:44:27 AM: Finished restoring cached go cache
3:44:27 AM: go version go1.16.5 linux/amd64
3:44:27 AM: go version go1.16.5 linux/amd64
3:44:27 AM: Installing missing commands
3:44:27 AM: Verify run directory
3:44:29 AM: ​
3:44:29 AM: ────────────────────────────────────────────────────────────────
3:44:29 AM:   Netlify Build                                                 
3:44:29 AM: ────────────────────────────────────────────────────────────────
3:44:29 AM: ​
3:44:29 AM: ❯ Version
3:44:29 AM:   @netlify/build 26.1.3
3:44:29 AM: ​
3:44:29 AM: ❯ Flags
3:44:29 AM:   baseRelDir: true
3:44:29 AM:   buildId: 61e404cb9f4ef3bc4d4f8e3b
3:44:29 AM:   deployId: 61e404cb9f4ef3bc4d4f8e3d
3:44:29 AM: ​
3:44:29 AM: ❯ Current directory
3:44:29 AM:   /opt/build/repo
3:44:29 AM: ​
3:44:29 AM: ❯ Config file
3:44:29 AM:   No config file was defined: using default values.
3:44:29 AM: ​
3:44:29 AM: ❯ Context
3:44:29 AM:   production
3:44:29 AM: ​
3:44:29 AM: ────────────────────────────────────────────────────────────────
3:44:29 AM:   1. Build command from Netlify app                             
3:44:29 AM: ────────────────────────────────────────────────────────────────
3:44:29 AM: ​
3:44:29 AM: $ npm run build
3:44:30 AM: > problem-bank@0.1.0 build
3:44:30 AM: > react-scripts build
3:44:32 AM: Creating an optimized production build...
3:44:49 AM: Failed to compile.
3:44:49 AM: 
3:44:49 AM: The target environment doesn't support dynamic import() syntax so it's not possible to use external type 'module' within a script
3:44:49 AM: ​
3:44:49 AM: ────────────────────────────────────────────────────────────────
3:44:49 AM:   "build.command" failed                                        
3:44:49 AM: ────────────────────────────────────────────────────────────────
3:44:49 AM: ​
3:44:49 AM:   Error message
3:44:49 AM:   Command failed with exit code 1: npm run build
3:44:49 AM: ​
3:44:49 AM:   Error location
3:44:49 AM:   In Build command from Netlify app:
3:44:49 AM:   npm run build
3:44:49 AM: ​
3:44:49 AM:   Resolved config
3:44:49 AM:   build:
3:44:49 AM:     command: npm run build
3:44:49 AM:     commandOrigin: ui
3:44:49 AM:     environment:
3:44:49 AM:       - NODE_ENV
3:44:49 AM:       - NODE_VERSION
3:44:49 AM:     publish: /opt/build/repo/build
3:44:49 AM:     publishOrigin: ui
3:44:49 AM: Caching artifacts
3:44:49 AM: Started saving node modules
3:44:49 AM: Finished saving node modules
3:44:49 AM: Started saving build plugins
3:44:49 AM: Finished saving build plugins
3:44:49 AM: Started saving pip cache
3:44:49 AM: Finished saving pip cache
3:44:49 AM: Started saving emacs cask dependencies
3:44:50 AM: Finished saving emacs cask dependencies
3:44:50 AM: Started saving maven dependencies
3:44:50 AM: Finished saving maven dependencies
3:44:50 AM: Started saving boot dependencies
3:44:50 AM: Finished saving boot dependencies
3:44:50 AM: Started saving rust rustup cache
3:44:50 AM: Finished saving rust rustup cache
3:44:50 AM: Started saving go dependencies
3:44:50 AM: Finished saving go dependencies
3:44:53 AM: Build failed due to a user error: Build script returned non-zero exit code: 2
3:44:53 AM: Creating deploy upload records
3:44:53 AM: Failing build: Failed to build site
3:44:53 AM: Failed during stage 'building site': Build script returned non-zero exit code: 2
3:44:53 AM: Finished processing build request in 1m43.558264807s

I believe you should convert your dynamic imports to static like:

import foo from 'bar'

If that’s not it, your repo would help us check further.