简体   繁体   中英

Typescript + Webpack + Node and process.env is empty

I am trying to compile my application first with TypeScript then with Webpack. I need this because my server has to be a single js file.

You can download my problem project here:

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

Results in

{ 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] }
{}

you have to inject environment variables into your project by using npm package. dotenv is the most popular one. all you have to do, in the root directory of your project, which is where your package.json is, create .env file and add your env varibles like this:

DB_URL=DB_URI=mongodb://127.0.0.1:27017/myDb // without strings

then in package.json inject the variables with a script:

      "dev": "nodemon --watch dist --exec  \"node -r dotenv/config dist/bundle.js\"",

I re-read the title, and I guess your problem is that process.env is empty. Am I right? In that case, this is how you add environmental variables.

process.env.someVariable = "some variable";
console.log(process.env);

If that's not your question, would you specify what is expected result.

Also, you can store your environmental variables in .env file that must be placed in root directory of your project. To access them you can use dotenv npm package.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM