ngx-rocket / generator-ngx-rocket

:rocket: Extensible Angular 14+ enterprise-grade project generator
https://ngx-rocket.github.io/
MIT License
1.53k stars 218 forks source link

Cannot find module './.env' or its corresponding type declarations #575

Closed Wintermute79 closed 3 years ago

Wintermute79 commented 3 years ago

I'm submitting a...

Current behavior

On very first npm start right after creating a new project, I get the following error:

ERROR in src/environments/environment.ts:11:21 - error TS2307: Cannot find module './.env' or its corresponding type declarations.

11 import { env } from './.env';

Expected behavior

no error message

Minimal reproduction of the problem with instructions

  1. npm install -g generator-ngx-rocket
  2. ngx new
  3. npm start

Environment



ngX-Rocket: 9.1.0
Node.js: v15.0.1
Npm: 7.0.3
OS: win32 x64 10.0.19041

Generated project options:
{
  "generator-ngx-rocket": {
    "version": "9.1.0",
    "props": {
      "location": "path",
      "strict": false,
      "skipInstall": false,
      "skipQuickstart": false,
      "initGit": true,
      "usePrefix": true,
      "appName": "rocket2",
      "target": [
        "web"
      ],
      "pwa": false,
      "ui": "ionic",
      "layout": "side-menu",
      "auth": false,
      "lazy": false,
      "angulartics": false,
      "languages": [
        "de-DE",
        "en-US"
      ],
      "tools": [
        "prettier",
        "hads"
      ],
      "utility": [
        "lodash"
      ],
      "deploy": "none",
      "projectName": "rocket-2",
      "packageManager": "npm",
      "mobile": [],
      "desktop": []
    }
  }
}

     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/

Angular CLI: 10.1.7
Node: 15.0.1
OS: win32 x64

Angular: 10.2.2
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Ivy Workspace: Yes

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1001.7
@angular-devkit/build-angular   0.1001.7
@angular-devkit/core            10.1.7
@angular-devkit/schematics      10.1.7
@angular/cli                    10.1.7
@schematics/angular             10.1.7
@schematics/update              0.1001.7
rxjs                            6.6.3
typescript                      4.0.5

Wintermute79 commented 3 years ago

Here is another user that obviously had encountered this behavior before: https://stackoverflow.com/a/57115209/467341

jiniguez commented 3 years ago

I have found the same issue and I think this might be a npm bug.

@Wintermute79 Can you try executing npm run env ?

I am running npm 7.0.14 in Windows 10 and I get a list of environment variables. However, according to npm documentation:

The env script is a special built-in command that can be used to list environment variables that will be available to the script at runtime. If an "env" command is defined in your package, it will take precedence over the built-in.

Rocket has an "env" command defined in package.json but still does not take precendence over the built in command.

If you edit package.json and change the script name from env to env-fix, for example, running npm run env-fix works as intended and creates the missing environment file.

Wintermute79 commented 3 years ago

Thank you so much @jiniguez , that solves it.

Running npm run env just outputs my environment variables instead of executing the script defined in package.json.

Once I rename env (and all occurences) to env-fix in package.json as you suggested, it works. So the important line becomes: "env-fix": "ngx-scripts env npm_package_version"

Then the .env.ts file in src/environments is created as intended by npm run env-fix

I am running npm 7.0.3 and node 15.0.1 on Windows 10.

Mef45 commented 3 years ago

I have the same issue

https://github.com/ngx-rocket/scripts/issues/21

joboyx commented 3 years ago

Thank you so much @jiniguez , that solves it.

Running npm run env just outputs my environment variables instead of executing the script defined in package.json.

Once I rename env (and all occurences) to env-fix in package.json as you suggested, it works. So the important line becomes: "env-fix": "ngx-scripts env npm_package_version"

Then the .env.ts file in src/environments is created as intended by npm run env-fix

I am running npm 7.0.3 and node 15.0.1 on Windows 10.

Also encountered the issue and renaming "env" script to something else worked.

I'm running in MacOS 11.1; npm 7.0.8; node v15.2.1.

image

sinedied commented 3 years ago

Thanks for pinpointing the issue and the fix, I could never reproduce this one! Will push a fix.

ci-rebot commented 3 years ago

:tada: This issue has been resolved in version 9.2.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: