Zeit/pkg: 找不到模块'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);

我在本地"配置"目录中有一个默认值:

{
    "message": "whodapunk?"
}

我的包装。

{
    "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';
}

我尝试直接链接到配置模块,但是之后,它开始抱怨它在我的配置文件夹中找不到和文件。这对我有用。

最新更新