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
  }
}
3
  • 1
    ES modules use export, not exports.
    – Teemu
    Commented Mar 15, 2022 at 7:52
  • 3
    That is not in my hand. It's nuxt module. Third party package :( Commented Mar 15, 2022 at 8:23
  • 1
    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 }) Commented Mar 15, 2022 at 8:30

2 Answers 2

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';

3
  • 2
    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. Commented Apr 30, 2022 at 21:18
  • I have mts files that get converted to mjs. I import from these mjs and still get the error.
    – Timo
    Commented Jan 21 at 18:08
  • 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.
    – Timo
    Commented Jan 22 at 20:29
-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.

3
  • I am not using "module.exports" nor babel (don't know if nestjs internally uses babel though) Commented Mar 15, 2022 at 10:38
  • @AnonymousCreator Can you share your .tsconfig file in the question? Commented Mar 15, 2022 at 17:20
  • Updated question and also answer. Commented Mar 15, 2022 at 19:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.