-
Notifications
You must be signed in to change notification settings - Fork 28
Open
Description
Hello! I'm trying a simple configuration with ECMAScript modules. Getting the error: "Error: Must use import to load ES Module"
/Users/test/.nvm/versions/node/v20.11.0/bin/npm run generate-mongoose-types
> mtgen@1.0.0 generate-mongoose-types
> mtgen "src/models/*.ts" --debug
Generating mongoose typescript definitions...
tsreader: Registering tsconfig.json with ts-node at path: /Users/test/sources/mtgen/tsconfig.json
tsreader: Parsing tsconfig.json at path '/Users/test/sources/mtgen/tsconfig.json' to search for 'paths' field
Debug mode enabled
Generating mongoose typescript definitions... !
Error: Must use import to load ES Module: /Users/test/sources/mtgen/src/models/test.ts
require() of ES modules is not supported.
require() of /Users/test/sources/mtgen/src/models/test.ts from
/Users/test/sources/mtgen/node_modules/mongoose-tsgen/lib/parser/utils.js is an ES module file as it is a .ts
file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope
as ES modules.
Instead change the requiring code to use import(), or remove "type": "module" from
/Users/test/sources/mtgen/package.json.
Code: ERR_REQUIRE_ESM
Process finished with exit code 1
tsconfig.json: I took one suggested from tsconfig https://github.com/tsconfig/bases/blob/main/bases/node20.json
{
"compilerOptions": {
"lib": ["es2023"],
"module": "nodenext",
"target": "es2022",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"moduleResolution": "node16"
},
"exclude": ["node_modules"]
}package.json: minimal, with "type": module
{
"name": "mtgen",
"version": "1.0.0",
"description": "",
"main": "src/index.js",
"scripts": {
"build": "tsc",
"run": "node ./src/index.js",
"generate-mongoose-types": "mtgen \"src/models/*.ts\" --debug"
},
"type": "module",
"devDependencies": {
"mongoose-tsgen": "^9.5.2",
"typescript": "^5.5.3"
},
"private": true,
"dependencies": {
"mongoose": "^8.14.1"
}
}src/models/test.ts file: simple model
import mongoose, { Schema } from 'mongoose';
const schema = new Schema(
{
_id: Schema.Types.ObjectId,
name: {
type: String,
required: true,
},
},
);
export const Test = mongoose.model('model', schema);Changing the type to "common.js" in the package.json and updating imports solves the problem.
So far, I wanna make it work with modules
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels