11

We are creating different components in reactJS,

Example:

App.js
index.js
LandingPage.js
.....

While importing this component in another component, we are not adding the extension .js

Example:

index.js:

import App from './App'

// here './App' we are not adding .js  

Does anyone know the reason why?

1

3 Answers 3

10

Your Webpack config is taking care of resolving the common extensions (ie: .js or .jsx). If your project is using create-react-app, then this is already done for you behind the scenes.

Create-react-app already resolves the following extensions automatically:

extensions: [".web.js", ".mjs", ".js", ".json", ".web.jsx", ".jsx"],

More info here https://github.com/webpack/docs/wiki/Configuration#resolveextensions

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

2 Comments

Well, to make life a little bit easier so you don't have to include the extension every single time. Plus, in case you change your file name from .js to .jsx for example, you don't have to modify your code!
Thanks, this pointed me in the right direction. For my case, I used a community template for create-react-app called typescript-empty. For some reason, they neglected to keep in the functionality that generates the tsconfig.json file. This causes a problem inside CRA's webpack config, where it will essentially ignore .ts and .tsx files. Resolved by copying a valid tsconfig from another project created with the plain 'ol typescript CRA template. :)
2

It all done by webpack module resolution, a resolver is a library which helps in locating a module by its absolute path.

The dependency module can be from the application code or a third-party library. The resolver helps webpack find the module code that needs to be included in the bundle for every such require/import statement. webpack uses enhanced-resolve to resolve file paths while bundling modules.

Once the path is resolved based on the above rule, the resolver checks to see if the path points to a file or a directory. If the path points to a file:

  • If the path has a file extension, then the file is bundled straightaway.
  • Otherwise, the file extension is resolved using the resolve.extensions option, which tells the resolver which extensions are acceptable for resolution e.g. .js, .jsx.

Resolve extensions: These options change how modules are resolved. webpack provides reasonable defaults, but it is possible to change the resolving in detail.

In webpack.config.js

module.exports = {
  //...
  resolve: {
    enforceExtension: false
  }
};

If the value is true here, it will not allow extension-less files. So by default require('./foo') works if ./foo has a .js extension, but with this (enforceExtension) enabled only require('./foo.js') will work.

1 Comment

It Resolves destination URLs. Feed it a source url and the resolver returns with the destination url if any redirects happened.
1

Add .js to resolve/extensions in webpack.config.js

resolve: {
  extensions: [".ts", ".js", ".mjs", ".json"],
  symlinks: false,
  cacheWithContext: false,
},

Comments

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.