简体   繁体   English

TypeScript/Knex:不能在模块外使用 import 语句

[英]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.我是 TypeScript 的新手,目前使用 Knex 在我们的 PostgreSQL 数据库中构建模板表。 On this file and others and am continually running in to this same issue with TypeScript, code is as follows:在这个文件和其他文件上,我不断地遇到与 TypeScript 相同的问题,代码如下:

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.对此的任何帮助都会很棒。

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"). import / exportJavaScript 标准模块语法(“ECMAScript 模块”的“ESM”),但在exports上分配属性是CommonJS 模块语法(又名“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 ).您需要使用其中一个,如果您使用的是 ESM,则需要告诉.mjspackage.json中使用"type": "module"使用 TypeScript,您可能需要"module": "ES2020"或类似的(更多here )。

The error tells us that Node.js and/or TypeScript think you're using CJS.该错误告诉我们 Node.js 和/或 TypeScript 认为您正在使用 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. ...但请参阅文档,因为他们显示将该导出称为 function。 (Perhaps you're doing that later.) (也许你稍后会这样做。)

This error can occur if you setup knex without specifying you'll be using typescript.如果您设置 knex而不指定您将使用 typescript,则可能会发生此错误。

Make sure you use the following command to initialize knex, if you're creating typescript migrations:如果要创建 typescript 迁移,请确保使用以下命令初始化 knex:

knex init -x ts

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:毕竟缺少的是 ts-node:

yarn add ts-node -D纱线添加 ts-node -D

And you're done =]你完成了=]

  1. At first make sure you've instantiate a typescript project so you may need to add typescript to your dependencies首先确保您已实例化 typescript 项目,因此您可能需要将 typescript 添加到您的依赖项中

    yarn add -D typescript纱线添加-D typescript

  2. Make sure to also have a tsconfig.json file确保还有一个 tsconfig.json 文件

  3. Add a knexfile.ts to your project root dir with this command使用此命令将 knexfile.ts 添加到项目根目录

    knex init -x ts knex 初始化 -x ts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在后端使用 typescript 的模块外部使用导入语句 - Cannot use import statement outside a module using typescript on backend Typescript 与 mocha - 无法在模块外使用导入语句 - Typescript with mocha - Cannot use import statement outside a module Typescript &amp; TypeORM:不能在模块外使用导入语句 - Typescript & TypeORM: Cannot use import statement outside a module 不能在模块、Typescript、WebdriverIO 之外使用导入语句 - Cannot Use Import Statement Outside Module, Typescript, WebdriverIO Mocha + TypeScript:不能在模块外使用导入语句 - Mocha + TypeScript: Cannot use import statement outside a module Jest 无法使用 Typescript 导入(语法错误:无法在模块外使用导入语句) - Jest fails to import with Typescript (SyntaxError: Cannot use import statement outside a module) Javascript - 不能在模块外使用导入语句 - Javascript - Cannot use import statement outside a module firebase中的模块外不能使用import语句 - Cannot use import statement outside the module in firebase 不能在模块外使用 import 语句 - Cannot use import statement outside a module SyntaxError: 不能在模块外使用 import 语句 - SyntaxError: Cannot use import statement outside a module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM