'Error on Netlify Build: Import assignment cannot be used when targeting ECMAScript modules

This is the what the log files are showing on netlify

────────────────────────────────────────────────────────────────
5:26:11 PM:   1. @netlify/plugin-nextjs (onPreBuild event)                  
5:26:11 PM: ────────────────────────────────────────────────────────────────
5:26:11 PM: ​
5:26:11 PM: No Next.js cache to restore.
5:26:11 PM: Netlify configuration property "build.environment.NEXT_PRIVATE_TARGET" value changed.
5:26:11 PM: ​
5:26:11 PM: (@netlify/plugin-nextjs onPreBuild completed in 28ms)
5:26:11 PM: ​
5:26:11 PM: ────────────────────────────────────────────────────────────────
5:26:11 PM:   2. Build command from Netlify app                             
5:26:11 PM: ────────────────────────────────────────────────────────────────
5:26:11 PM: ​
5:26:11 PM: $ yarn build
5:26:11 PM: yarn run v1.22.17
5:26:12 PM: $ next build
5:26:12 PM: warn  - No build cache found. Please configure build caching for faster rebuilds. Read more: https://nextjs.org/docs/messages/no-cache
5:26:12 PM: info  - Checking validity of types...
5:26:17 PM: Failed to compile.
5:26:17 PM: 
5:26:17 PM: ./node_modules/mongoose/types/cursor.ts:1:1
5:26:17 PM: Type error: Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead.
5:26:17 PM: > 1 | import stream = require('stream');
5:26:17 PM:     | ^
5:26:17 PM:   2 | 
5:26:17 PM:   3 | declare module 'mongoose' {
5:26:17 PM:   4 |   type CursorFlag = 'tailable' | 'oplogReplay' | 'noCursorTimeout' | 'awaitData' | 'partial';
5:26:17 PM: error Command failed with exit code 1.
5:26:17 PM: info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
5:26:17 PM: ​
5:26:17 PM: ────────────────────────────────────────────────────────────────
5:26:17 PM:   "build.command" failed                                        
5:26:17 PM: ────────────────────────────────────────────────────────────────
5:26:17 PM: ​

Locally the build is successful. I added environment variables on netlify to ensure versions of node and yarn are the same as those local and it stil fails.

I tried the solution of replacing module on tsconfi.json from

"module": "esnext"

to

"module": "commonjs"

The only time the build on netlify works is when typescript errors are set to be ignored.

Any ideas on how to build successfully if i don't want to ignore typescript errors?



Solution 1:[1]

I'd try using import or require syntax here

import stream from "steam"
// Or
const stream = require ('stream')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 aisflat439