Recently switched to Mongosh for scripting from the older mongo shell. I use several external libraries, so I was hoping to leverage the “import” statement to slim down my loaded js to only the necessary functions rather than loading the entire bundle. Example importing functions from the “date-fns” library. This produces an error, see below.
Example:
import {subDays} from ‘date-fns’
Uncaught:
SyntaxError: ‘import’ and ‘export’ may appear only with ‘sourceType: “module”’ (1:0)
Consider renaming the file to ‘.mjs’, or setting sourceType:module or sourceType:unambiguous in your Babel config for this file.
1 | import {subDays} from ‘date-fns’
- | ^*
- 2 |*
>
It is possible to import a single function using “require”, e.g. subDays=require(‘date-fns/subDays’), but it seems a bit clumsy. What am I missing here?
MongoDB version: 5.0.4
MongoSH version: 1.1.4
OS: Ubuntu 18.04