@@ -46,6 +46,7 @@ public OutOfProcessNodeInstance(
4646 string [ ] watchFileExtensions ,
4747 string commandLineArguments ,
4848 ILogger nodeOutputLogger ,
49+ bool isDevelopmentMode ,
4950 bool launchWithDebugging ,
5051 int ? debuggingPort )
5152 {
@@ -58,7 +59,7 @@ public OutOfProcessNodeInstance(
5859 _entryPointScript = new StringAsTempFile ( entryPointScript ) ;
5960
6061 var startInfo = PrepareNodeProcessStartInfo ( _entryPointScript . FileName , projectPath , commandLineArguments ,
61- launchWithDebugging , debuggingPort ) ;
62+ isDevelopmentMode , launchWithDebugging , debuggingPort ) ;
6263 _nodeProcess = LaunchNodeProcess ( startInfo ) ;
6364 _watchFileExtensions = watchFileExtensions ;
6465 _fileSystemWatcher = BeginFileWatcher ( projectPath ) ;
@@ -99,7 +100,7 @@ public void Dispose()
99100 // This method is virtual, as it provides a way to override the NODE_PATH or the path to node.exe
100101 protected virtual ProcessStartInfo PrepareNodeProcessStartInfo (
101102 string entryPointFilename , string projectPath , string commandLineArguments ,
102- bool launchWithDebugging , int ? debuggingPort )
103+ bool isDevelopmentMode , bool launchWithDebugging , int ? debuggingPort )
103104 {
104105 string debuggingArgs ;
105106 if ( launchWithDebugging )
@@ -130,11 +131,16 @@ protected virtual ProcessStartInfo PrepareNodeProcessStartInfo(
130131 }
131132
132133 var nodePathValue = existingNodePath + Path . Combine ( projectPath , "node_modules" ) ;
133- #if NET451
134- startInfo . EnvironmentVariables [ "NODE_PATH" ] = nodePathValue ;
135- #else
136- startInfo . Environment [ "NODE_PATH" ] = nodePathValue ;
137- #endif
134+ SetEnvironmentVariable ( startInfo , "NODE_PATH" , nodePathValue ) ;
135+
136+ // If there's no NODE_ENV variable, then set one based on the ASP.NET runtime environment
137+ var existingNodeEnv = Environment . GetEnvironmentVariable ( "NODE_ENV" ) ;
138+ if ( string . IsNullOrEmpty ( existingNodeEnv ) )
139+ {
140+ // These strings are the values recognized by Express.js and have become a de-facto standard in Node
141+ var nodeEnvValue = isDevelopmentMode ? "development" : "production" ;
142+ SetEnvironmentVariable ( startInfo , "NODE_ENV" , nodeEnvValue ) ;
143+ }
138144
139145 return startInfo ;
140146 }
@@ -179,6 +185,15 @@ private void EnsureFileSystemWatcherIsDisposed()
179185 }
180186 }
181187
188+ private static void SetEnvironmentVariable ( ProcessStartInfo startInfo , string name , string value )
189+ {
190+ #if NET451
191+ startInfo . EnvironmentVariables [ name ] = value ;
192+ #else
193+ startInfo . Environment [ name ] = value ;
194+ #endif
195+ }
196+
182197 private static Process LaunchNodeProcess ( ProcessStartInfo startInfo )
183198 {
184199 var process = Process . Start ( startInfo ) ;
0 commit comments