![](/img/trans.png)
[英]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.