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

[BUG]: Cannot import used-pm from within ESM #22

Closed
sounisi5011 opened this issue May 6, 2023 · 1 comment · Fixed by #25
Closed

[BUG]: Cannot import used-pm from within ESM #22

sounisi5011 opened this issue May 6, 2023 · 1 comment · Fixed by #25
Labels
bug Something isn't working

Comments

@sounisi5011
Copy link
Contributor

Description of the bug

used-pm/README.md

Lines 27 to 33 in f724043

In `esm` / `typescript`:
```ts
import currentPackageManager from 'used-pm';
const { name, version } = currentPackageManager();
```

When this code is executed, Node.js throws the following error:

(node:14784) 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)
.../node_modules/used-pm/dist/esm/index.js:1
export default () => {
^^^^^^

SyntaxError: Unexpected token 'export'

Steps To Reproduce

  1. Go into the new directory and create a package.json file with the following contents:

    {
      "scripts": {
        "foo": "node bar.mjs"
      }
    }
  2. Run npm install [email protected]

  3. Create a bar.mjs file with the following contents:

    import currentPackageManager from 'used-pm';
    console.log(currentPackageManager());
  4. Run npm run foo - This command should fail with the following error:

    $ npm run foo
    
    > foo
    > node bar.mjs
    
    (node:14989) 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)
    .../node_modules/used-pm/dist/esm/index.js:1
    export default () => {
    ^^^^^^
    
    SyntaxError: Unexpected token 'export'
    ...

Additional Information

In order for a file with a .js extension to be interpreted as an ESM, you need to add "type": "module" to the package.json file.
Node.js and TypeScript will detect the "type": "module" in the package.json file in the nearest parent directory.
see package.json and file extensions - Modules: Packages | Node.js v20.1.0 Documentation

In the case of used-pm, I would suggest creating a dist/esm/package.json file after the build with the following content:

{"type":"module"}
@sounisi5011 sounisi5011 added the bug Something isn't working label May 6, 2023
mheob added a commit that referenced this issue May 6, 2023
After the last fix, javascript files stopped working.
This is now fixed by splitting the source files into separate commonJS and ESM files.

closed #22, closed #23
@kodiakhq kodiakhq bot closed this as completed in #25 May 7, 2023
kodiakhq bot pushed a commit that referenced this issue May 7, 2023
After the last fix, JavaScript files stopped working.
This is now fixed by splitting the source files into separate commonJS and ESM files.

Resolves #22 
Resolves #23

### Additional Notes

The following cases are working:

```
Test in Common JS: 'pnpm exec node foo.js':
{ name: 'pnpm', version: '8.4.0' }

Test in Common JS: 'pnpm exec node foo.cjs':
{ name: 'pnpm', version: '8.4.0' }

Test in ESM JS: 'pnpm exec node foo.mjs':
{ name: 'pnpm', version: '8.4.0' }

Test in TS: 'pnpm exec ts-node foo.ts':
{ name: 'pnpm', version: '8.4.0' }

Test in Common TS: 'pnpm exec ts-node foo.cts':
{ name: 'pnpm', version: '8.4.0' }
```

I am considering adding unit tests for this script (see #24).
@sounisi5011
Copy link
Contributor Author

This bug has been fixed in [email protected]. Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant