Rodrigo Rodrigo - 1 year ago 132
Javascript Question

no window object present webpack nodejs

I'm using webpack with babel to compile my ecmascript 6 code. Everything works fine but if I add certain dependeciens like the requests npm package. Here are my files:


import os from 'os'

export class User {

constructor(username) {
this.username = username;

register() {

var client = new User("hey")


webpack config:

var webpack = require('webpack')

module.exports = {
entry: [
output: {
path: "dist",
publicPath: "/dist/",
filename: "stela.js"
watch: false,
module: {
loaders: [{
test: /\.js$/,
// excluding some local linked packages.
// for normal use cases only node_modules is needed.
exclude: /node_modules/,
loader: 'babel'
}, {
test: /\.json$/,
loader: 'json-loader'
externals: {
fs: '{}',
tls: '{}',
net: '{}',
console: '{}'
babel: {
presets: ['es2015'],
plugins: ['transform-runtime']
resolve: {
modulesDirectories: ['node_modules']

Now if I run
and then run
node dist/stella.js
everything works fine it logs out
; however, if I add certain dependencies like the
npm package:

import request from 'request'

I run
everything compiles down with no errors but then I try running
node dist/stella.js
and I get this error:

throw new Error('no window object present');

Answer Source

By default, Webpack is set up to target the browser, not a Node environment. Try setting target in your config:

module.exports = {
    // ...
    target: "node",
    // ...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download