> npm run start:dev
它显示--- “STAGE”未被识别为内部或外部命令
我认为 .env 有问题。如果有人可以的话请回答。
代码
.env.stage.dev
MONGO_DATABASE=dbName
MONGO_USERNAME=myName
MONGO_PASSWORD=myPass
MONGO_HOST=localhost:27017
MONGO_CONNECTION_STRING=mongodb://localhost:27017
app.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { MongooseModule } from '@nestjs/mongoose';
import { TitleModule } from './titles/title.module';
import { TopicsModule } from './topics/topics.module';
import { AssetsModule } from './assets/assets.module';
import { ConfigModule, ConfigService } from '@nestjs/config';
@Module({
imports: [
MongooseModule.forRootAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => {
const username = configService.get('MONGO_USERNAME');
const password = configService.get('MONGO_PASSWORD');
const database = configService.get('MONGO_DATABASE');
const host = configService.get('MONGO_HOST');
const devConnectionString = configService.get(
'MONGO_CONNECTION_STRING',
);
return {
uri: `mongodb://${username}:${password}@${host}`,
dbName: database,
};
},
inject: [ConfigService],
}),
TitleModule,
TopicsModule,
AssetsModule,
ConfigModule.forRoot({
isGlobal: true,
envFilePath: [`.env.stage.${process.env.STAGE}`],
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
> pakcage.json
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "STAGE=dev nest start --watch",
"start:debug": "STAGE=dev nest start --debug --watch",
"start:prod": "STAGE=production node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch --detectOpenHandles",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json",
"offline": "npm run build && npx sls offline",
"tsc:check": "tsc --noEmit"
},
在package.json中
SET STAGE=dev nest start --watch
或
cross-env STAGE=dev nest start --watch