gshigeto / ionic-environment-variables

Easy to use environment variables for Ionic3!
MIT License
277 stars 36 forks source link

Error: Cannot find module "@app/env" #34

Open bhumin3i opened 5 years ago

bhumin3i commented 5 years ago

package.json


  "scripts": {
    "clean": "ionic-app-scripts clean",
    "build": "ionic-app-scripts build",
    "lint": "ionic-app-scripts lint",
    "ionic:build": "ionic-app-scripts build",
    "ionic:serve": "ionic-app-scripts serve",
    "debug:ios": "SENTRY_SKIP_AUTO_RELEASE=true ionic cordova prepare",
    "build:ios": "SENTRY_SKIP_AUTO_RELEASE=true ionic cordova build ios",
    "serve:prod": "LLI_ENV=prod ionic-app-scripts serve"
  },
"config": {
      "ionic_webpack": "./config/webpack.config.js",
      "ionic_generate_source_map": "true"
    },

weback.config.js

var chalk = require("chalk");
var fs = require('fs');
var path = require('path');
var useDefaultConfig = require('@ionic/app-scripts/config/webpack.config.js');

var env = process.env.LLi_ENV;

useDefaultConfig.prod.resolve.alias = {
  "@app/env": path.resolve(environmentPath('prod'))
};

useDefaultConfig.dev.resolve.alias = {
  "@app/env": path.resolve(environmentPath('dev'))
};

if (env !== 'prod' && env !== 'dev') {
  // Default to dev config
  useDefaultConfig[env] = useDefaultConfig.dev;
  useDefaultConfig[env].resolve.alias = {
    "@app/env": path.resolve(__dirname + 'config/config.' + process.env.LLi_ENV + '.ts'),
  };
}

function environmentPath(env) {
  var filePath = './src/environments/environment' + (env === 'prod' ? '' : '.' + env) + '.ts';
  if (!fs.existsSync(filePath)) {
    console.log(chalk.red('\n' + filePath + ' does not exist!'));
  } else {
    return filePath;
  }
}

module.exports = function () {
  return useDefaultConfig;
};

tsconfig.JSON

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [
      "dom",
      "es2015"
    ],
    "module": "es2015",
    "moduleResolution": "node",
    "sourceMap": true,
    "target": "es5",
    "baseUrl": "./src",
    "paths": {
      "@app/env": [
        "environments/environment"
      ]
    }
  },
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules",
    "src/**/*.spec.ts",
    "src/**/__tests__/*.ts"
  ],
  "compileOnSave": false,
  "atom": {
    "rewriteTsconfig": false
  }
}

USE in my code

import { ENV } from '@app/env'; //error

SERVER_URL: string = ENV.mode;

jiamoon commented 5 years ago

Have you solved this problem?

bhumin3i commented 5 years ago

@jiamoon no still facing same :(

Pietaman commented 5 years ago

If your files in your "./src/environments" folder have a ".dev.ts" or ".prod.ts" extension, this does not match the path settings in tsconfig.json. So either you have to use one file named "environments.ts" or you have to list (at least one) files in the tsconfig with an extension you are currently using.

aronAtWex commented 3 years ago

For some reason I just got this to happen to me. app/env just stopped working. Out of the blue when I build for ionic browser.
Works if I build local, find if I build for ios and android.

Cannot find module '@app/env' or its corresponding type declarations.