I'm trying to import crypto-js so I can use any of the crypto-js functions. I can't successfully import it when I serve a static test webpage from Express.js.
I get the error Uncaught SyntaxError: import not found: sha256 in the browser console.
To reproduce with code snippets below:
npm install expressnpm install crypto-jsindex.html and index.js files both in the /public directorynode app.js to run it127.0.0.1:3000/static/I have tried changing the path of the import statement to import sha256 from 'crypto-js/sha256'; but that just leads to another error.
app.js
const express = require('express');
const app = express();
const port = 3000;
const path = require('path');
app.use('/static' , express.static(path.join(__dirname,'/node_modules')));
app.use('/static', express.static(path.join(__dirname, '/public')));
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
index.html
<!DOCTYPE html>
<html>
<head>
<title> Test Page! </title>
<script src="index.js" type="module" ></script>
</head>
<body>
<h1>TEST PAGE!</h1>
</body>
</html>
index.js
import {sha256} from '/static/crypto-js/sha256.js';