компиляция файлов .jsx с помощью npm - PullRequest
1 голос
/ 08 июня 2019

Я пытаюсь создать полный стек Django и React, следуя этому учебнику.

Сейчас я нахожусь в точке, где я должен запустить скрипт dev для создания файла main.js, но у меня возникли проблемы.

Если назвать мои файлы ____. Js, я не могу вернуть HTML-код.Я могу сделать это, только если я назову свои файлы ___. Jsx.Но когда я это делаю, скрипт не работает, говоря: «Модуль не найден: ошибка: не удается разрешить» ./components/App'"

Дерево моих файлов:

├── package-lock.json
├── package.json
├── unimanager
│   ├── db.sqlite3
│   ├── ems
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   │   ├── __init__.cpython-37.pyc
│   │   │   ├── admin.cpython-37.pyc
│   │   │   ├── apps.cpython-37.pyc
│   │   │   ├── models.cpython-37.pyc
│   │   │   ├── permissions.cpython-37.pyc
│   │   │   ├── serializers.cpython-37.pyc
│   │   │   ├── urls.cpython-37.pyc
│   │   │   └── views.cpython-37.pyc
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── migrations
│   │   │   ├── 0001_initial.py
│   │   │   ├── __init__.py
│   │   │   └── __pycache__
│   │   │       ├── 0001_initial.cpython-37.pyc
│   │   │       └── __init__.cpython-37.pyc
│   │   ├── models.py
│   │   ├── permissions.py
│   │   ├── serializers.py
│   │   ├── tests.py
│   │   ├── urls.py
│   │   └── views.py
│   ├── frontend
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   │   ├── __init__.cpython-37.pyc
│   │   │   ├── admin.cpython-37.pyc
│   │   │   ├── models.cpython-37.pyc
│   │   │   ├── urls.cpython-37.pyc
│   │   │   └── views.cpython-37.pyc
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── migrations
│   │   │   ├── __init__.py
│   │   │   └── __pycache__
│   │   │       └── __init__.cpython-37.pyc
│   │   ├── models.py
│   │   ├── src
│   │   │   ├── components
│   │   │   │   ├── App.jsx
│   │   │   │   └── layout
│   │   │   │       └── Header.jsx
│   │   │   └── index.js
│   │   ├── static
│   │   │   └── frontend
│   │   │       └── main.js
│   │   ├── templates
│   │   │   └── frontend
│   │   │       └── index.html
│   │   ├── tests.py
│   │   ├── urls.py
│   │   └── views.py
│   ├── manage.py
│   └── unimanager
│       ├── __init__.py
│       ├── __pycache__
│       │   ├── __init__.cpython-37.pyc
│       │   ├── settings.cpython-37.pyc
│       │   ├── urls.cpython-37.pyc
│       │   └── wsgi.cpython-37.pyc
│       ├── settings.py
│       ├── urls.py
│       └── wsgi.py
├── webpack.config.js
└── .babelrc

Package.json:

{
  "name": "kayra",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "webpack --mode development ./unimanager/frontend/src/index.js --output ./unimanager/frontend/static/frontend/main.js",
    "build": "webpack --mode production ./unimanager/frontend/src/index.js --output ./unimanager/frontend/static/frontend/main.js"
  },
  "repository": {
    (redacted)
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "homepage": "(redacted)",
  "devDependencies": {
    "@babel/core": "^7.4.5",
    "@babel/preset-env": "^7.4.5",
    "@babel/preset-react": "^7.0.0",
    "babel-loader": "^8.0.6",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "webpack": "^4.33.0",
    "webpack-cli": "^3.3.2"
  },
  "dependencies": {
    "prop-types": "^15.7.2",
    "react": "^16.8.6",
    "react-dom": "^16.8.6"
  }
}

.babelrc:

{
  "presets": ["@babel/preset-env", "@babel/preset-react"],
  "plugins": ["transform-class-properties"]
}

webpack.config.js:

module.exports = {
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader",
                }
            },
            {
                test: /\.jsx$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader",
                    options: {
                        presets: ["@babel/preset-env", "@babel/preset-react"]
                    }
                }
            }
        ]
    }
}

index.js:

import App from './components/App'

App.jsx:

import React, { Component } from 'react';
import ReactDOM from 'react-dom';

import Header from './layout/Header'

class App extends Component {
    render() {
        return (<Header />)
    }
}

ReactDOM.render(<App />, document.getElementById('app'));

Ожидаемый результат - отсутствие ошибок, компиляция и рендеринг индексного HTML-файла.Текущий результат - это сообщение об ошибке, когда команда «npm run build» или «npm run dev»:

> kayra@1.0.0 dev /Users/kayra/Projects/Unitraining/kayra
> webpack --mode development ./unimanager/frontend/src/index.js --output ./unimanager/frontend/static/frontend/main.js

Hash: f556d5c5e69069927c65
Version: webpack 4.33.0
Time: 1310ms
Built at: 06/07/2019 3:44:24 PM
  Asset      Size  Chunks             Chunk Names
main.js  4.11 KiB    main  [emitted]  main
Entrypoint main = main.js
[./unimanager/frontend/src/index.js] 35 bytes {main} [built]

ERROR in ./unimanager/frontend/src/index.js
Module not found: Error: Can't resolve './components/App' in '/Users/kayra/Projects/Unitraining/kayra/unimanager/frontend/src'
 @ ./unimanager/frontend/src/index.js 1:0-35
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! kayra@1.0.0 dev: `webpack --mode development ./unimanager/frontend/src/index.js --output ./unimanager/frontend/static/frontend/main.js`
npm ERR! Exit status 2
npm ERR! 
npm ERR! Failed at the kayra@1.0.0 dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/kayra/.npm/_logs/2019-06-07T22_44_24_365Z-debug.log
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...