错误TS2307:找不到模块' path'或其相应的类型声明。试图用KNEX在Heroku应用中迁移时

发布于 2025-01-21 18:01:56 字数 4746 浏览 1 评论 0 原文

我想迁移一个sqlite数据库:

knex --knexfile knexfile.ts migrate:latest

但是,这给出了以下打字稿错误:

⨯ Unable to compile TypeScript:
knexfile.ts:1:18 - error TS2307: Cannot find module 'path' or its corresponding type declarations.

1 import path from 'path';
                   ~~~~~~
knexfile.ts:4:1 - error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.

4 module.exports = {
  ~~~~~~
knexfile.ts:7:28 - error TS2304: Cannot find name '__dirname'.

7     filename: path.resolve(__dirname, 'src', 'database', 'database.sqlite'),
                             ~~~~~~~~~
knexfile.ts:10:29 - error TS2304: Cannot find name '__dirname'.

10     directory: path.resolve(__dirname, 'src', 'database', 'migrations'),
                               ~~~~~~~~~
knexfile.ts:13:29 - error TS2304: Cannot find name '__dirname'.

13     directory: path.resolve(__dirname, 'src', 'database', 'seeds'),
                               ~~~~~~~~~

TSError: ⨯ Unable to compile TypeScript:
knexfile.ts:1:18 - error TS2307: Cannot find module 'path' or its corresponding type declarations.

1 import path from 'path';
                   ~~~~~~
knexfile.ts:4:1 - error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.

4 module.exports = {
  ~~~~~~
knexfile.ts:7:28 - error TS2304: Cannot find name '__dirname'.

7     filename: path.resolve(__dirname, 'src', 'database', 'database.sqlite'),
                             ~~~~~~~~~
knexfile.ts:10:29 - error TS2304: Cannot find name '__dirname'.

10     directory: path.resolve(__dirname, 'src', 'database', 'migrations'),
                               ~~~~~~~~~
knexfile.ts:13:29 - error TS2304: Cannot find name '__dirname'.

13     directory: path.resolve(__dirname, 'src', 'database', 'seeds'),
                               ~~~~~~~~~

bellow是我的 knexfile

import path from 'path';
import 'dotenv/config';

module.exports = {
  client: 'sqlite3',
  connection: {
    filename: path.resolve(__dirname, 'src', 'database', 'database.sqlite'),
  },
  migrations: {
    directory: path.resolve(__dirname, 'src', 'database', 'migrations'),
  },
  seeds: {
    directory: path.resolve(__dirname, 'src', 'database', 'seeds'),
  },
  useNullAsDefault: true,
};

如何解决此类问题? 我正在使用以下依赖项:

devDepentencies 依赖项
“@type/bcryptjs”:“^2.4.2”, “@vscode/sqlite 3”:“^5.0.8”
“@types/cors/cors/cors”:^2.8.7“,”, “ bcryptjs”:“^2.4.3”
“@types/express”:“^4.17.7”, “ cors”:“^2.8.5”
“@type/jsonwebtoken”:“^8.5.5.8” “ dotenv” :“^8.2.0”
“@type/node”:“^17.0.24” “ express”:“^4.17.1”
“ knex-types”:“^0.3.2” “ jsonwebtoken”:“^8.5。 1“”
“ ts-node-dev”:“^1.0.0-pre.56” “ knex”:“^1.0.7”
“ typescript”:“^4.6.3” “ ts-node”:“^8.10。 2”
“ sqlite3”:“^5.0.3”

编辑 package.json 文件

{
  "name": "imonitor-server",
  "version": "1.0.0",
  "description": "",
  "main": "src/server.ts",
  "scripts": {
    "start": "node build/src/server.js",
    "postinstall": "tsc",
    "dev": "tsnd --transpile-only --ignore-watch node_modules --respawn src/server.ts",
    "knex:migrate": "knex --knexfile knexfile.ts migrate:latest",
    "knex:seed": "knex --knexfile knexfile.ts seed:run",
    "knex:rollback": "knex --knexfile knexfile.ts migrate:rollback",
    "build": "tsc"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/lucasbbs/imonitor-backend.git"
  },
  "keywords": [],
  "engines": {
    "node": "16.14.0",
    "npm": "8.3.1"
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/lucasbbs/imonitor-backend/issues"
  },
  "homepage": "https://github.com/lucasbbs/imonitor-backend#readme",
  "devDependencies": {
    "@types/bcryptjs": "^2.4.2",
    "@types/cors": "^2.8.7",
    "@types/express": "^4.17.7",
    "@types/jsonwebtoken": "^8.5.8",
    "@types/node": "^17.0.24",
    "knex-types": "^0.3.2",
    "ts-node-dev": "^1.0.0-pre.56",
    "typescript": "^4.6.3"
  },
  "dependencies": {
    "@vscode/sqlite3": "^5.0.8",
    "bcryptjs": "^2.4.3",
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "jsonwebtoken": "^8.5.1",
    "knex": "^1.0.7",
    "pg": "^8.3.0",
    "sqlite3": "^5.0.3",
    "ts-node": "^8.10.2"
  }
}

I want to migrate a SQlite database:

knex --knexfile knexfile.ts migrate:latest

However this gives the following typescript error:

⨯ Unable to compile TypeScript:
knexfile.ts:1:18 - error TS2307: Cannot find module 'path' or its corresponding type declarations.

1 import path from 'path';
                   ~~~~~~
knexfile.ts:4:1 - error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.

4 module.exports = {
  ~~~~~~
knexfile.ts:7:28 - error TS2304: Cannot find name '__dirname'.

7     filename: path.resolve(__dirname, 'src', 'database', 'database.sqlite'),
                             ~~~~~~~~~
knexfile.ts:10:29 - error TS2304: Cannot find name '__dirname'.

10     directory: path.resolve(__dirname, 'src', 'database', 'migrations'),
                               ~~~~~~~~~
knexfile.ts:13:29 - error TS2304: Cannot find name '__dirname'.

13     directory: path.resolve(__dirname, 'src', 'database', 'seeds'),
                               ~~~~~~~~~

TSError: ⨯ Unable to compile TypeScript:
knexfile.ts:1:18 - error TS2307: Cannot find module 'path' or its corresponding type declarations.

1 import path from 'path';
                   ~~~~~~
knexfile.ts:4:1 - error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.

4 module.exports = {
  ~~~~~~
knexfile.ts:7:28 - error TS2304: Cannot find name '__dirname'.

7     filename: path.resolve(__dirname, 'src', 'database', 'database.sqlite'),
                             ~~~~~~~~~
knexfile.ts:10:29 - error TS2304: Cannot find name '__dirname'.

10     directory: path.resolve(__dirname, 'src', 'database', 'migrations'),
                               ~~~~~~~~~
knexfile.ts:13:29 - error TS2304: Cannot find name '__dirname'.

13     directory: path.resolve(__dirname, 'src', 'database', 'seeds'),
                               ~~~~~~~~~

Bellow is my knexfile:

import path from 'path';
import 'dotenv/config';

module.exports = {
  client: 'sqlite3',
  connection: {
    filename: path.resolve(__dirname, 'src', 'database', 'database.sqlite'),
  },
  migrations: {
    directory: path.resolve(__dirname, 'src', 'database', 'migrations'),
  },
  seeds: {
    directory: path.resolve(__dirname, 'src', 'database', 'seeds'),
  },
  useNullAsDefault: true,
};

How can I address such issue?
I am using the following dependencies:

devDependencies dependencies
"@types/bcryptjs": "^2.4.2", "@vscode/sqlite3": "^5.0.8"
"@types/cors": "^2.8.7", "bcryptjs": "^2.4.3"
"@types/express": "^4.17.7", "cors": "^2.8.5"
"@types/jsonwebtoken": "^8.5.8" "dotenv": "^8.2.0"
"@types/node": "^17.0.24" "express": "^4.17.1"
"knex-types": "^0.3.2" "jsonwebtoken": "^8.5.1"
"ts-node-dev": "^1.0.0-pre.56" "knex": "^1.0.7"
"typescript": "^4.6.3" "ts-node": "^8.10.2"
"sqlite3": "^5.0.3"

Edit package.json file

{
  "name": "imonitor-server",
  "version": "1.0.0",
  "description": "",
  "main": "src/server.ts",
  "scripts": {
    "start": "node build/src/server.js",
    "postinstall": "tsc",
    "dev": "tsnd --transpile-only --ignore-watch node_modules --respawn src/server.ts",
    "knex:migrate": "knex --knexfile knexfile.ts migrate:latest",
    "knex:seed": "knex --knexfile knexfile.ts seed:run",
    "knex:rollback": "knex --knexfile knexfile.ts migrate:rollback",
    "build": "tsc"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/lucasbbs/imonitor-backend.git"
  },
  "keywords": [],
  "engines": {
    "node": "16.14.0",
    "npm": "8.3.1"
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/lucasbbs/imonitor-backend/issues"
  },
  "homepage": "https://github.com/lucasbbs/imonitor-backend#readme",
  "devDependencies": {
    "@types/bcryptjs": "^2.4.2",
    "@types/cors": "^2.8.7",
    "@types/express": "^4.17.7",
    "@types/jsonwebtoken": "^8.5.8",
    "@types/node": "^17.0.24",
    "knex-types": "^0.3.2",
    "ts-node-dev": "^1.0.0-pre.56",
    "typescript": "^4.6.3"
  },
  "dependencies": {
    "@vscode/sqlite3": "^5.0.8",
    "bcryptjs": "^2.4.3",
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "jsonwebtoken": "^8.5.1",
    "knex": "^1.0.7",
    "pg": "^8.3.0",
    "sqlite3": "^5.0.3",
    "ts-node": "^8.10.2"
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请叫√我孤独 2025-01-28 18:01:56

Typescript

这里的直接问题是,您正在尝试在运行时使用Typescript,但是您已将@type/node 定义为开发依赖关系。默认情况下, heroku strips devDepplencies

一种选择是跳过修剪步骤代码> DevDepentencies 到位,但这可能不是正确的选择。通常,您会希望这些依赖项被剥离。除其他原因外,这减少了您的应用程序slug的大小。

另一个选项是将 devDependencies 移动到依赖项。但是我认为这也不是正确的举动。我的直觉说,您根本不应该在生产中使用打字稿。

由于您的构建脚本只能运行 TSC ,我想知道它是否已经将您的 knexfile.ts 纳入 knexfile.js 。如果确实如此,您可以尝试直接运行JavaScript文件:

knex --knexfile knexfile.js migrate:latest

由于这是KNEX寻找的默认文件,因此您实际上可以完全跳过参数:

knex migrate:latest

SQLITE

这应该使您的迁移趋于迁移,但是您将遇到另一个问题: Heroku's ersphemeral filesystemystem 使SQLITE成为贫穷的数据库选择。

您对数据库的任何更改,包括迁移引入的架构更改,每当您的Dyno重新启动时,都会丢失。此经常发生(至少每天一次)。

为了解决该问题,您需要从SQLite切换到客户端服务器数据库。 Heroku自己的 Postgres Service 是一个合理的起点,但是有其他数据库addons 如果您喜欢。您还可以在云上的其他地方托管自己的数据库,例如Microsoft Azure或AWS上。

TypeScript

The immediate issue here is that you're trying use TypeScript at runtime, but you've defined @types/node as a development dependency. By default, Heroku strips devDependencies from your project after building it.

One option is to skip the pruning step, which will leave your devDependencies in place, but that likely isn't the right choice. Normally you'll want these dependencies to be stripped. Among other reasons, this reduces the size of your application slug.

Another option is to move @types/node from devDependencies to dependencies. But I don't think that's the right move here, either. My gut says you shouldn't be using TypeScript at all in production.

Since your build script simply runs tsc, I wonder if it has already compiled your knexfile.ts to knexfile.js. If it did, you can try running the JavaScript file directly:

knex --knexfile knexfile.js migrate:latest

Since that's the default file that Knex looks for, you can actually skip the argument entirely:

knex migrate:latest

SQLite

This should get your migrations going, but you're going to run into another issue: Heroku's ephemeral filesystem makes SQLite a poor database choice.

Any changes you make to the database, including schema changes introduced by your migrations, will be lost whenever your dyno restarts. This happens frequently (at least once per day).

To resolve that issue, you'll want to switch from SQLite to a client-server database. Heroku's own Postgres service is a reasonable starting point, but there are other database addons if you prefer. You can also host your own database elsewhere on the cloud, e.g. on Microsoft Azure or AWS.

一梦等七年七年为一梦 2025-01-28 18:01:56

我在另一个模块中遇到了同样的问题,我试图在文件中导入 @mui/x-data-rid,但有同样的错误。我在上面提到的那样发现,库在依赖关系中的依赖项对象中,因此我只需要将其删除并再次安装在DevDepentencies上,现在错误消息已经消失了。似乎 @types/node必须与您要使用的模块相同的依赖项对象(是否开发)。

I had the same issue with another module, I was trying to import @mui/x-data-grid in a file, but got the same error. I found as mentioned by @Chris above that the library was in the dependencies object not in the devDependencies, so I just have to remove it and install it again on the devDependencies, now the error message is gone. It seems that @types/node must be in the same dependencies object (dev or not) as the module you are trying to use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文