Pkg: Is there any other way to resolve dynamic require

Created on 22 Sep 2017  路  2Comments  路  Source: vercel/pkg

Hi All,

compilation warnings:

> Warning Cannot resolve 'TEST_ARRAY[argv.topic].backend'
  /home/nilesh/component/index.js
  Dynamic require may fail at run time, because the requested file
  is unknown at compilation time and not included into executable.
  Use a string literal as an argument for 'require', or leave it
  as is and specify the resolved file name in 'scripts' option.

index.js:-

...
const {TEST_ARRAY} = require('./config/constants.file.js');
...
const
    argv     = require('./lib/sss.yargs.js').parseArgs(),
    client = require(TEST_ARRAY[argv.topic].backend);
...

./config/constants.file.js:-

...
constants.TEST_ARRAY = {};
constants.TEST_ARRAY[constants.qqqqq] = {
    description: ...,
    backend: 'some value',
    queue: ..,
    logger: ...
}
...

./lib/sss.yargs.js:-

...

function parseArgs() {
    const options      = _buildOptions();
    const descriptions = _buildDescriptions();

    return require('yargs')
            .usage('Usage: $0 -t <topic> -p <aaaaa> [-td <bbbbb>]')
                .example(`$0 -t ${qqqqq} -p ${ccccc}`)
                ...
            .help('h')
            .alias('h', 'help')
            .argv;
}

...

package.json

"bin":"index.js",
"pkg":  {
  "scripts": "./lib/**/*:./config/**/*",
  "assets": "./lib/**/*:./config/**/*"
}

Most helpful comment

I'm using pkg with browserify for solving this kind of problems since browserify is smarter and have well-defined rules and plugins for require(), something like this:

browserify myappindex.js --no-bundle-external  --no-builtins  -o bundle.js
pkg -t node8-linux -c pkg.json -o execfile bundle.js

All 2 comments

I'm using pkg with browserify for solving this kind of problems since browserify is smarter and have well-defined rules and plugins for require(), something like this:

browserify myappindex.js --no-bundle-external  --no-builtins  -o bundle.js
pkg -t node8-linux -c pkg.json -o execfile bundle.js

Thanks for the reply.
I will try using browserify.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hellower picture hellower  路  3Comments

telunc picture telunc  路  4Comments

gpip picture gpip  路  3Comments

ByeongYeon picture ByeongYeon  路  4Comments

Admiral-Enigma picture Admiral-Enigma  路  3Comments