Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SyntaxError: Cannot use import statement outside a module #57

Closed
adrfantini opened this issue Nov 23, 2023 · 3 comments
Closed

SyntaxError: Cannot use import statement outside a module #57

adrfantini opened this issue Nov 23, 2023 · 3 comments

Comments

@adrfantini
Copy link

Hello, maybe this is a very dumb question, but I can't figure out how to test this package.

If I manually load it in Node using require, after installing with npm i unitmath:

> require('unitmath')
/dev/shm/unitmath/node_modules/unitmath/index.js:1
import UnitMath from './src/Unit.js'
^^^^^^

Uncaught:
SyntaxError: Cannot use import statement inside the Node.js REPL, alternatively use dynamic import:
>

Node using import:

> await import ("unitmath")
(node:145890) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
Uncaught [SyntaxError: Unexpected token (1:13)
] {
  pos: 13,
  loc: Position { line: 1, column: 13 },
  raisedAt: 23
}

Deno using import:

> await import('unitmath')
Uncaught TypeError: 'import', and 'export' cannot be used outside of module code at file:///dev/shm/unitmath/node_modules/.deno/unitmath@1.0.1/node_modules/unitmath/index.js:1:1

  import UnitMath from './src/Unit.js'
  ~~~~~~
    at async <anonymous>:1:22

The same seem to occur when running a .js or .mjs file with the following content:

const unit = require('unitmath')

let a = unit('5 m').div('2 s')   // 2.5 m / s
let b = unit('40 km').to('mile')  // 24.8548476894934 mile
b.toString({ precision: 4 })          // "24.85 mile"

What am I doing wrong?

@adrfantini
Copy link
Author

This is fixed by adding

  "exports": {
    ".": {
      "import": "./dist/UnitMath.js",
      "require": "./dist/UnitMath.js"
    }
  },

to the package.json.

I can open a PR if needed

@ericman314
Copy link
Owner

Thank you for reporting, and especially for your solution! I did not know about the exports keyword.

This should be fixed now in v1.0.2.

@adrfantini
Copy link
Author

Thank you for reporting, and especially for your solution! I did not know about the exports keyword.

This should be fixed now in v1.0.2.

No problem, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants