我正在尝试使用节点- Postgres将我的应用程序连接到Postgres。我使用的代码是:
import React from 'react';
import pg from 'pg';
import fs from 'fs';
var cn = {
host: 'localhost', // server name or IP address;
port: 5432,
database: 'my_db',
user: 'myname',
password: 'mypass'
};它会产生错误:
index.js:5 Uncaught ReferenceError: __dirname is not defined
up @ index.js:5
__webpack_require__ @ bootstrap 539ecc7…:19
(anonymous function) @ client.js:4
__webpack_require__ @ bootstrap 539ecc7…:19
(anonymous function) @ index.js:3
console.EventEmitter._events @ index.js:81
__webpack_require__ @ bootstrap 539ecc7…:19
(anonymous function) @ app.js:26957
__webpack_require__ @ bootstrap 539ecc7…:19
content @ bootstrap 539ecc7…:39
__webpack_require__ @ bootstrap 539ecc7…:19
(anonymous function) @ bootstrap 539ecc7…:39
(anonymous function) @ bootstrap 539ecc7…:39
webpackUniversalModuleDefinition @ universalModuleDefinition:7
(anonymous function) @ universalModuleDefinition:10控制台中提供的index.js路径是webpack:/./~/pg/~/pgpass/lib/index.js:5
我在how to write file with node webkit js?尝试了@Renzo解决方案,但仍然得到了相同的错误。我认为这可能与https://github.com/nwjs/nw.js/wiki/Differences-of-JavaScript-contexts#resolving-relative-paths-to-other-scripts和https://github.com/nwjs/nw.js/issues/264的讨论有关。他们说
// __dirname is not defined in webkit context, this is only node.js thing
console.log(__dirname); // undefined和
__dirname在Node.js模块中工作,即在使用JavaScript ()调用的JavaScript代码中工作。__dirname不仅在WebKit脚本中工作,也不适用于使用WebKit调用的JavaScript代码、jQuery的$.getScript()或任何其他类似的方法。
有什么想法吗?让我知道我还需要包括哪些其他信息。
编辑
我想我的目标是
var config = {
entry: { app: './src/index.jsx'},
output: {
libraryTarget: 'umd',
path: path.join(__dirname, 'dist'),
filename: '[name].js'
}, ...我的webpack.config.js看起来像:
...
node: {
console: true,
__dirname: true,
dns: 'empty',
fs: 'empty',
net: 'empty',
tls: 'empty'
}
...发布于 2016-03-18 17:48:14
在这种情况下,将其添加到webpack配置中:
{
node: {
__dirname: true
}
}这将告诉webpack用模块的路径替换__dirname实例。这条相对于context的路径
https://stackoverflow.com/questions/36090842
复制相似问题