如何在普通 javascript 文件或简单节点项目中从 React 外部的 normalizr 节点包导入规范化、模式函数
How to import normalize, schema function from normalizr node package outside of React in a plain javascript file or simple node project
我一定遗漏了一些关于将外部包函数导入我的纯 javascript 项目/文件的非常基本的内容。
我有一个简单的项目,我从 npm init 开始,然后安装了 normalizr 包。我必须 运行 下面这种规范化代码,当我 运行 React 组件中的下面代码时,这是完美的工作。
但是导入语句本身在我的简单明文中失败了 JavaScript file/project.
const normalize = "./node_modules/normalizr/dist/normalizr.js";
const schema = "./node_modules/normalizr/dist/normalizr.js";
const articlesData = {
articles: [
{
id: 1,
title: "Dagon",
tags: [{ id: 1, name: "old ones" }, { id: 2, name: "short story" }]
},
{
id: 2,
title: "Azathoth",
tags: [{ id: 1, name: "old ones" }, { id: 3, name: "novel" }]
},
{
id: 3,
title: "At the Mountains of Madness",
tags: [{ id: 4, name: "insanity" }, { id: 3, name: "novel" }]
}
]
};
const tag = new schema.Entity("tags", {});
const article = new schema.Entity("articles", {
tags: [tag]
});
// We assume articlesData is the (parsed) JSON object that we got
const normalizedData = normalize(articlesData, { articles: [article] });
console.log(articlesData);
我还尝试导入下面的 2 个函数(规范化、模式),但没有用
const normalize = require("normalizr");
const schema = require("normalizr");
并且还尝试了以下方法运行文件,同样失败
node --experimental-modules ./file.js
经历了这个official page,但对我帮助不大。
下面是我的package.json文件
{
"name": "test-js-codes-repo",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"babel": {
"presets": [
"@babel/preset-env"
]
},
"license": "ISC",
"dependencies": {
"@babel/cli": "^7.8.4",
"@babel/node": "^7.8.7",
"@babel/preset-env": "^7.8.7",
"axios": "^0.19.2",
"babel-core": "^6.26.3",
"babel-eslint": "^10.1.0",
"babel-loader": "^8.0.6",
"babel-plugin-dynamic-import-webpack": "^1.1.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1",
"babel-register": "^6.26.0",
"es6-promise": "^4.2.5",
"esm": "^3.2.25",
"https": "^1.0.0",
"isomorphic-fetch": "^2.2.1",
"lodash.flatten": "^4.4.0",
"lodash.map": "^4.6.0",
"lodash.omit": "^4.5.0",
"lodash.orderby": "^4.6.0",
"lodash.partialright": "^4.2.1",
"lodash.pick": "^4.4.0",
"lodash.sortby": "^4.7.0",
"moment": "^2.22.2",
"normalizr": "^3.6.0",
"request": "^2.88.0"
},
"description": "",
"devDependencies": {
"babel-plugin-dynamic-import-node": "^2.3.0"
}
}
像在 Node.js require docs
中导入任何其他模块一样导入它
const { normalize, schema } = require('normalizr')
我一定遗漏了一些关于将外部包函数导入我的纯 javascript 项目/文件的非常基本的内容。
我有一个简单的项目,我从 npm init 开始,然后安装了 normalizr 包。我必须 运行 下面这种规范化代码,当我 运行 React 组件中的下面代码时,这是完美的工作。
但是导入语句本身在我的简单明文中失败了 JavaScript file/project.
const normalize = "./node_modules/normalizr/dist/normalizr.js";
const schema = "./node_modules/normalizr/dist/normalizr.js";
const articlesData = {
articles: [
{
id: 1,
title: "Dagon",
tags: [{ id: 1, name: "old ones" }, { id: 2, name: "short story" }]
},
{
id: 2,
title: "Azathoth",
tags: [{ id: 1, name: "old ones" }, { id: 3, name: "novel" }]
},
{
id: 3,
title: "At the Mountains of Madness",
tags: [{ id: 4, name: "insanity" }, { id: 3, name: "novel" }]
}
]
};
const tag = new schema.Entity("tags", {});
const article = new schema.Entity("articles", {
tags: [tag]
});
// We assume articlesData is the (parsed) JSON object that we got
const normalizedData = normalize(articlesData, { articles: [article] });
console.log(articlesData);
我还尝试导入下面的 2 个函数(规范化、模式),但没有用
const normalize = require("normalizr");
const schema = require("normalizr");
并且还尝试了以下方法运行文件,同样失败
node --experimental-modules ./file.js
经历了这个official page,但对我帮助不大。
下面是我的package.json文件
{
"name": "test-js-codes-repo",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"babel": {
"presets": [
"@babel/preset-env"
]
},
"license": "ISC",
"dependencies": {
"@babel/cli": "^7.8.4",
"@babel/node": "^7.8.7",
"@babel/preset-env": "^7.8.7",
"axios": "^0.19.2",
"babel-core": "^6.26.3",
"babel-eslint": "^10.1.0",
"babel-loader": "^8.0.6",
"babel-plugin-dynamic-import-webpack": "^1.1.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1",
"babel-register": "^6.26.0",
"es6-promise": "^4.2.5",
"esm": "^3.2.25",
"https": "^1.0.0",
"isomorphic-fetch": "^2.2.1",
"lodash.flatten": "^4.4.0",
"lodash.map": "^4.6.0",
"lodash.omit": "^4.5.0",
"lodash.orderby": "^4.6.0",
"lodash.partialright": "^4.2.1",
"lodash.pick": "^4.4.0",
"lodash.sortby": "^4.7.0",
"moment": "^2.22.2",
"normalizr": "^3.6.0",
"request": "^2.88.0"
},
"description": "",
"devDependencies": {
"babel-plugin-dynamic-import-node": "^2.3.0"
}
}
像在 Node.js require docs
中导入任何其他模块一样导入它const { normalize, schema } = require('normalizr')