Closed mounoydev closed 4 years ago
You can make it work if you use require
instead:
const SCD30 = require('scd30-node').SCD30;
The import
keyword does not work out of the box in Node.js. If you want to use import
, you have a couple of options:
"type": "module"
to your package.json
(see
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import, the Browser Compatibility section)I'm sorry if the README is misleading.
I fixed the example in README to use require
and it now works out of the box.
Let me know if you face any other issues. Thanks for reporting!
pi@raspberrypi:~/m $ node index.js /home/pi/m/index.js:1 import {SCD30} from 'scd30-node'; ^^^^^^
SyntaxError: Cannot use import statement outside a module at wrapSafe (internal/modules/cjs/loader.js:1072:16) at Module._compile (internal/modules/cjs/loader.js:1122:27) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10) at Module.load (internal/modules/cjs/loader.js:1002:32) at Function.Module._load (internal/modules/cjs/loader.js:901:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) at internal/main/run_main_module.js:18:47 pi@raspberrypi:~/m $ nodejs index.js /home/pi/m/index.js:1 (function (exports, require, module, filename, dirname) { import {SCD30} from 'scd30-node'; ^
SyntaxError: Unexpected token { at new Script (vm.js:79:7) at createScript (vm.js:251:10) at Object.runInThisContext (vm.js:303:10) at Module._compile (internal/modules/cjs/loader.js:657:28) at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10) at Module.load (internal/modules/cjs/loader.js:599:32) at tryModuleLoad (internal/modules/cjs/loader.js:538:12) at Function.Module._load (internal/modules/cjs/loader.js:530:3) at Function.Module.runMain (internal/modules/cjs/loader.js:742:12) at startup (internal/bootstrap/node.js:283:19)