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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: TypeError: template is not a function #13719

Closed
1 task
I-Want-ToBelieve opened this issue Sep 1, 2021 · 2 comments
Closed
1 task

[Bug]: TypeError: template is not a function #13719

I-Want-ToBelieve opened this issue Sep 1, 2021 · 2 comments
Labels
i: question outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@I-Want-ToBelieve
Copy link

I-Want-ToBelieve commented Sep 1, 2021

馃捇

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

mkdir demo;
cd demo;
echo 'import template from "@babel/template";
import generate from "@babel/generator";
import * as t from "@babel/types";

const buildRequire = template(`
  var %%importName%% = require(%%source%%);
`);

const ast = buildRequire({
  importName: t.identifier("myModule"),
  source: t.stringLiteral("my-module"),
});

console.log(generate(ast).code);' > index.js;
echo '{
  "name": "demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js","type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "author",
  "license": "ISC",
  "devDependencies": {
    "@babel/generator": "^7.15.0",
    "@babel/template": "^7.14.5",
    "@babel/types": "^7.15.0"
  }
}
' > package.json;
yarn;
node index.js;

Configuration file name

No response

Configuration

No response

Current and expected behavior

TypeError: template is not a function

Environment

System:
OS: Linux 5.13 Arch Linux
Binaries:
Node: 14.17.4 - ~/.nvm/versions/node/v14.17.4/bin/node
Yarn: 1.22.11 - /usr/bin/yarn
npm: 6.14.14 - ~/.nvm/versions/node/v14.17.4/bin/npm
npmPackages:
@babel/generator: ^7.15.0 => 7.15.0
@babel/template: ^7.14.5 => 7.14.5
@babel/types: ^7.15.0 => 7.15.0

Possible solution

No response

Additional context

template.default is a function

@babel-bot
Copy link
Collaborator

Hey @backtolife2021! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite."

@nicolo-ribaudo
Copy link
Member

This is because of how Node.js's strategy to import CJS from ESM works.

When using native ESM, you'll need something like this:

import _template from "@babel/template";
const template = _template.default;

This will work better when we'll switch to ESM (#11701, #13414), but it takes time.

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Dec 2, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: question outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

3 participants