required namespace never provided -- ES6 Modules and Closure

860 views Asked by At

I am trying to use ES6 modules while compiling my code using Closure. While it is discouraged, it seems supported.

I have tried to reduce an example to the minimum and this is my current situation.

File Structure

Path: /user/.../myapp/js/

Contains:

  • closure.jar
  • test/app.js
  • test/module.js

test/module.js

const A = function() { return 'Hello, World' };

export { A }

test/app.js

import { A } from '/test/module.js'

const text = A();
console.log(text);

Compilation Command

Then, I run this command to compile my code:

java -jar closure.jar \
  --js test/**.js \
  --js_output_file test/main.js \
  --dependency_mode=STRICT \
  --entry_point test/app.js \
  --language_in ECMASCRIPT6

Error

I get the following error:

test/app.js:1: ERROR - required "module$test$module" namespace never provided
import { A } from '/test/module.js'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Questions: Why is this happening? How can I fix it?

Thank you!


Import Tests

I have tried a few import options such as:

import { A } from '/test/module.js'
import { A } from 'test/module.js'
import { A } from '/test/module'
import { A } from 'test/module'
import { A } from './module.js'
import { A } from './module'
import { A } from '/module.js'
import { A } from '/module'
import { A } from 'module.js'
import { A } from 'module'

Different imports will throw different errors as they may not detect the file. Examples:

test/app.js:1: WARNING - Failed to load module "/test/module"
import { A } from '/test/module'

or

test/app.js:1: WARNING - Invalid module path "test/module.js" for resolution mode "BROWSER"
import { A } from 'test/module.js'

Export Tests

Using the original import:

import { A } from '/test/module.js'

I have also used a few export alternatives:

export function A() { return 'Hello, World' };

export default function A() { return 'Hello, World' };
0

There are 0 answers