Noah
Noah

Reputation: 491

Node CLI argv (arguments array) is empty

I am creating my first command line node module using Typescript, transpiled using Webpack. The process.argv property where I expect to find an array of the command line arguments is empty. Currently the only code I have simply logs the the process object to the console. This is the result.

myArgs:  { nextTick: [Function],
 title: 'browser',
 browser: true,
 env: {},
 argv: [],
 version: '',
 versions: {},
 on: [Function: noop],
 addListener: [Function: noop],
 once: [Function: noop],
 off: [Function: noop],
 removeListener: [Function: noop],
 removeAllListeners: [Function: noop],
 emit: [Function: noop],
 binding: [Function],
 cwd: [Function],
 chdir: [Function],
 umask: [Function] }

I've noticed that "browser" is set to true. Does this mean node thinks it's running in a browser and not on the command line.

FYI: I am running on OSX.

Upvotes: 1

Views: 1517

Answers (2)

Noah
Noah

Reputation: 491

Thanks Jason Livesay for putting me on the right track. The default target for Webpack is the browser.

In order to "pack" a module that executes in a node like environment you must set the 'target' property in your 'webpack.config.js' to 'node' as in my example below:

    var config = {
    target: 'node',
    entry: './src/index.ts',
    output: {
        filename: 'dist/index.js'
    },
    resolve: {
        extensions: ['', '.webpack.js', '.web.js', '.ts', '.js']
   },
    module: {
        loaders: [
        { test: /\.ts$/, loader: 'ts-loader' }
        ]
    },
   externals : {
        "archiver" : "archiver",
        "lodash" : "lodash",
        "xmlbuilder" : "xmlbuilder",
    }
};

module.exports = config;

Upvotes: 4

Jason Livesay
Jason Livesay

Reputation: 6377

Why are you using webpack on the server code -- that's for the browser and explains the argv issue.

Upvotes: 0

Related Questions