0

I have a config file named config.js that looks like this :

var config = {};
config.web = {};
config.web.param = 'oneParam';

module.exports = config;

I also use Webpack to give alias to my modules so I have this line in my webpack.config.js :

alias: {
  configFile: 'app/config.js'
}

I then try to import my config file with this :

import config from 'configFile';

Inside a React Component. However, when I try to access configvariable, all I get is an undefined error.

My full webpack.config.js

var webpack = require('webpack');

module.exports = {
  entry: [
    './app/app.jsx'
  ],
  output: {
      path: __dirname,
      filename: './dist/bundle.js'
  },
  externals: {
    'Config': JSON.stringify()
  },
  resolve: {
    root: __dirname,
    alias: {
      configFile: 'app/config.js'
    },
    extensions: ['', '.js', '.jsx']
  },
  module: {
    loaders: [
      {
        loader: 'babel-loader',
        query: {
          presets: ['react', 'es2015', 'stage-0']
        },
        test: /\.jsx?$/,
        exclude: /(node_modules|bower_components)/
      },
      {test: /\.scss?$/, exclude: /node_modules/, loader: "style-loader!css-loader!sass-loader!"},
      {test: /\.css?$/, loader: "style-loader!css-loader!"},
      {test: /\.(png|jpg)$/, loader: "file-loader?name=./dist/images/[name].[ext]"}
    ]
  },
  devtool: 'cheap-module-source-map'
};

What am I doing wrong here ?

3 Answers 3

1

Try

 import * as config from 'configFile';

And try console.log the config variable and see if you get not defined. If it is still not defined, the problem lies in exporting the configFile.

Sign up to request clarification or add additional context in comments.

5 Comments

Ok this seems to do trick, so thank you ! Here's where I am at the moment (I'll edit the question to reflect the actual status of my problem). I went back to module.exports = config: in my config.js When I use @slopeofhope suggestion, I can effectively log the config variable. But why ? I'm clueless...
I accepted your answer as correct because it worked. Could you tell me (or redirect me somewhere) why this works and not my initial import ? (import config from 'configFile';). I only have one object inside my config.js, so I thought it was sufficient to "declare" it without "*".
clearly your reasoning is correct. I would like to think of this as more of compatibility issue. like you are importing using es6 syntax now also try to export using es6 syntax. let me know if it does not work out.
I tried multiple combination of import/export and... now it even works with the exact same configuration I posted on my question (so, module.exports = config; and `import config from 'configFile';). I'm lost to be honest, I'm almost certain I didn't change anything else (nothing in the webpack.config) so I don't know... I'm still glad I could use your answer to gain knowledge on the subject so thank you.
so a lot of times any interpreter will try to build a cached file after you run the first time to make it run faster so in this case, it's prolly using a cached file. now if you delete the cached file..you will again get the exception.
0

Your path 'app/config.js' will the most probably point to node_modules/app/config.js.

The simplest solution will be changing your path in alias to absolute (or more proper in your scenario):

alias: {
  configFile: '/app/config.js'
}

4 Comments

That doesn't seem to do the trick. It seems the name resolution works fine with 'app/config.js' because I don't have any error using webpack (if I use'/app/config.js'I got Module not found: Error: Cannot resolve 'file' or 'directory' ...
have you tried exporting in es6 syntax ? export default config
Here is the webpack.config : jsfiddle.net/yc1se7nL Note: I removed all my components aliases for clarity.
@MehdiJorJor : I tried with export default config instead of module.exports = config; Same issue.
0

You should define relative path to root:

alias: {
  configFile: './app/config.js'
}

or relative to contentBase

2 Comments

When I do that, I have an error using webpack (Module not found: Error: Cannot resolve 'file' or 'directory' ./app/config.js). What I can do however is specify the path like this '../config.js',but I still got the issue.
@Clafou I would try it without root and extensions properties.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.