没有窗口对象存在webpack nodejs
我正在使用带有babel的webpack来编译我的ecmascript 6代码。一切正常,但如果我添加某些dependeciens喜欢请求npm包。这是我的文件:
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:
main.js
import os from 'os'
export class User {
constructor(username) {
this.username = username;
}
register() {
console.log("registering...");
}
}
var client = new User("hey")
console.log(user.register());
webpack config:
webpack config:
var webpack = require('webpack')
module.exports = {
entry: [
'./src/main.js'
],
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']
}
}
现在如果我运行 webpack
然后运行 node dist / stella.js
一切正常,它注销注册...
;但是,如果我添加某些依赖项,如 requests
npm package:
Now if I run webpack
and then run node dist/stella.js
everything works fine it logs out registering...
; however, if I add certain dependencies like the requests
npm package:
...
import request from 'request'
...
I运行 webpack
一切都编译好没有错误然后我尝试运行 node dist / stella.js
然后我收到此错误:
I run webpack
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');
默认情况下,Webpack设置为定位浏览器,而不是Node环境。尝试在配置中设置 target
:
By default, Webpack is set up to target the browser, not a Node environment. Try setting target
in your config:
module.exports = {
// ...
target: "node",
// ...
}