Is there a way to import a constant to use in a module without modifying package.json or the html? [duplicate]
Solution 1:
I got this error because I forgot the type="module"
inside the script tag:
<script type="module" src="milsymbol-2.0.0/src/milsymbol.js"></script>
Solution 2:
Update For Node.js / NPM
Add "type": "module"
to your package.json
file.
{
// ...
"type": "module",
// ...
}
Note: When using modules, if you get ReferenceError: require is not defined
, you'll need to use the import
syntax instead of require
. You can't natively mix and match between them, so you'll need to pick one or use a bundler if you need to use both.
Solution 3:
It looks like the cause of the errors are:
-
You're currently loading the source file in the
src
directory instead of the built file in thedist
directory (you can see what the intended distributed file is here). This means that you're using the native source code in an unaltered/unbundled state, leading to the following error:Uncaught SyntaxError: Cannot use import statement outside a module
. This should be fixed by using the bundled version since the package is using rollup to create a bundle. -
The reason you're getting the
Uncaught ReferenceError: ms is not defined
error is because modules are scoped, and since you're loading the library using native modules,ms
is not in the global scope and is therefore not accessible in the following script tag.
It looks like you should be able to load the dist
version of this file to have ms
defined on the window
. Check out this example from the library author to see an example of how this can be done.