[英]Zeit/pkg: Cannot find module 'config'
我已經將這個問題提煉為一個簡單的測試。 我正在使用節點“config”模塊來定義我的應用程序的配置值。 Pkg 不會抱怨構建,但會在運行時發出以下消息。 我錯過了什么嗎?
jim-macbookpro:~/development/node/pkgtest$ ./pkgtest-macos
pkg/prelude/bootstrap.js:1172
throw error;
^
Error: Cannot find module 'config'
1) If you want to compile the package/file into executable, please pay attention to compilation warnings and specify a literal in 'require' call. 2) If you don't want to compile the package/file into executable and want to 'require' it from filesystem (likely plugin), specify an absolute path in 'require' call using process.cwd() or process.execPath.
at Function.Module._resolveFilename (module.js:540:15)
at Function.Module._resolveFilename (pkg/prelude/bootstrap.js:1269:46)
at Function.Module._load (module.js:470:25)
at Module.require (module.js:583:17)
at Module.require (pkg/prelude/bootstrap.js:1153:31)
at require (internal/module.js:11:18)
at Object.<anonymous> (/snapshot/pkgtest/index.js:1:78)
at Module._compile (pkg/prelude/bootstrap.js:1243:22)
at Object.Module._extensions..js (module.js:650:10)
at Module.load (module.js:558:32)
index.js 很簡單:
const config = require('config');
console.log('yo:', config.message);
我在本地“config”目錄中有一個 default.json:
{
"message": "whodapunk?"
}
我的 package.json,它的價值:
{
"name": "pkgtest",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"config": "^1.30.0"
},
"bin": "index.js"
}
我遇到了同樣的問題,這是因為我的配置文件已添加到.gitignore
- 一旦我從那里刪除它,它就像一個魅力!
我只是有同樣的問題。 然而,這並不是一個非常漂亮的解決方案,我設法找到了解決它的方法。
調用 pkg 時,我沒有設置 e NODE_ENV。 然后在我的入口點我檢查是否設置了 NODE_ENV。 如果沒有,我在那里定義我需要的變量。
let port = null;
let db = null;
let name = null;
if (process.env.NODE_ENV) {
const config = require('config');
port = config.get('port');
db = config.get('database');
name = config.get('name');
} else {
port = 3000;
db = 'mongodb://localhost:27017/production';
name = 'Server Production';
}
我嘗試直接鏈接到 config 模塊,但之后它開始抱怨它在我的 config 文件夾中找不到文件。 這對我來說是一種解決方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.