Skip to content

Instantly share code, notes, and snippets.

@dkwingsmt
Last active August 11, 2017 23:24
Show Gist options
  • Select an option

  • Save dkwingsmt/37cfca39948da637d29b90c8448155f9 to your computer and use it in GitHub Desktop.

Select an option

Save dkwingsmt/37cfca39948da637d29b90c8448155f9 to your computer and use it in GitHub Desktop.
"Module not found" when using named re-exporting from an alias
{
"presets": [
["es2015", {
"modules": false
}],
"stage-0"
],
"plugins": [
["module-resolver", {
"alias": {
"Root": "."
}
}]
]
}

Install

npm i

Run

./node_modules/.bin/webpack main.js --output-filename bundle.js

Case 1: named re-export, module: false (the base case)

  • Observation:

ERROR in ./main.js

Module not found: Error: Can't resolve 'Root/mod' in '/Users/tong/tmp/reexport-alias'

@ ./main.js 1:0-29

Case 2: default re-export, module: false

  • Modification:
    • In main.js: comment export {a} from 'Root/mod', uncomment export a from 'Root/mod'
  • Observation:

build passed

Case 3: named re-export, module: commonjs

  • Modification:
    • In main.js: revert what was done in case 2
    • In .babelrc: remove the line "modules": false
  • Observation:

build passed

Case 3: named re-export, module: commonjs

  • Modification:
    • In main.js: comment export {a} from 'Root/mod', uncomment export {a} from './mod'
  • Observation:

build passed

export {a} from 'Root/mod' // This will not work
//export a from 'Root/mod' // This will work
//export {a} from './mod' // This will work
export const a = 1;
export default 2;
{
"name": "reexport-alias",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.25.0",
"babel-loader": "^7.1.1",
"babel-plugin-module-resolver": "^2.7.1",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"webpack": "^3.5.3"
}
}
module.exports = {
module: {
rules: [
{
test: /\.js$/,
use: {
loader: 'babel-loader',
}
},
],
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment