發表文章

目前顯示的是 10月, 2025的文章

簡記:解決Firebase部屬Next.js使用pnpm的部屬失敗問題

在本機端檢查與建置都會成功,但是總是在傳到雲端(cloud build)後發生錯誤 索性就安裝這個錯誤訊息所指出的套件 pnpm install @opentelemetry/api@1.9.0 解決這個部屬問題... 拿這個問GPT它會說,這應該是只有使用pnpm時才會發生,因為firebase-tools只使用與npm操作的狀況 另外,每次pnpm run build建置都會顯示的warning也很煩 pnpm install -D esbuild 0.19.2 裝完會寫說有東西需要approve我就確認 pnpm approve-builds 空白鍵選擇 esbuild再按確認,完畢!

簡記:Node.js使用google sheet API讀取(新版google-auth&typescript)

 這裡是serviceaccount的json放在環境變數中,並googleapis套件操作google sheet api。 因為新版google auth套件有改版把fromJSON()和給廢掉,導致升級版本程式要調整,跟google&AI弄了弄,範例都給我奇怪的結果,這篇把這些整理起來 pnpm add googleapis import { google , sheets_v4 } from 'googleapis' ; async function main ( ... args : string []) { const credentials = JSON . parse (process. env . SHEET_CREDENTIALS ); const authClient = new google . auth . JWT ({ scopes : [ 'https://www.googleapis.com/auth/spreadsheets' ] }) authClient . fromJSON ( credentials ); const sheetAPIs = google . sheets ({ version : 'v4' , auth : authClient }); const res = await sheetAPIs . spreadsheets . get ({ spreadsheetId : process. env . SHEET_ID }); if ( ! res . ok ) throw Error(`http error code:${ res . status }`); console . log ( res . data ) } const args = process . argv . slice ( 2 ); main ( ... args ). catch ( console . error ); scopes上面是提供讀寫 如果只讀取,就改成 scopes: ['https://www.go...

簡記:在typescript中自定義環境變數內容

在原始碼最上層建立檔案 env.d.ts 寫入以下相關, 其他所需的變數請自行舉一反三增加 declare namespace NodeJS { interface ProcessEnv { /** * App 執行環境 * - development: 開發環境 * - production: 正式環境 * - test: 測試環境 */ NODE_ENV : 'development' | 'production' | 'test' ; /** 僅伺服器端 */ PRODUCT_SHEET_CREDENTIALS : string ; PRODUCT_SHEET_ID : string ; STORAGE_CREDENTIALS : string ; STORAGE_BUCKET_NAME : string ; /** 公開給前端 */ NEXT_PUBLIC_FIREBASE_SDK_CONFIGS : string ; NEXT_PUBLIC_API_URL : string ; } }  

簡記:node.js v20中設定typescript與.env

pnpm init pnpm add -D typescript @types/node src/index.ts async function main ( ... args : string []) { console . debug ( 'process.env.NODE_ENV=' , process . env . NODE_ENV ) console . debug ( 'process.env.STORAGE_BUCKET_NAME=' , process . env . STORAGE_BUCKET_NAME ) } const args = process . argv . slice ( 2 ); main ( ... args ). catch ( console . error ); package.json "type" : "commonjs" , "scripts" : { "build" : "tsc" , "dev" : "pnpm run build && node --env-file=.env.development.local ." , "start" : "pnpm run build && node --env-file=.env.production.local ." }, tsconfig.json "compilerOptions" : { "module" : "CommonJS" , "moduleResolution" : "Node" , "outDir" : "dist" , "rootDir" : "src" 這個設定的優點是import不需要寫.js, 缺點就是沒有top-...