[英]error TS2307: Cannot find module or its corresponding type declarations. on Github Actions
[英]error TS2307: Cannot find module 'path' or its corresponding type declarations. when trying to migrate in heroku app with Knex
我想迁移一个 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'),
~~~~~~~~~
贝娄是我的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,
};
我该如何解决这样的问题? 我正在使用以下依赖项:
开发依赖 | 依赖关系 |
---|---|
"@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/节点”:“^17.0.24” | “快递”:“^4.17.1” |
“knex 类型”:“^0.3.2” | “jsonwebtoken”:“^8.5.1” |
“ts-node-dev”:“^1.0.0-pre.56” | “knex”:“^1.0.7” |
“打字稿”:“^4.6.3” | “ts节点”:“^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"
}
}
这里的直接问题是您尝试在运行时使用 TypeScript,但您已将@types/node
定义为开发依赖项。 默认情况下, Heroku 在构建项目后会从您的项目中devDependencies
。
一种选择是跳过修剪步骤,这将使您的devDependencies
,但这可能不是正确的选择。 通常,您会希望剥离这些依赖项。 除其他原因外,这会减少应用程序块的大小。
另一种选择是将@types/node
从devDependencies
到dependencies
。 但我认为这也不是正确的做法。 我的直觉说你根本不应该在生产中使用 TypeScript。
由于您的build
脚本只是运行tsc
,我想知道它是否已经将您的knexfile.ts
编译为knexfile.js
。 如果是这样,您可以尝试直接运行 JavaScript 文件:
knex --knexfile knexfile.js migrate:latest
由于这是 Knex 查找的默认文件,因此您实际上可以完全跳过该参数:
knex migrate:latest
这应该会让你的迁移继续进行,但你会遇到另一个问题:Heroku 的临时文件系统使 SQLite 成为一个糟糕的数据库选择。
您对数据库所做的任何更改,包括迁移引入的模式更改,都会在您的 dyno 重新启动时丢失。 这种情况经常发生(至少每天一次)。
要解决该问题,您需要从 SQLite 切换到客户端-服务器数据库。 Heroku 自己的Postgres 服务是一个合理的起点,但如果您愿意,还有其他数据库插件。 您还可以在云的其他地方托管您自己的数据库,例如在 Microsoft Azure 或 AWS 上。
我在另一个模块上遇到了同样的问题,我试图在文件中导入 @mui/x-data-grid,但得到了同样的错误。 正如上面@Chris 所提到的,我发现该库位于依赖项对象中而不是在 devDependencies 中,因此我只需将其删除并在 devDependencies 上再次安装它,现在错误消息就消失了。 似乎 @types/node 必须与您尝试使用的模块位于同一依赖对象(无论是否开发)中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.