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 #38

Open
morganney opened this issue Jun 26, 2023 · 0 comments · May be fixed by #40
Open

SyntaxError: Cannot use import statement outside a module #38

morganney opened this issue Jun 26, 2023 · 0 comments · May be fixed by #40

Comments

@morganney
Copy link

While running Jest against tests for an ES module that uses astravel as a dependency, I encountered the following SyntaxError:

morgan@mactop magic-comments-loader (develop) $ npm run test:spec -- __tests__/loader.parser.spec.js 

> magic-comments-loader@1.5.1 test:spec
> NODE_OPTIONS='--loader=babel-register-esm --no-warnings --experimental-vm-modules' BABEL_ENV=test jest -c jest.config.spec.js __tests__/loader.parser.spec.js

 FAIL  __tests__/loader.parser.spec.js
  parser
    ✕ parses most complex constructs for dynamic imports (1393 ms)

parser > parses most complex constructs for dynamic imports
    [
      {
        moduleIdentifier: 'javascript/esm|/Users/morgan/code/magic-comments-loader/src/index.js!/Users/morgan/code/magic-comments-loader/__tests__/__fixtures__/complex.js',
        moduleName: './__fixtures__/complex.js',
        message: 'Module build failed (from ../src/index.js):\n' +
          '/Users/morgan/code/magic-comments-loader/node_modules/astravel/dist/module/astravel.js:1\n' +
          'import { defaultTraveler } from "./defaultTraveler.js";\n' +
          '^^^^^^\n' +
          '\n' +
          'SyntaxError: Cannot use import statement outside a module\n' +
          '    at internalCompileFunction (node:internal/vm:73:18)\n' +
          '    at wrapSafe (node:internal/modules/cjs/loader:1177:20)\n' +
          '    at Module._compile (node:internal/modules/cjs/loader:1221:27)\n' +
          '    at Module._extensions..js (node:internal/modules/cjs/loader:1311:10)\n' +
          '    at Module.load (node:internal/modules/cjs/loader:1115:32)\n' +
          '    at Module._load (node:internal/modules/cjs/loader:962:12)\n' +
          '    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:165:29)\n' +
          '    at ModuleJob.run (node:internal/modules/esm/module_job:192:25)\n' +
          '    at async CustomizedModuleLoader.import (node:internal/modules/esm/loader:246:24)\n' +
          '    at async parse (file:///Users/morgan/code/magic-comments-loader/src/util.js:838:41)',
morgan@mactop magic-comments-loader (develop) $ node -v
v20.3.1
morgan@mactop magic-comments-loader (develop) $ npm -v
9.7.2
morgan@mactop magic-comments-loader (develop) $ npm ls astravel
magic-comments-loader@1.5.1 /Users/morgan/code/magic-comments-loader
└── astravel@0.6.1

package.json

{
  "name": "magic-comments-loader",
  "version": "1.5.1",
  "description": "Add webpack magic comments to your dynamic imports at build time",
  "main": "dist",
  "type": "module",
  "exports": {
    ".": {
      "import": "./dist/index.js",
      "require": "./dist/cjs/index.cjs",
      "default": "./dist/index.js"
    },
    "./package.json": "./package.json"
  },
  "engines": {
    "node": ">=12.17.0"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/morganney/magic-comments-loader.git"
  },
  "bugs": {
    "url": "https://github.com/morganney/magic-comments-loader/issues"
  },
  "scripts": {
    "prepack": "npm run build",
    "build": "npm run build:esm && npm run build:cjs",
    "build:esm": "babel src --out-dir dist",
    "build:cjs": "BABEL_ENV=cjs babel src --out-dir dist/cjs --out-file-extension .cjs && node cjs.js",
    "lint": "eslint . src __tests__ --ext .js,.cjs",
    "lint:fix": "npm run lint -- --fix",
    "test:unit": "node --experimental-vm-modules --no-warnings ./node_modules/.bin/jest",
    "test:spec": "NODE_OPTIONS='--loader=babel-register-esm --no-warnings --experimental-vm-modules' BABEL_ENV=test jest -c jest.config.spec.js",
    "test": "npm run test:unit && npm run test:spec"
  },
  "keywords": [
    "webpack",
    "magic",
    "comments",
    "loader",
    "dynamic",
    "import"
  ],
  "author": "Morgan Ney <morganney@gmail.com>",
  "license": "MIT",
  "files": [
    "dist"
  ],
  "devDependencies": {
    "@babel/cli": "^7.22.5",
    "@babel/core": "^7.22.5",
    "@babel/eslint-parser": "^7.22.5",
    "@babel/preset-env": "^7.22.5",
    "babel-register-esm": "^1.2.4",
    "codecov": "^4.0.0-0",
    "eslint": "^8.42.0",
    "eslint-config-prettier": "^8.8.0",
    "eslint-plugin-jest": "^27.2.1",
    "eslint-plugin-prettier": "^4.2.0",
    "jest": "^29.5.0",
    "jest-light-runner": "^0.5.0",
    "memfs": "^4.0.0",
    "prettier": "^2.8.8",
    "webpack": "^5.87.0"
  },
  "dependencies": {
    "acorn": "^8.9.0",
    "acorn-jsx": "^5.3.2",
    "acorn-stage3": "^4.0.0",
    "astravel": "^0.6.1",
    "astring": "^1.8.6",
    "loader-utils": "^3.2.1",
    "micromatch": "^4.0.4",
    "schema-utils": "^4.1.0"
  }
}

jest.config.spec.js

export default {
  collectCoverage: true,
  collectCoverageFrom: ['**/src/**/*.js'],
  coverageProvider: 'babel',
  coverageReporters: ['text', 'text-summary'],
  modulePathIgnorePatterns: ['dist'],
  /**
   * Use alternative runner to circumvent segmentation fault when
   * webpack's node.js API uses dynamic imports while running
   * jest's v8 vm context code.
   *
   * @see https://github.com/nodejs/node/issues/35889
   * @see https://github.com/nodejs/node/issues/25424
   */
  runner: 'jest-light-runner',
  testMatch: ['**/__tests__/**/*.spec.js'],
  transform: {}
}

I've noticed that you provide ESM and CJS builds via conditional exports in your package.json file. I believe this error is because you do not include "type": "module" in your package.json file so that consumers attempting to load the ES variant of astravel from an ES module context trigger the exception.

@morganney morganney linked a pull request Jun 28, 2023 that will close this issue
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

Successfully merging a pull request may close this issue.

1 participant