I’ve been looking around and can’t seem to find a clear answer on this.
Is it possible to import a an index.js file from a client directory using the directory name (omitting /index.js
)? If so, how? This is a new project and I don’t really want to bundle my code at this point.
The node version is v17.1.0.
File structure
package.json
server.js
src/
index.js
utils/
index.js
package.json
{
"type": "module",
"main": "server.js",
"scripts": {
"start": "nodemon --ignore src"
},
"devDependencies": {
"express": "^4.17.1",
"nodemon": "^2.0.14"
}
}
server.js
import express from 'express'
express()
.use(express.static('src'))
.get('/', (_, res) => res.send(`
<script type="module" src="index.js"></script>`))
.listen(3000, () => console.log(`Running at http://localhost:3000`))
src/index.js
import { stuff } from './utils'
// Firefox: Loading module from “http://localhost:3000/utils/”
// was blocked because of a disallowed MIME type (“text/html”).
I’ve tried running the app with node --experimental-specifier-resolution=node server.js
and tried adding express.static.mime.define({'text/javascript': ['js']})
to server.js, but neither worked for me.