ionic-environment-variables
ionic-environment-variables copied to clipboard
Error: Cannot find module "@app/env"
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;
Have you solved this problem?
@jiamoon no still facing same :(
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.
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.