简体   繁体   中英

Zeit/pkg: Cannot find module 'config'

I've distilled this issue down to a simple test. I'm using the node "config" module to define configuration values for my app. Pkg doesn't complain on build, but barfs at runtime with the following message. Am I missing something?

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 is simple:

const config = require('config');
console.log('yo:', config.message);

and I have a default.json in the local 'config' directory:

{
    "message": "whodapunk?"
}

My package.json, for what it's worth:

{
    "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 - 一旦我从那里删除它,它就像一个魅力!

I just had the same issue. However not a really beautiful solution I managed to find a way to work around it.

When calling pkg I do not set e NODE_ENV. Then in my entry point I check if a NODE_ENV is set. If not I define the variables I need there.

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

I tried linking directly to the config module but after that it started complaining that it could not find and files in my config folder. This worked for me as a work around.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM