在為兩臺機器更新我的節點版本/依賴項的某個地方,我注意到在另一臺機器上構建我的應用程式會輸出錯誤,而在我的主機上構建生產時不會。
如果我將受影響的 devDependencies 移動到我的依賴項陣列中,錯誤就會消失package.json,但這更像是一種解決方法,我想知道為什么我會看到不同的行為。
據我了解,npm install在生產中運行時,它不會安裝我的開發依賴項。這很好,因為我在我的開發機器上對此進行了測驗,即使沒有我的開發依賴項,該應用程式也可以正常構建。
但是,在生產中,我收到如下錯誤:
Cannot find module 'sinon' or its corresponding type declarations.
Could not find a declaration file for module 'cors'. '/home/ubuntu/brobot/source/node_modules/cors/lib/index.js' implicitly has an 'any' type. Try `npm i --save-dev @types/cors` if it exists or add a new declaration (.d.ts) file containing `declare module 'cors';
如果我將受影響的包移動到依賴項陣列而不是開發依賴項,它們就會消失,但為什么呢?為什么它可以在我的開發機器上沒有我的開發依賴項的情況下構建得很好?
這是我的配置:
package.json縮短:
{
"main": "dist/src/index.js",
"type": "module",
"scripts": {
"build": "rimraf dist && tsc",
"test": "mocha",
"dev:old": "cross-env NODE_ENV=development concurrently \"tsc -w\" \"nodemon -q -r dotenv/config --es-module-specifier-resolution=node dist/src/index.js\"",
"deploy": "pm2 deploy ecosystem.config.cjs production",
"preserve": "npm run build",
},
"dependencies": {
"@pkmn/randoms": "^0.5.11",
"@pkmn/sim": "^0.5.11",
"@twurple/api": "^5.0.18",
"@twurple/auth": "^5.0.18",
"@twurple/chat": "^5.0.18",
"@twurple/eventsub": "^5.0.18",
"@twurple/eventsub-ngrok": "^5.0.18",
"@types/cookie-session": "^2.0.44",
"@types/express": "^4.17.13",
"@types/express-ws": "^3.0.1",
"@types/mocha": "^9.1.0",
"@types/node": "^17.0.23",
"@types/passport": "^1.0.7",
"@types/request": "^2.48.8",
"axios": "^0.26.1",
"cookie-session": "^2.0.0",
"cors": "^2.8.5",
"dotenv": "^16.0.0",
"express": "^4.17.3",
"express-ws": "^5.0.2",
"helmet": "^5.0.2",
"mocha": "^9.2.2",
"moment-timezone": "^0.5.34",
"mongoose": "^6.2.8",
"passport": "^0.5.2",
"passport-oauth": "^1.0.0",
"request": "^2.88.2",
"rimraf": "^3.0.2",
"typescript": "^4.6.2",
"winston": "^3.6.0",
"ws": "^8.5.0"
},
"devDependencies": {
"@types/chai": "^4.3.0",
"@types/cors": "^2.8.12",
"@types/sinon": "^10.0.11",
"@types/ws": "^8.5.3",
"@typescript-eslint/eslint-plugin": "^5.16.0",
"@typescript-eslint/parser": "^5.16.0",
"chai": "^4.3.6",
"concurrently": "^7.0.0",
"cross-env": "^7.0.3",
"eslint": "^8.11.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-promise": "^6.0.0",
"lint-staged": "^12.3.7",
"nodemon": "^2.0.15",
"pm2": "^5.2.0",
"prettier": "^2.6.0",
"sinon": "^13.0.1",
"ts-node": "^10.7.0"
},
"lint-staged": {
"*.{js,ts}": [
"eslint --cache --fix",
"prettier --check"
]
}
}
tsconfig.json:
{
"compilerOptions": {
"target": "ES6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */
"module": "ESNext", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ /* Generates a sourcemap for each corresponding '.d.ts' file. */
"sourceMap": true, /* Generates corresponding '.map' file. */
"outDir": "dist", /* Redirect output structure to the directory. */
"strict": true, /* Enable all strict type-checking options. */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
}
PM2 ecosystem.config.cjs:
require('dotenv').config();
// Default configuration
module.exports = {
apps: [...],
deploy: {
production: {
user: process.env.AWS_USER,
host: process.env.AWS_PUBLIC_IP,
key: process.env.AWS_SSH_KEY,
ref: 'origin/main',
repo: '...',
path: process.env.AWS_EC2_PATH,
node_args: "--experimental-modules --es-module-specifier-resolution=node",
env: { NODE_ENV: 'production', },
'post-deploy':
'npm install && npm run build && pm2 startOrRestart ecosystem.config.cjs --env production'
^^^^^^^^^^^^^^^ this is where errors are thrown in production. `npm run build` works fine on development without dev dependencies installed.
}
}
};
我考慮過的事情:
- 不要在生產中轉譯。我在部署程序中克隆了我的 repo,所以除非我在我的 repo 中包含我的構建,否則我不能這樣做。
- 將 devDependencies 切換到依賴項:我真的想避免這樣做,因為它們應該是開發依賴項
我嘗試過的其他事情:
- 清除 npm 快取
- 完全洗掉我的 PM2 實體并重新配置它
- 洗掉 package-lock.json (我知道這很糟糕,但我很絕望,沒有看到沖突)
- 洗掉 --experimental-modules 并添加回“.js”擴展匯入
- 在 tsconfig.json 中排除我的 node_modules
我的開發/生產機器之間一定有一些錯誤配置。盡管它們運行的??是相同版本的 node 和 Typescript。據我所知,兩臺機器都沒有使用全域安裝的軟體包。奇怪的是,這在我的生產機器上運行得很好,盡管我可能沒有正確地使用生產標志運行它。即使我的 node_env 是“生產”,它也可能在我沒有注意到的情況下安裝了我的開發依賴項。
我在這里沒有選擇,非常感謝任何幫助。
uj5u.com熱心網友回復:
sinon 是一個開發依賴項,因此它不捆綁用于生產。如果您需要它進行生產,您可以將其添加到依賴項中,如果不需要,您可以洗掉對 sinon 的參考并重新捆綁,它應該會自行解決。
編輯1:
如果我將受影響的 devDependencies 移動到 package.json 中的 dependencies 陣列,錯誤就會消失
我最初掩蓋了這一點。是的,聽起來您在生產代碼中使用這些庫,但由于它們不在依賴項中,因此它們不包含在您的生產包中。它們在開發中作業,因為您將它們作為開發依賴項。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/449560.html
標籤:javascript 节点.js 打字稿 npm pm2
