Promoting Babelized ES module code to native Node 14+

Viewed 45

I have lot of javascript written to be run by nodejs, but which we ran through the Babel loader at runtime, so that we could write ES syntax -- in particular using import rather than require.

We have a layout like:

package.json
node_modules/
  ...packages...
top/
  server.js
  fribbity.js

server.js looks like (I've elided the babel import boilerplate) :

import {fribbity} from 'top/fribbity'

const x = fribbity()

console.log(`fribbity = ${x}`)

while fribbity.js might be

export const fribbity = () => 17

I'd like to promote all this code to use native ES modules in Node 14+. I added "type": "module" to package.json. But now I've run into the module resolution rules. By default, Node now expects my import in server.js to be

import {fribbity} from './fribbity.js'

Are there settings I can apply in package.json, or on the node command line, that would enable node to resolve the imports as they were originally written? That is, preserving the deep import path style (import string begins with no slash or dot, and ends without the ".js" extension)? I've tried several false starts.

0 Answers
Related