exports is not defined in ES module scope
Asked Answered
B

2

20

I created nest js project as following.

nest new project-name

and imported following from nuxt3 which is of type module of node js with mjs file extension (type definition of import doesn't require to write mjs).

import { ViteBuildContext, ViteOptions, bundle } from '@nuxt/vite-builder-edge';

It gives me below error.

Uncaught Error Error [ERR_REQUIRE_ESM]: require() of ES Module c:\project\node_modules\@nuxt\vite-builder-edge\dist\index.mjs not supported. Instead change the require of c:\project\node_modules\@nuxt\vite-builder-edge\dist\index.mjs to a dynamic import() which is available in all CommonJS modules.

So I tried adding "type": "module" in package.json so now I am getting below error.

Uncaught ReferenceError ReferenceError: exports is not defined in ES module scope This file is being treated as an ES module because it has a '.js' file extension and 'c:\project\package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension. at <anonymous> (c:\project\dist\main.js:2:23)

How do I fix this? Error is related to typescript (or say compiled javascript) is unable to import mjs exports. What is solution to this?

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false
  }
}
Broz answered 15/3, 2022 at 7:48 Comment(3)
ES modules use export, not exports.Toga
That is not in my hand. It's nuxt module. Third party package :(Broz
ok. with "type": "module" my main.js is having first line as below. That is causing error. So how to avoid that. Object.defineProperty(exports, "__esModule", { value: true })Broz
B
14

Updating tsconfig "module" and "target" to "ES6" (module is past, ES is future. Please use ES so that they can deprecate module) solved the issue.

But after that, imports stopped working without extension.

So I had to add ".js" in import even though they were ".ts" files.

e.g. I had to import app.module.ts file as following (note extension).

import { AppModule } from './app.module.js';

Broz answered 15/3, 2022 at 19:26 Comment(3)
Quick note is that .js extension is because the TS team is outspoken for not wanting to rewrite paths (see Ryan's comment here). That does not take away from the fact that the answer is, indeed, correct - if ESM is used in tests, the target should be set accordingly.Padilla
I have mts files that get converted to mjs. I import from these mjs and still get the error.Instinctive
I do not use type:module in package.json - default is commonjs and now the imports work as described by Anonymous Creator. I import from mjs files and not js.Instinctive
J
-1

From the comments to your question it seems you are using Babel too. Both "module" type and Babel won't work together.

Further as @Teemu mentioned you need to use export default {} syntax instead of module.exports for a ES6 style export.

Jeanejeanelle answered 15/3, 2022 at 10:35 Comment(3)
I am not using "module.exports" nor babel (don't know if nestjs internally uses babel though)Broz
@AnonymousCreator Can you share your .tsconfig file in the question?Jeanejeanelle
Updated question and also answer.Broz

© 2022 - 2024 — McMap. All rights reserved.