【发布时间】:2019-09-16 15:50:01
【问题描述】:
我尝试先用 TypeScript 编译我的应用程序,然后再用 Webpack。我需要这个,因为我的服务器必须是单个 js 文件。
你可以在这里下载我的问题项目:
https://s3-ap-southeast-2.amazonaws.com/test-bucket-alsh/untitled+folder.zip
index.ts
console.log(process)
console.log(process.env)
webpack.config.js
module.exports = {
entry: path.join(__dirname, '/index.ts'),
mode,
node: {
console: true,
global: true,
process: true,
__filename: true,
__dirname: true,
Buffer: true,
setImmediate: true
},
output: {
filename: 'index.js',
path: __dirname
},
module: {
rules: [
{
test: /\.ts$/,
use: ['ts-loader']
}
]
}
}
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"baseUrl": "./",
"outDir": "./dist/",
"sourceMap": true,
"esModuleInterop": true,
"lib": [
"es2016"
],
"types": [
"node"
]
},
"exclude": [
"node_modules"
]
}
npx webpack && node index.js
结果
{ 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],
prependListener: [Function: noop],
prependOnceListener: [Function: noop],
listeners: [Function],
binding: [Function],
cwd: [Function],
chdir: [Function],
umask: [Function] }
{}
【问题讨论】:
-
转译的
index.js的内容是什么? -
预期结果是什么?你console.log两个对象,一个是process,另一个是process.env,里面是空的。怎么了?
-
环境应该填充我系统的环境,但是它是空的。假设我运行
MYVAR=something && npx webpack && node index.js,process.env.MYVAR将是undefined -
MYVAR=something && npx webpack && node index.jsMYVAR=something 应该在 EVERY COMMAND 前面明确添加您想要为其设置环境变量的内容。例如:npx webpack && MYVAR=something node index.js。否则你可以先export MYVAR=something它。 -
抱歉,语法不准确。我的观点是,在 shell 中显式设置环境变量并运行应用程序将证明不显示任何变量。用我提供的同一个应用程序试试,你会明白我的意思
标签: node.js typescript webpack