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

[ES modules] package.json located in root path can't be resolved when checking type field #33438

Closed
vitalets opened this issue May 16, 2020 · 2 comments · Fixed by #33476
Closed
Labels
confirmed-bug Issues with confirmed bugs. esm Issues and PRs related to the ECMAScript Modules implementation. module Issues and PRs related to the module subsystem.

Comments

@vitalets
Copy link

vitalets commented May 16, 2020

  • Version: v14.2.0
  • Platform: Linux c89b7c439bd7 4.19.76-linuxkit #1 SMP Fri Apr 3 15:53:26 UTC 2020 x86_64 Linux

What steps will reproduce the bug?

Create docker image that generates package.json and index.js in root of filesystem:

FROM node:14-alpine

RUN echo '{ "type": "module" }' > package.json
RUN echo 'import fs from "fs";' > index.js

CMD ["node", "index.js"]

Build and run the container:

docker build -t app .
docker run --rm app

Output:

(node:1) 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)
/index.js:1
import fs from "fs";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (vm.js:344:18)
    at wrapSafe (internal/modules/cjs/loader.js:1106:15)
    at Module._compile (internal/modules/cjs/loader.js:1140:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1196:10)
    at Module.load (internal/modules/cjs/loader.js:1040:32)
    at Function.Module._load (internal/modules/cjs/loader.js:929:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

How often does it reproduce? Is there a required condition?

Always.

What is the expected behavior?

index.js should be loaded as es module because the nearest package.json has "type": "module" field.

What do you see instead?

index.js is being loaded as commonjs.

Additional information

If package.json and index.js are generated in some subdirectory - everything works. You can check it with the following image:

FROM node:14-alpine

# add workdir to generate files in /app not in /
WORKDIR app 

RUN echo '{ "type": "module" }' > package.json
RUN echo 'import fs from "fs";' > index.js

CMD ["node", "index.js"]

The reason of such behavior is this line:

(separatorIndex = checkPath.lastIndexOf(path.sep)) > rootSeparatorIndex

When checkPath = '/index.js', both separatorIndex and rootSeparatorIndex are equals to 0 and condition does not pass.

@vitalets vitalets changed the title When [ES modules] package.json located in root path can't be resolved when checking type field May 16, 2020
@DerekNonGeneric
Copy link
Contributor

/cc @nodejs/modules-active-members

@guybedford
Copy link
Contributor

Yes this looks like a bug to me, we should support a type at the root. PRs to fix welcome, although understood testing might be an issue!

@GeoffreyBooth GeoffreyBooth added confirmed-bug Issues with confirmed bugs. esm Issues and PRs related to the ECMAScript Modules implementation. module Issues and PRs related to the module subsystem. labels May 20, 2020
GeoffreyBooth added a commit that referenced this issue May 22, 2020
Fix package.json files at the volume root so that
when they contain {"type": "module"}, they behave
as documented, like such a package.json file in
any other folder.

Fixes: #33438

PR-URL: #33476
Reviewed-By: Guy Bedford <[email protected]>
Reviewed-By: Jan Krems <[email protected]>
codebytere pushed a commit that referenced this issue Jun 18, 2020
Fix package.json files at the volume root so that
when they contain {"type": "module"}, they behave
as documented, like such a package.json file in
any other folder.

Fixes: #33438

PR-URL: #33476
Reviewed-By: Guy Bedford <[email protected]>
Reviewed-By: Jan Krems <[email protected]>
MylesBorins pushed a commit that referenced this issue Jul 14, 2020
Fix package.json files at the volume root so that
when they contain {"type": "module"}, they behave
as documented, like such a package.json file in
any other folder.

Fixes: #33438

PR-URL: #33476
Reviewed-By: Guy Bedford <[email protected]>
Reviewed-By: Jan Krems <[email protected]>
nodejs-github-bot pushed a commit that referenced this issue Aug 15, 2020
This patch converts the "read package scope" algorithm's while loop
into a do-while loop enabling items at the filesystem root dir to
be considered within the scope of a sibling package.json also at the
filesystem root dir.

Fixes: #33438

Co-authored-by: Guy Bedford <[email protected]>

PR-URL: #34595
Reviewed-By: Jan Krems <[email protected]>
Reviewed-By: Mary Marchini <[email protected]>
MylesBorins pushed a commit that referenced this issue Aug 17, 2020
This patch converts the "read package scope" algorithm's while loop
into a do-while loop enabling items at the filesystem root dir to
be considered within the scope of a sibling package.json also at the
filesystem root dir.

Fixes: #33438

Co-authored-by: Guy Bedford <[email protected]>

PR-URL: #34595
Reviewed-By: Jan Krems <[email protected]>
Reviewed-By: Mary Marchini <[email protected]>
BethGriggs pushed a commit that referenced this issue Aug 20, 2020
This patch converts the "read package scope" algorithm's while loop
into a do-while loop enabling items at the filesystem root dir to
be considered within the scope of a sibling package.json also at the
filesystem root dir.

Fixes: #33438

Co-authored-by: Guy Bedford <[email protected]>

PR-URL: #34595
Reviewed-By: Jan Krems <[email protected]>
Reviewed-By: Mary Marchini <[email protected]>
guybedford pushed a commit to guybedford/node that referenced this issue Sep 28, 2020
This patch converts the "read package scope" algorithm's while loop
into a do-while loop enabling items at the filesystem root dir to
be considered within the scope of a sibling package.json also at the
filesystem root dir.

Fixes: nodejs#33438

Co-authored-by: Guy Bedford <[email protected]>

PR-URL: nodejs#34595
Reviewed-By: Jan Krems <[email protected]>
Reviewed-By: Mary Marchini <[email protected]>
codebytere pushed a commit that referenced this issue Oct 1, 2020
This patch converts the "read package scope" algorithm's while loop
into a do-while loop enabling items at the filesystem root dir to
be considered within the scope of a sibling package.json also at the
filesystem root dir.

Fixes: #33438

Co-authored-by: Guy Bedford <[email protected]>

PR-URL: #34595
Backport-PR-URL: #35385
Reviewed-By: Jan Krems <[email protected]>
Reviewed-By: Mary Marchini <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs. esm Issues and PRs related to the ECMAScript Modules implementation. module Issues and PRs related to the module subsystem.
Projects
None yet
4 participants