I'm trying to execute a node script inside package.json which looks like this.
"parse-xlsx": "node ./services/orders-parsing/xlsx-parser executeXlsxParsing ./private/testdata/$FILENAME"
So when I run FILENAME=unfried-xlsx-2.xlsx npm run parse-xlsx for exemple, it executes this:
import XlsxParser from "./executeExcelParsing";
const executeXlsxParsing = () => {
const xlsxParser = new XlsxParser({ fileName: process.argv.slice(2)[1] })
xlsxParser.executeParsing()
}
export default executeXlsxParsing;
But when I do so, I got this error
SyntaxError: Cannot use import statement outside a module
I did try to flag with --input-type
but did not work and it outputs the same error than the one above:
"parse-xlsx": "node --input-type=module ./services/orders-parsing/xlsx-parser executeXlsxParsing ./private/testdata/$FILENAME"
For some reason I don't want to use type=module
inside my package.json
file.
Any workaround to be able to "force" the execution of this script with ES6 imports? Did try with latest LTS node 16.17.0 and my "old" version v12.22.1.
Thanks !
node --input-type
only tells node how to parse string input you're passing it:
> node --help
Usage: node [options] [ script.js ] [arguments]
node inspect [options] [ script.js | host:port ] [arguments]
Options:
...
--input-type=... set module type for string input
...
However, you're telling it to run the file ./services/orders-parsing/xlsx-parser
so that won't work.
Your only option (until Node changes how --input-type
works) is to have a package.json with the type
property set to module
. Thankfully, you canhave lots of package.json files, and node
will simply use the one closest to the file it's being asked to run, so you can add a minimal package.json
inside your services/orders-parsing
dir, and then make sure that's set to "type": "module"
, and now Node will run in ESM mode when asked to run files in the services/orders-parsing
dir.
However, also note that if you want to pass arguments to xlsx-parser
, you will need to use:
"parse-xlsx": "node ./services/orders-parsing/xlsx-parser -- executeXlsxParsing ./private/testdata/$FILENAME"
with those explicit --
in there, because:
> node --help
Usage: node [options] [ script.js ] [arguments]
node inspect [options] [ script.js | host:port ] [arguments]
Options:
- script read from stdin (default if no file name is
provided, interactive mode if a tty)
-- indicate the end of node options
If you don't add those --
then everything is considered options for the node executable instead of for whatever script you're running.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.