0

I am trying to add Objection.js to my project (using ES6 "type": "module"), and getting this error which points to ./models/user.js:

import { Model } from "objection";
         ^^^^^
SyntaxError: The requested module 'objection' does not provide an export named 'Model'

Using the following code:

./methods.js

import User from "./models/user.js";

async function getInfo(idnum) {
  const someUser = await User.query().findById(idnum);
  return someUser;
}

./models/user.js

import db from "../connection.js";
import { Model } from "objection";

Model.knex(db);

class User extends Model {
  static get tableName() {
    return "users";
  }
}

export default User;

./connection.js

const environment = process.env.NODE_ENV || "development";

import knexfile from "../knexfile.js";
const connection = knexfile[environment];

import knex from "knex";
const db = knex(connection);

export default db;

UPDATE The creator of Objection.js said import { Model } from "objection" should work.

What am I doing wrong?

Raj
  • 1,555
  • 18
  • 32
  • can you try objection module with require keyword. Objection.js is not a Common JS module I think – Surya Apr 27 '20 at 12:25
  • @Owner the rest of the project is ES6 modules so it would break. – Raj Apr 27 '20 at 12:28
  • Mostly, using import statements you can use experimental modules. I don't think Objection module is experimental. If you are using ES6, my recommendation would be to have require statements. @Raj: check this out https://medium.com/the-node-js-collection/an-update-on-es6-modules-in-node-js-42c958b890c – Surya Apr 27 '20 at 12:31
  • Why do you think the code will break? – Surya Apr 27 '20 at 12:32
  • @Owner `ReferenceError: require is not defined` because I have `"type":"module"` in my package.json (and I can't remove it from my package.json because the rest of the project uses imports. – Raj Apr 27 '20 at 12:36
  • Its an optional parameter, "type" can have "commonjs" as well. – Surya Apr 27 '20 at 12:39
  • You can also downgrade your node version. Not every node module is supported in Node 14+ versions. Many of them are being deprecated. – Surya Apr 27 '20 at 12:40
  • @Owner if I switch to `"type":"commonjs"` then I can't use `import x from "y"`, which is what every file in my project currently uses. – Raj Apr 27 '20 at 12:41
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/212615/discussion-between-owner-and-raj). – Surya Apr 27 '20 at 12:43

2 Answers2

1

I hope that you are using a .mjs extension for the file if you are using import in a Node app.

But if you are using a .js as extension then you have to call that module using require.

const { Model } = require('objection');

This was the problem I once had... I don't know if this is the solution to your problem.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • I'm using `"type":"module"` for native ES6 support (albeit experimental): https://nodejs.org/api/esm.html#esm_package_json_type_field so no I can't use `require` afaik – Raj Apr 27 '20 at 17:42
1

The only current workaround appears to be importing the Model like this:

import objection from "objection";
const { Model } = objection;

since Objection.js does exports like this:

export default { Model }

and not like this:

export { Model }
Raj
  • 1,555
  • 18
  • 32