ionic-environment-variables icon indicating copy to clipboard operation
ionic-environment-variables copied to clipboard

Error: Cannot find module "@app/env"

Open bhumin3i opened this issue 6 years ago • 4 comments

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;

bhumin3i avatar Oct 30 '18 12:10 bhumin3i

Have you solved this problem?

jiamoon avatar Nov 27 '18 08:11 jiamoon

@jiamoon no still facing same :(

bhumin3i avatar Nov 27 '18 12:11 bhumin3i

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.

Pietaman avatar Dec 20 '18 16:12 Pietaman

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.

aronAtWex avatar Apr 06 '21 22:04 aronAtWex