decaporg / decaporg/decap-cms

Error of importing module

Open
#7,307 0 comments 0 reactions 0 assignees View on GitHub
ssg: gatsby type: bug
Dominant language
JavaScript
Stars
19.4k
Forks
3.1k
Avg merge
1d 14h
Merged PRs (30d)
9

Description

**Describe the bug**

I get an error when importing a module at CMS's administrator page.

I use `registerPreviewTemplate` in cms.js and the error occurs at “import” in the component file loaded with it.
In the component file, I use alias like `import MyStyle from "@common/Style"`.
But, cms.js cannot interpret the alias.

**To Reproduce**

1, `npx decap-server`
2. In another session of shell, `npx gatsby develop`

The files are organized as follows.
```
/
 ├ src/
 │ └ cms/
 │   └ cms.js
 ├ templates/
 │ └ layout.js
 ├ common/
 │ └ Style.js
 ├ src/
 │ └ admin/
 │   └ config.yml
 ├ gatsby-config.js
 ├ gatsby-node.js
 └ jsconfig.json
```

cms.js
```js
import CMS from "decap-cms-app";

import MyLayout from "./../templates/layout"

CMS.registerPreviewTemplate("MyCollection", MyLayout);
```

layout.js
```js
import "@common/Style"

const MyLayout = () => {
return (
hogehoge
);
};

export default MyLayout;
```

config.yml
```
......
collections:
- name: "MyCollection"
label: "MyCollection"
......
```

gatsby-config.js
```js
......
{
resolve: `gatsby-plugin-decap-cms`,
options: {
modulePath: `${__dirname}/src/cms/cms.js`,
},
},
......
```

gatsby-node.js
```js
const path = require("path");

......

exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
resolve: {
alias: {
"@common": path.resolve(__dirname, "src/common/"),
},
},
});
};

```

jsconfig.json
```json
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@common/*": ["src/common/*"],
}
}
}

```

**Expected behavior**

```
error in ./src/templates/layout.js

Module not found: Error: Can't resolve '@common/Style' in '/src/templates'
```
**Screenshots**

**Applicable Versions:**

- Decap CMS version:
- decap-cms-app@3.1.10
- decap-cms-core@3.3.6
- gatsby-plugin-decap-cms@4.0.4
- Git provider: GitHub
- OS: ubuntu:20.04 (Docker container)
- Browser version: chrome 129.0

- Node.JS version: v18.12.1

**CMS configuration**

```yml
backend:
name: github
repo: ***
branch: master
commit_messages:
create: "Create {{collection}} “{{slug}}”"
update: "Update {{collection}} “{{slug}}”"
delete: "Delete {{collection}} “{{slug}}”"
uploadMedia: "[skip ci] Upload “{{path}}”"
deleteMedia: "[skip ci] Delete “{{path}}”"

locale: "ja"

publish_mode: editorial_workflow
media_folder: static/img
public_folder: /img

collections:
- name: "MyCollection"
label: "MyCollection"
......
```
**Additional context**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.