Maximoose
Maximoose

Reputation: 151

TypeScript/Knex: Cannot use import statement outside a module

I'm a newbie to TypeScript and currently using Knex to build a template table in our PostgreSQL database. On this file and others and am continually running in to this same issue with TypeScript, code is as follows:

import * as Knex from 'knex';


exports.up = async (knex: Knex): Promise<void> => {
    await knex.schema.raw(`
    CREATE TABLE test (
        test TEXT NOT NULL,
        tes2  INTEGER NOT NULL,
        PRIMARY KEY (key, website)
    ) 
`);
}


exports.down = async (knex: Knex): Promise<void> => {
    await knex.schema.dropTable('test');
}

and I get this error:

import * as Knex from 'knex';
^^^^^^

SyntaxError: Cannot use import statement outside a module

I have also tried these varients:

   import * as Knex = require('knex');
   import { * as Knex } from 'knex';
   const * = require('knex');
   const { * as Knex } = require('knex');

But I cannot find any solution online which seems to solve the problem.

Any help on this would be awesome.

Upvotes: 14

Views: 10966

Answers (5)

superjos
superjos

Reputation: 12705

For those using tsx, this comment from a Knex GitHub issue worked great. It relies on knex JS CLI file:

The following is copied from original, credits to author

For those using tsx runtime i used

scripts: {
    ...
    "knex": "tsx ./node_modules/knex/bin/cli.js"
}

and used it like

npm run knex -- migrate:latest

Upvotes: 0

Abdelmomen
Abdelmomen

Reputation: 39

  1. At first make sure you've instantiate a typescript project so you may need to add typescript to your dependencies

    yarn add -D typescript

  2. Make sure to also have a tsconfig.json file

  3. Add a knexfile.ts to your project root dir with this command

    knex init -x ts

Upvotes: 0

Tried alot of things and nothing helped, trying to change the type to module only made things worse. After all what was missing was ts-node:

yarn add ts-node -D

And you're done =]

Upvotes: 15

Castrohenge
Castrohenge

Reputation: 8993

This error can occur if you setup knex without specifying you'll be using typescript.

Make sure you use the following command to initialize knex, if you're creating typescript migrations:

knex init -x ts

Upvotes: 3

T.J. Crowder
T.J. Crowder

Reputation: 1074495

You're mixing two different kinds of modules. import/export is JavaScript standard module syntax ("ESM" for "ECMAScript Module"), but assigning to properties on exports is CommonJS module syntax (aka "CJS"). You need to use one or the other, and if you're using ESM, you need to tell Node.js that by using "type": "module" in package.json or using the .mjs file extension — or, since you're using TypeScript, you might want "module": "ES2020" or similar (more here).

The error tells us that Node.js and/or TypeScript think you're using CJS. If you want to keep doing that, it would probably be:

const Knex = require("knex");

...but see the documentation, as they show calling that export as a function. (Perhaps you're doing that later.)

Upvotes: 2

Related Questions