I am currently working on a project in React and I use Typescript as language. In my project I have Webpack installed. Everything works fine but now, since we are going to production, I would like to have an easy way to store/retrieve config settings such as server URL (which is usually different between development, testing and production phases) and I got stuck. I tried to use the webpack.config.js file by adding the "externals" key:
externals: {
'config': JSON.stringify(process.env.ENV === 'production' ? {
serviceUrl: "https://prod.myserver.com"
} : {
serviceUrl: "http://localhost:8000"
})
}
and then try to reference the file from my tsx component files as such (take into account that the webpack.config.js is in the root folder and my components in /ClientApp/components):
import config from '../../webpack.config.js';
or
import {externals} from '../../webpack.config.js';
but I get the following error message:
'webpack.config.js' was resolved to '[PROJECT_DIR]/webpack.config.js', but '--allowJs' is not set.
Any solution/alternative to solve this issue? Thanks
If you're using typescript, just rename the file extension to .ts instead of .js. (foo.ts) . Then when you import, do not use the file extension: import ( Foo ) from '../node_modules/foo/foo';(fromcoffeenexusin typescript Github issue)