npm install --save-dev dotenv后,打包的时候回识别到吗
时间: 2024-04-20 19:27:09 浏览: 88
在使用 `dotenv` 库时,需要注意以下几点:
1. `dotenv` 库只是将环境变量从 `.env` 文件中读取到 Node.js 的 `process.env` 中,它并不会影响打包过程。
2. 在使用 `dotenv` 库时,需要将 `.env` 文件添加到 `.gitignore` 文件中,避免将敏感信息提交到代码仓库中。
3. 在打包时,需要为不同的环境设置不同的环境变量。例如,可以使用 webpack 的 DefinePlugin 插件设置环境变量:
```js
const webpack = require('webpack')
module.exports = {
// ...
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"',
API_URL: JSON.stringify(process.env.API_URL)
}
})
]
}
```
在上面的示例中,我们设置了 `NODE_ENV` 和 `API_URL` 两个环境变量,其中 `API_URL` 的值从 Node.js 的 `process.env` 中读取。
在打包时,可以通过设置环境变量来传递不同的值:
```bash
API_URL=https://api.example.com npm run build
```
这样,在打包过程中,`API_URL` 的值就是 `https://api.example.com`。
阅读全文
相关推荐


















